SitePoint Sponsor

User Tag List

Results 1 to 2 of 2

Hybrid View

  1. #1
    SitePoint Member
    Join Date
    Jan 2002
    Location
    USA
    Posts
    19
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Making random JavaScript redirecter play nice with non-JavaScript-enabled browsers

    The index page of the site I'm working on has three different versions, and Management (truly a proper noun, in this case) wants one version displayed 50% of the time, and the second and third versions each displayed 25% of the time. It needs to be done with JavaScript, which makes me really sad; I know it'd be simple, elegant, and easy with PHP, and we'd get to avoid pesky browser bugs and people with JavaScript disabled. Without any chance of using server-side scripting, we're doomed to the client. Oh well.

    Since I want the index page to work with JavaScript disabled, I devised the following code. (The JavaScript may not be the most beautiful thing ever, but I'm a programming newbie and it was the best I could do. I'm sort of proud of it. )

    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html lang="en">
    <head>
    	<title>...</title>
    	<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">	
    	<link href="style.css" rel="stylesheet" type="text/css">
    	<script type="text/JavaScript">
    	<!--
    	// create random number: 1, 2, 3, or 4
    	var random_number = (Math.ceil(Math.random() * 4));
    
    	// loads page depending on random_number's outcome, without breaking back button
    	switch(random_number) {
    		case 1:
    			window.location.replace("page1.html");
    			break;
    		case 2:
    			window.location.replace("page1.html");
    			break;		
    		case 3:
    			window.location.replace("page2.html");
    			break;
    		case 4:
    			window.location.replace("page3.html");
    			break;
    		}				
    	// -->
    	</script>
    </head>
    <body>
    <noscript>
    	<!-- all HTML between body tags in original page1.html file,
    so they miss out on the random page stuff, but still get the one we wanted them to see 50% of the time anyway -->
    </noscript>
    </body>
    </html>
    I thought that browsers wouldn't render anything between noscript tags if JavaScript were enabled. This works fine in Internet Explorer 6.0, Netscape 6.2.1, Mozilla 0.9.7, Netscape 4.78, Lynx 3.2, and a little WebTV viewer on Windows 2000. In Opera 6.0, though, and on my graphic designer co-workers' IE 5.0 for Macintosh, the noscript content is flashed on the screen for a moment before the JavaScript randomizer thing actually runs and takes them to page1.html, page2.html, or page3.html. It doesn't look very good, and I think it's because a) Opera 6.0 renders pages really, really fast, and b) because those designers have huge new Macintoshes of unimaginable power, so that IE 5 renders pages really, really fast. But they shouldn't be displaying what's between the noscript tags anyway, right?

    Is there a better way to display a random page with JavaScript and still make it friendly for people with JavaScript disabled?

    Thanks a lot.

  2. #2
    SitePoint Guru Vincent Puglia's Avatar
    Join Date
    Feb 2000
    Location
    where the World once stood
    Posts
    700
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    Theoritically, you are correct. So...try putting the noscript tags between the head tags.

    You might be able to write a randomizer in a refresh meta tag (using the time%4 as the randNum number), but I'm only supposing...you'd need to check the standards to see what is and is not allowed

    Of course, you can always tell the javascriptDisabled to take a hike -- my feeling is: if you have javascript disabled, you should be reading printed material, not digitals

    Vinny
    Where the World Once Stood
    the blades of grass
    cut me still


Bookmarks

Posting Permissions

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