Click to See Complete Forum and Search --> : Halting execution of page


bhollin
09-30-2003, 06:59 PM
I have some conditional code in the HEAD that redirects the page if a condition is met. (BTW, location.href and document.location seem equivalent - are they exactly?)

The problem is that while the redirect is happening (could be slow on some PCs or networks), the HTML in the BODY is interpreted and the original page starts to load. So for a second or two the page that I don't want to display is shown before the target URL is displayed.

I would like to add something to my HEAD script that in addition to triggering the location.href redirect also halts execution of the rest of the page. What's the best way to do this?

Thanks.

Gollum
10-01-2003, 03:22 AM
Not sure if there is a way to halt execution per se, and even if there was, I would expect it to be browser specific.
Here's a better idea...

<html>
<head>
<script>
var bNavigateToOtherPage = DecideWhetherToNavigate();

if ( bNavigateToOtherPage )
{
window.location = "http://somewhereelse.com";
}
</script>
</head>
<body>
<script>if ( bNavigateToOtherPage ) document.write(<div style="visibility:hidden">);</script>
If navigating, this text should not be seen
<script>if ( bNavigateToOtherPage ) document.write(</div">);</script>
</body>
</html>

This won't actually stop the document from executing, but it should hide the page contents until the next page comes.

bhollin
10-01-2003, 05:27 PM
Thanks - very good idea.

but shouldn't the

</div">

in the last script be

</div> ?

The redirect works fine, but the visibility:hidden isn't taking effect. I assume you bracket everything inside the BODY with the
document.write(<div style="visibility:hidden"> );
and
document.write(<div> ); ?

Here's a simple example (minus the condition check):

<html>
<head>
</head>
<body>
<script>document.write(<div style="visibility:hidden"> );</script>
<p>This is a bunch of text</p>
<p>and an image:</p>
<p><img src="bird_pic.gif" alt="" height="117" width="93" border="0"></p>
<script>document.write(</div> );</script>
</body>
</html>

Presumably everything within <p> tags should be hidden, but it's displayed. What have I got wrong?