Find the answer to your Linux question:
Results 1 to 3 of 3
I've been thinking for a while that'd it be cool to have my homepage be randomly selected from a list when I start my browser. I thought about making a ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Linux Engineer
    Join Date
    Jan 2005
    Location
    Chicago (USA)
    Posts
    1,028

    JavaScript error with Gecko


    I've been thinking for a while that'd it be cool to have my homepage be randomly selected from a list when I start my browser. I thought about making a Firefox extension, but I was too lazy to learn XUL. Also that wouldn't be cross-browser. So today I got an idea to do it in JavaScript and I've come up with:

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http&#58;//www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http&#58;//www.w3.org/1999/xhtml">
    	<head>
    		<meta http-equiv="Content-Type" content="application/xhtml+xml;charset=utf8"/>
    		<title></title>
    		<script type="text/javascript">
    			switch &#40;Math.round&#40;Math.random&#40;&#41;*5&#41;&#41;
    			&#123;
    				case 1&#58;
    					document.write&#40;"<meta http-equiv='Refresh' content='0;url=http&#58;//www.megatokyo.com/' />"&#41;
    					break;
    				case 2&#58;
    					document.write&#40;"<meta http-equiv='Refresh' content='0;url=http&#58;//www.unitedmedia.com/comics/dilbert/' />"&#41;
    					break;
    				case 3&#58;
    					document.write&#40;"<meta http-equiv='Refresh' content='0;url=http&#58;//www.foxtrot.com/' />"&#41;
    					break;
    				case 4&#58;
    					document.write&#40;"<meta http-equiv='Refresh' content='0;url=http&#58;//www.ucomics.com/boondocks/' />"&#41;
    					break;
    				default&#58;
    					document.write&#40;"<meta http-equiv='Refresh' content='0;url=http&#58;//slashdot.org/' />"&#41;
    					break;
    			&#125;
    		</script>
    	</head>
    	<body>
    		
    	</body>
    </html>
    This works fine with KHTML. But when I load up any browser with Gecko, it always goes to Slashdot. When I open up Firefox's JavaScript Console, I see:

    Error: uncaught exception: [Exception... "Object cannot be created in this context" code: "9" nsresult: "0x80530009 (NS_ERROR_DOM_NOT_SUPPORTED_ERR)" location: "file:///home/notroot/homepage.xhtml Line: LINE-DOCUMENT.WRITE-IS-ON"]

    I've tried moving the JavaScript into the body element and using a variable in the switch. The encoding is utf8 and the mimetype is application/xhtml+xml. Also when I use W3Schools's little TryIt editor it works with no errors.

  2. #2
    Linux Guru
    Join Date
    Oct 2001
    Location
    Täby, Sweden
    Posts
    7,578
    I think the problem is that you don't have Linuxforums dot org among your homepages. Try adding it, and it will probably work.

    If that, by any odd chance of fate, would not work, it just could be that you're attempting to use document.write in an XML document, which is unallowed. Konqueror is in error to allow that. I don't remember my JavaScript extremely well, but isn't there a property called window.location or similar, which you can assign to in order to point the web browser elsewhere?

  3. #3
    Linux Engineer
    Join Date
    Jan 2005
    Location
    Chicago (USA)
    Posts
    1,028
    Thanks. So just converting it to HTML 4.01 worked:
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http&#58;//www.w3.org/TR/html4/loose.dtd">
    <html>
            <head>
                    <title></title>
                    <meta http-equiv="Content-Type" content="text/html;charset=utf-8">
                    <script type="text/javascript">
                            switch &#40;Math.round&#40;Math.random&#40;&#41;*4&#41;&#41;
                            &#123;
                                    case 0&#58;
                                            document.write&#40;"<meta http-equiv='Refresh' content='0;url=http&#58;//www.megatokyo.com/' />"&#41;
                                            break
                                    case 1&#58;
                                            document.write&#40;"<meta http-equiv='Refresh' content='0;url=http&#58;//www.unitedmedia.com/comics/dilbert/' />"&#41;
                                            break
                                    case 2&#58;
                                            document.write&#40;"<meta http-equiv='Refresh' content='0;url=http&#58;//www.foxtrot.com/' />"&#41;
                                            break
                                    case 3&#58;
                                            document.write&#40;"<meta http-equiv='Refresh' content='0;url=http&#58;//www.ucomics.com/boondocks/' />"&#41;
                                            break
                                    case 4&#58;
                                            document.write&#40;"<meta http-equiv='Refresh' content='0;url=http&#58;//slashdot.org/' />"&#41;
                                            break
                            &#125;
                    </script>
            </head>
            <body>
                    
            </body>
    </html>

  4. $spacer_open
    $spacer_close

Posting Permissions

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