user1894929
user1894929

Reputation: 199

Detecting if iframe source have 'same origine' protection

I got a script that checks loading times of multiple urls that are stored in a txt file:

$.get("imones.txt", function (data) {
        var array = data.split(/\r\n|\r|\n/) 
        var beforeLoad = (new Date()).getTime();    
        var loadTimes = [];
            var beforeTimes = [];               
        $('#frame_id').on('load', function () {                                 
            beforeTimes.push(beforeLoad);
            loadTimes.push((new Date()).getTime()); 
            $('#frame_id').attr('src', array.pop());
                $.each(loadTimes, function (index, value) { 
                    var result = (value - beforeTimes[index]) / 1000;
                        if (result < 0) {
                            result = result * (-1);
                        }   
                    $("#loadingtime" + index).html(result);
                    beforeLoad = value;
                });
        }).attr('src', array.pop());
    });

If i got a url in my list with 'same origine' protection (google for example), it will crash my script and wont check loading values of other urls in the list. I was thinking i could see if a specific url has 'same origine' error, i would change that urls loading result "not available" or something:

     if(url has same origine){
result = "loading time not available";
} else {
//do my calculations

so yeah, can i somehow see if a specific url will give me 'same origine' error before i do my calculations for loading time?

Upvotes: 0

Views: 58

Answers (1)

rekire
rekire

Reputation: 47975

If you cannot read the src attribute of the iframe you have some content which is not from the same domain.

<iframe src="./" height="300" width="300" name="example"></iframe>
<input type="button" onclick="alert(document.getElementsByName('example')[0].src);" value="test" />
<a href="http://www.google.com" target="example">click me</a>

The code above should give you a idea. However this code does not work on jsfiddle.

Upvotes: 1

Related Questions