finspin finspin - 3 years ago 193
Javascript Question

Is it possible to follow Javascript redirect in node.js?

I'm using node.js request library which can follow HTTP redirects but is there a way to follow Javascript redirect such as this?

<html>
<head>
<meta http-equiv="refresh" content="0; /subpage"/>
<script type="text/javascript">
window.location.href = '/subpage/';
</script>
</head>
<body></body>
</html>


The only thing I can think of is to test for the
window.location.href
value in the head of the response and if true, follow it.

Is there some better solution?

Answer Source

Using window.location.href is not the only way to do a javascript redirect, so the hack you suggest would not work in every case.

Also, if the new url is a variable (eg: window.location.url = some_variable) you will not catch it.

The most elegant way to be sure to follow any javascript redirect is to use a webdriver such as Selenium.

The advantage of this solution is that it will follow any html/javascript redirect. The disadvantage is it requires setup, and the execution of the webdriver can be slow.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download