Sorry, I was unclear and hadn’t really thought the question through.
Here is the whole context:
on finishedLoading(URLtoLoad)
tell application "Safari"
set URL of document 1 to "about:blank"
repeat until document 1's source = ""
delay 0.5
end repeat
set URL of document 1 to URLtoLoad
repeat while document 1's source = ""
log "source3:" & document 1's source & ":"
delay 0.5
end repeat
The issue is not the loop - it is the fact that setting the URL of the document doesn’t have any effect. As you can see, I want to be sure that the window is empty before I set the URL to a regular web page. But for some reason that line most of the time doesn’t have any effect (it does work sometimes).