Search code examples
javascriptjqueryajaxgoogle-chromehref

How to get the status code of the page before it is loaded


There is a javascript line (you can try it in the browser console)

window.location.href='http://example.com'

that will push you to http://example.com

In the Browser(Google Chrome)-> Developer Tools-> Network section you may see the Status is 200 for it.

The question is:

how to get the status code 200/404/302 right BEFORE executing

window.location.href='http://example.com'

Thank you.

P.S. jQuery is OK for using.


Solution

  • The only way to get the status code would be to make the request before you navigate there. That means make an Ajax call to the resource and check the status. Only downside to this is the Same Origin Policy so the sites need to be in the same domain or they have to have CORS enabled for your resource.