code to check to see if webpage is loaded

I need a piece of code to check to see if a webpage is loaded. If the page is loaded, the script must finish (so I can have it handed off to Quickkeys to do GUI scripting). If the page isn’t loaded, I need the script to keep checking.

I found this section of code from Apple Script for Dummies

tell application "Safari"
	activate
	make new document
	set the URL of document 1 to "http://www.macnn.com/"
	set loaded to false
	repeat until loaded
		try
			set the_text to the source of document 1
			set start_info to the offset of "Apple Stock Quote" in the_text
			if start_info > 0 then
				set loaded to true
			end if
		on error e
			delay 2
		end try
		
	end repeat
end tell
activate
set end_info to start_info + 40
set the_info to characters start_info through end_info of the_text as string
set end_info to the offset of return in the_info
set the_info to characters 1 through end_info of the_info as string
display dialog the_info

I tried to extract just the part I needed, and came up with the following, **Warning!, this script causes my script editor to freeze up!! Becareful before running

tell application "Safari"
	activate
	make new document
	set the URL of document 1 to "http://www.macnn.com/"
	set loaded to false
	repeat until loaded
		try
			set the_text to the source of document 1
			
			if start_text > 0 then
				set loaded to true
			end if
		end try
		
	end repeat
end tell

Here it is outside of the fancy click thing, tell application “Safari”
activate
make new document
set the URL of document 1 to “http://www.macnn.com/
set loaded to false
repeat until loaded
try
set the_text to the source of document 1

		if start_text > 0 then
			set loaded to true
		end if
	end try
	
end repeat

end tell

Anyone have any ideas on how I can get this script to simply check if the page is loaded, and if so, end, and if not, then keep checking?

Thanks!
Tyler

Using a repeat loop to wait for an event is rather cpu-intensive because your script runs all the time and may actually interfere with the speed of downloading the site. Adding a delay to the loop helps, but the delay takes resources too. It is better to run a stay-open application with an ‘on idle’ handler in it that checks every 5 seconds or so. Then your script does nothing for the five-second delay and managing the delay is reasonably efficient.

Idle handlers look like this:

on run
-- here you run the first test and get the size of the source
end run 

on idle
-- here you check the size, compare it to the first one, quit if it's stable, or remember the value for the next round if not
return 3 -- units are seconds
end idle

on quit
continue quit
end quit

Hi,

You can use javascript. Something like this:


tell application "Safari"
	activate
	open location "http://macscripter.net/"
	delay 2
	set the_state to missing value
	repeat until the_state is "complete"
		set the_state to (do JavaScript "document.readyState" in document 1)
	end repeat
end tell
beep 3

You can add a timeout flag to the repeat loop. I’m not sure what happens if the page never completes or if there is a timeout, so you can experiment with these things. Note that the javascript returns “loading” if the page is loading. You might need to adjust the delay also. If the javascript is called before the page BEGINS loading, then the page will never load.

gl,