• Quick note - the problem with Youtube videos not embedding on the forum appears to have been fixed, thanks to ZiprHead. If you do still see problems let me know.

Javascript, location.href, and synchronization

TellyKNeasuss

Illuminator
Joined
Oct 4, 2006
Messages
3,781
I've run into a problem when using location.href in Javascript to load a new page into the browser window. The problem is that script execution does not wait for the new page to be loaded, but instead processing continues. This means that if I do something like

location.href = "new_page.html";
var mainDiv = document.getElementById("mainContent");
var paragraph = document.createElement("p");
paragraph.innerHTML = "blah blah blah";
mainDiv.appendChild(paragraph);

to add more verbiage once new_page.html gets loaded, the new verbiage will instead get written to the window while the current page is still loaded and then be over-written along with the rest of the current page once new_page.html actually gets loaded. Any ideas about how to get around this problem?
 
If you use jquery you could wrap that into the .ready() function of the page you are loading.
 
Best to do that in the "onload" handler I would think.

That works, except that I need to pass some data into the function. Using a global variable doesn't seem to work (no idea why). That leaves cookies. Not everyone has cookies turned on. Or am I overlooking something?
 
Do you control all the pages? And how much data are you trying to pass?

If you control the pages, why not pass the data back to the server and let it embed it in the next page?

Otherwise, you could pass it via the query parameters but that has a size limit.

window.name can be used but that data gets passed on to pages from other website, so it should only be used when you have a complete guarantee that nother private is ever going to be passed that way. (it's got other problems, too).
 

Back
Top Bottom