Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Is synchronous javascript possible?

Status
Not open for further replies.

jsteph

Technical User
Oct 24, 2002
2,562
US
I have an asp page, on which I have a button. The onclick calls a js function that reads some screen data, then sets the .location an iframe to another asp page. This other asp page has code which updates a sql server database.

Since the main page isn't being reloaded, I want to show a status immediately after the iframe's new .location code runs. What seems to be happening is that the javascript code is asynchronous, so that after the call setting the new iframe.location, my call to check the output (the iframe source writes a new textbox in the iframe with status info in it), returns nothing.

How do I (without randomly timed delay loops) wait for the iframe to finish it's update, write the status to the textbox on the iframe and then read it--all within the same javascript function, ie not having to have the user hit another button to see the results?

I understand that when the script started, the lay of the land was one way, and it changed while the script was running. But I need to know how I can 'refresh' the objects javascript sees on the page without exiting the code.
Thanks,
--Jim
 
You probably won't be able to do it all in the same function, but since you can pass arguments from one function to another fairly effortlessly, I don't see that posing too much of a problem.

This thread thread215-901861 has some code that polls the iframe at a regular interval to check if it has loaded the new url completely. It's not exactly what you're asking for, but it might steer you in the right direction.

Never be afraid to share your dreams with the world.
There's nothing the world loves more than the taste of really sweet dreams.
 
In IE only you can check readyState in a loop:
Code:
while (myIFrame.document.readyState!= "complete");
// iframe loaded, proceed
The drawback of this "synchronous" method is that browser will be literally frozen while iframe is loading.

Wanna do it asynchronously, use body onload event in IFrame.document for callback.
 
dwarfthrower, vongrunt,
I haven't tried the readystate property, but I'm not sure that will do it. What I have tried (and what I don't want to use) is the loop of an arbitrary time. So I see the advantage to readystate--the wait won't be arbitrary.

But the main thing I'm looking for is to have the object values refreshed. This is what doesn't seem to be happening. When I do the wait loop (and I loop very well long enough for the iframe to load), and then read the iframe, the script is reading the old values.

I'm 100% positive that the iframe was loaded--I even set the wait loop long enough for me to check the database using an odbc linked table in MS Access and the data was indeed there. IE even gives me a dialog saying that there is script that is causing the browser to take a long time and do I want to cancel, etc, etc. But the jscript still reads the old iframe values.

I will try the readystate loop, but can you think of anything I might be missing? For example, some sort of Refresh method--not reloading the page to the gui--but a similar concept that would reload object values to the javascript environment?

That is what seems to be the crux of it--the document object seems to be a 'snapshot' taken at the time the script started rather than a dynamic object, so I'm thinking there must be some way around this.
--Jim
 
dwarfthrower,
I looked at that thread, and tried the code. I get
"Access is denied"
at the line trying to reference the innerText of the iframe.

Do you know why this is? Thanks,
--Jim
 
>> "Access is denied" <<

Usually you'll get this error if you are trying to script across domains. Check the urls of the calling page and the IFRAME page. The browser security model will throw a tantrum even if both domains map back to the same physical box (like if the calling page has the domain name and the IFRAME has the IP address or an alternate hostname).

Never be afraid to share your dreams with the world.
There's nothing the world loves more than the taste of really sweet dreams.
 
What I don't see is--how can the code in the example work?

It's selecting a file, with a url that's something like "C:\whatever.txt", but the page on which that iframe exists will be some url like
It sounds like some sort of callback is what's needed--I've done this in VB by creating a hook into the windows message loop, so maybe something exists in the virtual machine that's similar.

I looked at the link from lrnmore, and while it doesn't seem elegant, it might do close to what I want.
--Jim
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top