Nick Sick
Nick Sick

Reputation: 149

window.location does not redirect on empty page?

I'm generating a page to fetch some variables and quickly redirect with them. But the redirect is not executed. This is the complete page rendered in my browser:

<HTML><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN" "http://www.w3.org/TR/html4/frameset.dtd">
<head><LINK REL='stylesheet' TYPE='text/css' HREF='css/style.css' NAME='style'>
<meta http-equiv='Content-Type' content='text/html; charset=utf-8'>
<meta name='viewport' content='width=device-width, minimum-scale=1.0, maximum-scale=1.0' />
<title></title>
</head>

<script>

function get_init_values()
{
    //document.write("Hi");

    $clientWidth = document.body.clientWidth;
    $clientHeight =  document.body.clientHeight;

    window.location="?page=init&clientwidth="+$clientWidth+"&clientheight="+$clientHeight;
    return false;

}</script>
<script>get_init_values();</script>
</HTML>

but when i uncomment the document.write statement the redirect executes as desired. What fundamental understanding am I missing here? I have tested this with IE, firefox and IPhone Safari with the same results.

Edit! The browser i just blank. Removed the return in : return get_init_values(); which where just added for testing. No difference.

Upvotes: 2

Views: 696

Answers (1)

leapin_leprechaun
leapin_leprechaun

Reputation: 605

Is the code placed at the top of the page or the bottom? I'm thinking there's nothing rendered in the doc before it runs so it hits null and doesn't work. If you put it to the bottom of the page or had a node in the document to start with I think it should work then.

  • Edit: You need a body element. Then put the script tags just inside the end </body> tag. The reason it doesn't work at the moment is because there is nothing within the document to get the ClientWidth/height from and when Null comes back for that it stops the script executing because there is an error "Uncaught TypeError: Cannot read property 'clientWidth' of null"

Upvotes: 2

Related Questions