Detect if webpage has javascript redirect

I use cURL to access multiple pages. I want an elegant way to check if a page has javascript redirection. I could check for window.location in the body, but since it could be inside the .js file or using the jQuery library, it looks like any solution would not be perfect. Does anyone have any idea?

+7
source share
4 answers

Thanks to Ikstar for pointing phantomjs I developed the following example:

test.js

 var page = require('webpage').create(); var testUrls = [ "http://www.google.nl", "http://www.example.com" ]; function testNextUrl() { var testUrl = testUrls.shift(); page.open(testUrl, function() { var hasRedirect = page.url.indexOf(testUrl) !== 0; console.log(testUrl + ": " + hasRedirect.toString()); if (testUrls.length) { testNextUrl(); } else { phantom.exit(); } }); } testNextUrl(); 

Result:

 D:\Tools\phantomjs-1.7.0-windows>phantomjs test.js http://www.google.nl: false http://www.example.com: true 
+2
source

You cannot do this just by parsing the script. Only execution will show you the true flow of the JS page.

One way to imitate execution is to have different levels of code that has redirection. The topmost one will be under the <script> , and any redirects here will be a direct redirect. If any redirects are inside functions, you need to track the structure of the program and make assumptions.

0
source

Depending on the purpose of using Curl and the actual need to redirect to the page. To perform the desired viewing, you can include a headless structure, for example PhantomJS (http://phantomjs.org/). You will be able to see if redirects occur, as well as track the execution of any other javascript on the page.

0
source

It is not possible to detect the presence of redirects simply by analyzing the source code of the web page.

An unsolvable stopping problem can be encoded in JavaScript. The algorithm may stop, which will lead to the generation of redirection, or work forever. Since we do not know whether the code will stop, it is also impossible to determine whether the redirection will be performed or not.

0
source

All Articles