Test url availability with javascript

左心房为你撑大大i 提交于 2019-12-14 03:48:40

问题


Is it possible through jQuery (or plain javascript) to test if a webpage on another domain is available? I tried getting the response headers with an ajax-call but I get an error no matter what site outside my own domain I test.

So do I really need a proxy script on my server or would I be able to skip that request?


回答1:


Yes, you need to use a proxy script on your server. JavaScript cannot be used in a browser to request resources across domains, as per the same-origin policy.




回答2:


Is it possible through jQuery (or plain javascript) to test if a webpage on another domain is available?

Due to same origin policy restriction you need a proxy/bridge on your server unless the remote server implements JSONP which obviously we cannot assume for the general case.




回答3:


You can create an <img> tag that points to an existing image on the external domain.

If the onerror event fires, image, and perhaps the entire site, is down.

If it fires after 5 seconds or so, it probably timed out, so the entire site is likely to be down.



来源:https://stackoverflow.com/questions/5573360/test-url-availability-with-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!