Emulating meta-refresh ... in Javascript!



  • The following Javascript code doesn't require much additional comment: 

    [code]

    <script language="JavaScript" type="text/javascript">
    <!--<br /> var Interval = 3;<br /> function startClock () {<br /> Interval = Interval - 1;<br /> if (Interval == -1) {<br /> window.location = unescape("http://new.url.com");<br /> } else {<br /> timerID = setTimeout("startClock()", 1000);<br /> }<br /> } // end func<br /> //-->
    </script>

    [/code]



     



  • Forum software vBulletin uses the following instead of meta http-equiv:




    [code]

    <script type="text/javascript">
    myvar = "";
    timeout = 300;
    function exec_refresh()
    {
    timerID = setTimeout("exec_refresh();", 100);
    if (timeout > 0)
    { timeout -= 1; }
    else { clearTimeout(timerID); window.location="online.php?order=asc&sort=username&pp=25&page=1"; }
    }
    exec_refresh();
    </script>

    [/code]




    It's used on the "Who's Online" pages and possibly in other places.



  • Well, the NoScript extension has an option to disable meta-refresh.

    Of course it also  disables JavaScript, so not sure what good this would do.  I've seen it in a lot of places though.



  • Well, there are stories floating around on the net of some legendary browser that is said to support javascript but not meta refresh. I personally didn't encounter it yet. But maybe those stories were the origins of code like this...

    I especially like how brilliantly the coder made sure the refresh occurs after three seconds instead of the usual one. Because calling setTimeout with something that is not a power of 10 will anger the gods as we all know. 



  • What are these developers doing? Aside from not using meta-refresh (and you get people who want that banned too), I love how the code doesn't take any arguments. Obviously, the best way to pause for three seconds before refreshing is to pause for one second, three times. Far more logical than startClock(3). Or, better, pause for 1/10th of a second, 30 times. Though the second example would pause for 300*100 ms, or 30 seconds. That cannot be right?



  • Maybe I'm just not javascript-fluent enough, but I've seen this before in a (very bad) ex-coworker's js before:

    <FONT face="Lucida Console"><script language="JavaScript" type="text/javascript">
    <!--</FONT>

    <FONT face="Lucida Console"></FONT> 

    <FONT face="Lucida Console">// enter js here


    //-->
    </script>

    What does the start and commented out end html comment do in js???</FONT>



  • @Morbii said:

    Maybe I'm just not javascript-fluent enough, but I've seen this before in a (very bad) ex-coworker's js before:

    <FONT face="Lucida Console"><script language="JavaScript" type="text/javascript">
    <!--</FONT>

    <FONT face="Lucida Console"></FONT> 

    <FONT face="Lucida Console">// enter js here<p<P <P="" <P="">
    //-->
    </script> <p <P="">What does the start and commented out end html comment do in js???<FONT </P="">



    Things like that appear in CSS also, its usually to make broken browsers behave that don't understand that that code between <script> and </script> is supposed to be hidden. <FONT </P="">
    </FONT>



  • That's no WTF but a common "hack" to increase browser compatibillity. Old browsers that do not understand javascript would treat the script code as normal text and display it on the page. To prevent this, the code is put into HTML comments. Browsers that support javascript simply ignore them and execute the script while those without support just see a big HTML comment that can be ignored.

    This hack is theoretically dangerous in XHTML because parsers are allowed to simply swallow comment elements even if the calling application would need them (which it shouldn't). I didn't hear of a case in practice where something happened though. Still, if you want to be on the save side, use external js files.



  • @PSWorx said:

    That's no WTF but a common "hack" to increase browser compatibillity. Old browsers that do not understand javascript would treat the script code as normal text and display it on the page.

    It should be noted that by "old" browsers, this means things like Netscape 1.0 and NCSA Mosaic, not anything written in the last 10 years or that is likely to run on anything after win2k. None of them support HTML 3.2, let alone anything after that. Your web pages do not work on them. 



  • I guess it's similar to the code that started this article then... the joys of cargo cult coding...



  • @Daniel Beardsmore said:

    Though the second example would pause for 300*100 ms, or 30 seconds. That cannot be right?

    That's what it does yes. The 30 secs is configurable. I think it defaults to 45 seconds on first-install.



  • @bairy said:

    That's what it does yes. The 30 secs is configurable. I think it defaults to 45 seconds on first-install.

    Oh, I see. It's one of those. I mistook it for one of those "If the page doesn't redirect automatically, we were too lame to read up on header ('Location: ...')" pages. But this is the greater evil: automatically refreshing pages. They're always the ones that lock up the browser during rendering, lurk in a background tab in a background window and make you wonder why the browser keeps freezing periodically when you're not loading anything.



  • @asuffield said:

    @PSWorx said:

    That's no WTF but a common "hack" to increase browser compatibillity. Old browsers that do not understand javascript would treat the script code as normal text and display it on the page.

    It should be noted that by "old" browsers, this means things like Netscape 1.0 and NCSA Mosaic, not anything written in the last 10 years or that is likely to run on anything after win2k. None of them support HTML 3.2, let alone anything after that. Your web pages do not work on them. 



    Yes, but it's in a lot of HTML books and tutorials so it lives on.  There's also a version using CDATA that I believe is added by HTMLtidy.



  • @Cap'n Steve said:



    Yes, but it's in a lot of HTML books and tutorials so it lives on. There's also a version using CDATA that I believe is added by HTMLtidy.

    A CDATA section lets you use < and & directly. Otherwise, you should use &lt; and &amp; instead.

    And in case you commented out the content of the <script> tags, you may also use -- directly (the end of comment is -- and not --> !) 



  • @Daniel Beardsmore said:

    I mistook it for one of those "If the page doesn't redirect automatically, we were too lame to read up on header ('Location: ...')" pages. But this is the greater evil: automatically refreshing pages.

    Oh! Yes, it's an auto refresh after x seconds. Sorry for the misunderstanding.


Log in to reply
 

Looks like your connection to What the Daily WTF? was lost, please wait while we try to reconnect.