ksz
03-27-2012, 05:14 PM
I am trying to build a meta tag using JS code. I ran into this thread http://www.codingforums.com/archive/.../t-243464.html which talks about building a JS tags using JS code. So I did the same thing for META tags. The following code worked just fine:
function addJavascript() {
var th = document.getElementsByTagName('head')[0];
var s = document.createElement('meta');
s.setAttribute('http-equiv','Content-Type');
s.setAttribute('content','text/html; charset=utf-8');
th.appendChild(s);
}
I called addJavascript() at window.onload
Now I tried to do the same thing to this meta tag:
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
function addJavascript() {
var th = document.getElementsByTagName('head')[0];
var s = document.createElement('meta');
s.setAttribute('http-equiv','X-UA-Compatible');
s.setAttribute('content','IE=edge');
th.appendChild(s);
}
but nothing happens, as if I didn't declare this meta tag at all.
My guess this meta tag <meta http-equiv="X-UA-Compatible" content="IE=edge" /> needs to be loaded before window.onload because it puts the browser in a special mode.
Also, I tried:
document.head.insertAdjacentHTML( 'afterBegin', '<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />' );
and
document.head.insertAdjacentHTML( 'afterBegin', '<meta http-equiv="X-UA-Compatible" content="IE=edge" />' );
The first one worked, while the second was ignored
Also, someone suggested the following code, but it didn't work
<head>
<script>
document.write('<meta http-equiv="X-UA-Compatible" content="IE=edge" />')
</script>
<style>
</style>
<script type="text/javascript">
</script>
</head>
function addJavascript() {
var th = document.getElementsByTagName('head')[0];
var s = document.createElement('meta');
s.setAttribute('http-equiv','Content-Type');
s.setAttribute('content','text/html; charset=utf-8');
th.appendChild(s);
}
I called addJavascript() at window.onload
Now I tried to do the same thing to this meta tag:
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
function addJavascript() {
var th = document.getElementsByTagName('head')[0];
var s = document.createElement('meta');
s.setAttribute('http-equiv','X-UA-Compatible');
s.setAttribute('content','IE=edge');
th.appendChild(s);
}
but nothing happens, as if I didn't declare this meta tag at all.
My guess this meta tag <meta http-equiv="X-UA-Compatible" content="IE=edge" /> needs to be loaded before window.onload because it puts the browser in a special mode.
Also, I tried:
document.head.insertAdjacentHTML( 'afterBegin', '<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />' );
and
document.head.insertAdjacentHTML( 'afterBegin', '<meta http-equiv="X-UA-Compatible" content="IE=edge" />' );
The first one worked, while the second was ignored
Also, someone suggested the following code, but it didn't work
<head>
<script>
document.write('<meta http-equiv="X-UA-Compatible" content="IE=edge" />')
</script>
<style>
</style>
<script type="text/javascript">
</script>
</head>