Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect if a web page has a javascript redirect

I'm using cURL to access a number of different pages. I want an elegant way of checking if the page has a javascript redirect. I could check for presence of a window.location in the body, but because it may be inside a .js file or using a library like jQuery, it seems like any solution wouldn't be perfect. Anyone have any ideas?

like image 436
madphp Avatar asked Nov 26 '12 19:11

madphp


1 Answers

Thanks to Ikstar for pointing out phantomjs I worked out 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
like image 86
sroes Avatar answered Sep 20 '22 17:09

sroes