![]() Good journals: (by blogrolling.com) Monthly: January 2005 (1) May 2004 (1) August 2003 (1) July 2003 (8) June 2003 (6) May 2003 (5) April 2003 (5) March 2003 (4) February 2003 (6) January 2003 (7) December 2002 (10) November 2002 (13) October 2002 (4) September 2002 (8) August 2002 (6) July 2002 (1) May 2002 (7) April 2002 (13) March 2002 (16) February 2002 (12) January 2002 (14) Stuff I found: RinkWorks Nectarine Demoscene Radio Random Blog Link The Yumb Forums |
The Blob: Ciaran's Journal
Welcome to my journal. This is where I'll be revealing my innermost thoughts and feelings to the world. Occasionally I'll talk about other stuff, but mostly about my experiences. Mar 21, 2002: Checking for JavaScript (link | trackback: 0) Time: 09:15 - Mood: Happy - Location: Work - Now playing: n/a Now and again, you need to check that visitors to your site are using JavaScript (incidentally, this site doesn't require it at all). Often, you want to redirect to another page if they don't have JavaScript, telling them of this. The trouble is, how do you do such a thing without scripting? <g> The easiest way to do it is to redirect to two separate pages. To do this, add the following line to your <HEAD></HEAD> section: <META HTTP-EQUIV="refresh" CONTENT="1; URL=nojs.html"> and also change your BODY line to add: onLoad="document.location='js-supported.html'" So, your BODY line might look like this: <body bgcolor="#FFFFFF" onLoad="document.location='js-supported.html'"> This will cause JavaScript-enabled browsers to redirect to "js-supported.html", while others go to "nojs.html". Problem solved! ...or is it? Often, when people do that, they say in the non-JS page that "you have an old browser. Please upgrade to the latest browser." But it's perfectly plausible that visitors to the site simply have JavaScript turned off, sometimes without even knowing it. This annoyed me so much that I thought that there had to be a solution. However, I couldn't think of one, as theoretically, there shouldn't be any difference between a browser that doesn't understand JavaScript, and one that does but has it turned off. After scratching my head a bit and experimenting, I found that there is one - a three-way redirect. Browsers which don't understand JavaScript will normally attempt to parse it as normal HTML, which is why a lot of the JavaScript code you see include HTML comments in the script... looking something like this: <script language="JavaScript"> <!-- ...code goes here... // --> </script> As an old browser coming across this would parse it as normal HTML, the code would show up in the page if it weren't for the comments. However, JavaScript will ignore HTML tags such as the opening section of the comment. It won't ignore the end section by default, so a JavaScript comment is placed in front of it. My redirection trick relies on the fact that old browsers will parse the JavaScript as normal HTML. To make this work, do the same as above, but also put this lot in your <HEAD></HEAD> section: <script> <!-- // --><META HTTP-EQUIV="refresh" CONTENT="0; URL=/nojs-notsupported.html"> </script> The above code takes a bit of explaining. Here's what happens:
[back to main screen] |