finspin
finspin

Reputation: 4061

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?

Upvotes: 0

Views: 724

Answers (1)

Aur&#233;lien Gasser
Aur&#233;lien Gasser

Reputation: 3120

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.

Upvotes: 4

Related Questions