Check for Redirect url is correct or not?
I am calling window.location.href = "some url";
I want to check whether that url is opening or not. Means if the url is wrong how do check it in javascript so that I开发者_StackOverflow中文版 can redirect it to another url.
Thanks.
You can't, not really anyway. Once you change the location, the browser will unload your page and begin to load the new one, there's no way to find out where the browser is going or if it failed to reach the target page.
If the URL is on the same domain you could check to see if it returns a status 200 with an AJAX request before setting the location, but if it's on another domain then you're out of luck due to the same origin policy.
You need to use a server side component for this to work.
Expose an ajax service that will perform a HEAD on the given url and return a status. Mind that this service needs to be protected to avoid being used for DDOS.
精彩评论