Home Articles FAQs XREF Games Software Instant Books BBS About FOLDOC RFCs Feedback Sitemap
irt.Org
#

Q350 Why does the code continue to be interpreted even after I've requested that another document be loaded?

You are here: irt.org | FAQ | JavaScript | General | Q350 [ previous next ]

If say you have code like:

if (x == y) {
    location.href = 'http://www.irt.org/';
}
document.write('<P>blah blah');

Then what happens is that JavaScript changes the href property of a document object to a text string, the *browser* knows that this means "go get http://www.irt.org/' - so it does, but JavaScript doesn't know this, it just knows that it has changed the value of an objects property. The JavaScript code continues to be interpreted.

To stop this from occurring do not have any JavaScript code after the request to load another document:

if (x == y) {
    location.href = 'http://www.irt.org/';
}
else {
    document.write('<P>blah blah');
}

If however you need both to occur, i.e. load another document but also output text to the current document then swap the order around:

document.write('<P>blah blah');
if (x == y) {
    location.href = 'http://www.irt.org/';
}

©2018 Martin Webb