Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    ksz
    ksz is offline
    New to the CF scene
    Join Date
    Mar 2012
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts

    I need an alternative in creating a <meta tags with JS code

    I am trying to build a meta tag in JS code. I ran into this thread http://www.codingforums.com/archive/.../t-243464.html which talks about building a JS tags with JS code. So I did the same thing for META tags. The following 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 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

  • #2
    Regular Coder
    Join Date
    Aug 2010
    Posts
    966
    Thanks
    19
    Thanked 211 Times in 209 Posts
    Quote Originally Posted by ksz View Post
    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 special mode.

    <head>

    <script>
    document.write('<meta http-equiv="X-UA-Compatible" content="IE=edge" />')
    </script>
    <style>

    </style>

    <script type="text/javascript">


    </script>

    </head>

  • #3
    ksz
    ksz is offline
    New to the CF scene
    Join Date
    Mar 2012
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Sorry to say it is not working. I placed it exactly as you suggested, but still it is not working.

  • #4
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,618
    Thanks
    0
    Thanked 645 Times in 635 Posts
    That meta tag needs to be the very first thing read - long before any JavaScript can run.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #5
    Regular Coder
    Join Date
    Aug 2010
    Posts
    966
    Thanks
    19
    Thanked 211 Times in 209 Posts
    Quote Originally Posted by felgall View Post
    That meta tag needs to be the very first thing read - long before any JavaScript can run.
    <head>

    <script>
    time=3;
    document.write('<meta http-equiv="refresh" content="'+time+'; url=http://example.com/">')
    </script>
    <style>

    </style>

    <script type="text/javascript">


    </script>

    </head>

  • Users who have thanked DaveyErwin for this post:

    ksz (03-30-2012)

  • #6
    ksz
    ksz is offline
    New to the CF scene
    Join Date
    Mar 2012
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts
    That is what I did originally, but still not working.

    I read somewhere about Document.body and doctype at this link Document.body and doctype. So I'm wondering if it has anything to do with it.

    I have to acknowledge that felgall is the one who listed the JS code in creating a <SCRIPT mentioned in my question. I commend you for your hard work and efforts. A+

  • #7
    ksz
    ksz is offline
    New to the CF scene
    Join Date
    Mar 2012
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts

  • #8
    ksz
    ksz is offline
    New to the CF scene
    Join Date
    Mar 2012
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts
    I just tried the following, but still not working :

    <head>
    <script>
    document.documentElement.write('<meta http-equiv="X-UA-Compatible" content="IE=edge" />')
    </script>
    .
    .
    .

  • #9
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,618
    Thanks
    0
    Thanked 645 Times in 635 Posts
    Quote Originally Posted by ksz View Post
    I just tried the following, but still not working :

    <head>
    <script>
    document.documentElement.write('<meta http-equiv="X-UA-Compatible" content="IE=edge" />')
    </script>
    .
    .
    .
    I(t will work if you just get rid of the unnecessary part of the code:

    Code:
    <head>
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    or better yet since it is a http-equiv simply serve the http header from the server so as to not try to apply it afterwards from the HTML.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #10
    ksz
    ksz is offline
    New to the CF scene
    Join Date
    Mar 2012
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts
    I know this works
    <head>
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />

    I never said it didn't. I am trying to see how to get the same result as above, but in JS code. Same as your JS code that created JS Tags.

    Thanks

  • #11
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,618
    Thanks
    0
    Thanked 645 Times in 635 Posts
    Quote Originally Posted by ksz View Post
    I know this works
    <head>
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />

    I never said it didn't. I am trying to see how to get the same result as above, but in JS code. Same as your JS code that created JS Tags.

    Thanks
    There is no JavaScript equivalent to that since there is no way you will ever get the JavaScript to run soon enough. Even with http-equiv in HTML you are sometimes pushing your luck getting the browser to read it in time. Such commands are best given using a server side language.

    For example the ideal way to do that code in PHP is with:

    Code:
    header("X-UA-Compatible: IE=edge");
    The HTML version is just a way to try to backward apply that command into the document that has already started loading. Unless it is the very first statement after the <head> tag it is going to be too late for it to be applied since it is really supposed to be applied before the doctype tag is received by the browser.

    Even JavaScript attached immediately after the head tag is unlikely to run soon enough to be able to apply an http-equiv - if it were then your approach using the antiquated document.write command would probably have a far better chance of actually working than any of the more modern JavaScript commands that replaced it after Netscape 4 disappeared.

    As an analogy, think about timing for making your lunch today. The PHP version makes your lunch in the morning prior to the start of your lunch break. The HTML version makes your lunch just as your lunchbreak is due to end so that if you eat quickly enough you still get to eat. The JavaScript version would be trying to makeyour lunch early in the evening long after the lunchbreak is over.
    Last edited by felgall; 03-28-2012 at 02:29 AM.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • Users who have thanked felgall for this post:

    ksz (03-30-2012)

  • #12
    ksz
    ksz is offline
    New to the CF scene
    Join Date
    Mar 2012
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts
    I'll go with your analogy, it makes sense.

    I do have one question though. The JS code works when I use:
    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);
    }

    Then I call addJavascript() during window.onload

    Also, it works when I use:
    document.head.insertAdjacentHTML( 'afterBegin', '<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />' );

    So JS works just fine with this META TAG:
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

    but it doesn't work with this META TAG:
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />

  • #13
    ksz
    ksz is offline
    New to the CF scene
    Join Date
    Mar 2012
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts
    I withdraw my last post, my bad.

    I am more convinced that felgall is correct with his analogy.

    Thank you guys

  • #14
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,618
    Thanks
    0
    Thanked 645 Times in 635 Posts
    Quote Originally Posted by ksz View Post
    I withdraw my last post, my bad.

    I am more convinced that felgall is correct with his analogy.

    Thank you guys
    As you have discovered the Content-Type http-equiv also needs to come very clost to the top in order to be recognised.

    http-equiv tags should be avoided where ever possible with the appropriate values being defined in the HTTP headers instead.

    For other meta tags that are not substituting for http headers the code you used for addJavaScript is the better solution but you don't have to wait for onload to run it - simply attach your JavaScript immediately before the </body> tag and it will be able to run without waiting for all the other files in the page to finish downloading - it only needs the HTML itself to be loaded which will be the case by that point.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #15
    ksz
    ksz is offline
    New to the CF scene
    Join Date
    Mar 2012
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Some how I missed reading one of the threads. It is the response from:
    DaveyErwin saying:
    <head>
    <script>
    time=3;
    document.write('<meta http-equiv="refresh" content="'+time+'; url=http://example.com/">')
    </script>
    <style>
    </style>
    <script type="text/javascript">
    </script>
    </head>

    I would like to try it, but I need to know:
    1- What goes in "http://example.com/"
    2- Which goes first:
    document.documentElement.write('<meta http-equiv="X-UA-Compatible" content="IE=edge" />')
    then
    document.write('<meta http-equiv="refresh" content="'+time+'; url=http://example.com/">')
    or the other way?
    3- What does "refresh" really do? Does it reload the page's code, by reading the code from its source again, or just refreshes the screen based on the already loaded code?

    I hope I made sense with my questions and sorry for missing that thread


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •