Need some Help? Have a tip to share?

Email me at MacTipper@gmail.com, or, leave a comment!

Learn How to Win Free Stuff Online


10/1/09

Run AppleScript When Webpage is Done Loading

If you're writing a script that relies on a webpage being fully loaded, there's a simple little AppleScript snippet you can use to determine if the page is loaded or not. The code is:

tell application "Safari"
delay 0.5
repeat until ((do JavaScript "document.readyState" in document 1) is "complete")
delay 0.25
end repeat

-- Action to perform once page is loaded.

end tell


The most important part of this script is the 'do javascript "document.readyState" ' section. This will return the current load status of the page.

While I haven't tested this with earlier versions of Safari, I have found that this works with Safari 4 in both Leopard and Snow Leopard.

0 comments:

Post a Comment

Subscribe Via Email!

Enter your email address:

Delivered by FeedBurner