I've been making a webpage that uses a sort of... external survey taking. I was wonder if when using the window.open method, can you tell if your new window had a 404 or the page timedout trying to load?
Here is what I have so far:
child = window.open(href, 'window');
setTimeout(function() {
// Do some backend processing if child window is still open and no 404 / timeout
}, 10000);
I've been making a webpage that uses a sort of... external survey taking. I was wonder if when using the window.open method, can you tell if your new window had a 404 or the page timedout trying to load?
Here is what I have so far:
child = window.open(href, 'window');
setTimeout(function() {
// Do some backend processing if child window is still open and no 404 / timeout
}, 10000);
Share
Improve this question
edited Oct 15, 2017 at 11:53
Donald Duck is with Ukraine
8,93223 gold badges79 silver badges103 bronze badges
asked Dec 25, 2011 at 20:41
Wilson212Wilson212
5636 silver badges18 bronze badges
2
- is the popup within the same domain and do you have control over its content? – sled Commented Dec 25, 2011 at 20:51
- Nope, the new window will be an external URL, and no control over the content – Wilson212 Commented Dec 25, 2011 at 20:51
2 Answers
Reset to default 2That's a pretty hard thing to do. You have to deal with security issues (cross domain access is forbidden in JS). But I think this function can do the trick:
child = window.open(href, 'window');
setTimeout(function() {
try {
var foo = child.location;
}
catch (err) {
// It has loaded. Manage that.
return;
}
// Still loading. Code here.
}
}, 10000);
Explanation: try to access the location
element of the child window. If the window has loaded, trying to access it will throw an exception (cross domain JS access is forbidden) so you manage that. But if you can access the location that will probably mean it's still loading.
The main problem is that, with this code, you can only distinguish when it's loading or not, so I suppose you could only check for timeout. I think that checking 404 error is almost impossible as the JS security restrictions don't allow you to get the location or content of an external page.
Have a look at cross-domain ajax requests:
http://james.padolsey./javascript/cross-domain-requests-with-jquery/
There are a few ways you could employ it depending on how the remote site is structured, but at the very least you could check that the page exists before launching the popup (though this won't guarantee that it loads in the popup).
Alternately, have the popup load a local script (php, etc) which first does a cUrl or other remote check... have it check the headers returned, and then - if all is good - it sends headers to forward to the remote site. If an error code is returned, then you have the popup window load a local html/js page that has a script that can speak to the opener window to let it know that it failed.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745427842a4627260.html
评论列表(0条)