0

I have a script that takes urls from an array, then puts them into an iframe one after another:

<script type="text/javascript">
    $(document).ready(function () {
        var array = ['http://www.example1.come', 'http://www.example2.com', 'http://www.example3.com'];
        var beforeLoad = (new Date()).getTime();
        var loadTimes = [];
        $('#1').on('load', function () {
            loadTimes.push((new Date()).getTime());
            $('#1').attr('src', array.pop());
            if (array.length === 0) {
                $.each(loadTimes, function (index, value) {
                    $("#loadingtime" + index).html(value - beforeLoad);
                });
            }
        }).attr('src', array.pop());
    });
</script>

My problem is, if i put url like "google.com", or "youtube.com" - it wont load thus making the other sources stuck and not load too. I am getting this error on chrome console mode if i try to put google.com as a source into iframe : Refused to display 'http://www.google.com/' in a frame because it set 'X-Frame-Options' to 'SAMEORIGIN'.

How can i make it so if theres such an url in my url list that gives this error and wont load, it would skip that url and load the next one instead? can i put a timeout or something?

Colin Brock
  • 20,219
  • 9
  • 43
  • 61
user1894929
  • 199
  • 5
  • 19

2 Answers2

1

Unfortunately, I think you can't get rid of this error, because it's a security feature preventing clickjacking.

See MDN for more information. According to it, X-Frame-Options header is now supported across all main desktop browsers.

There are several ideas for you to try:

  1. Your code seems to work in Firefox (Firefox Nightly, 2013-01-06), so if changing a browser is an option, you might try.
  2. Using Ajax, you can check website's header before changing iframe.src attribute. If one of the headers is "X-Frame-Options", you can choose to continue to another URL.
    Of course using this method you will stumble upon CORS, but this feature can be disabled both in Firefox and in Chrome.
    Obviously in your case loading a webpage via Ajax and then measuring it's load time seems like a not so good idea. However, you may try loading sub-page (like "http://youtube.com/SOMETHING") instead of root one, to avoid caching the thing you're measuring.
Community
  • 1
  • 1
kamituel
  • 30,600
  • 3
  • 71
  • 91
  • Thanks, i just tested this on firefox and indeed it doesnt brake my other sources and show all results. Seems like using firefox or finding another method to get loading time are my only options – user1894929 Feb 28 '13 at 07:44
0

Technically you could try to download the page on your web server and then show that file.

Legally you aren't allowed to do that.

yunzen
  • 30,001
  • 10
  • 64
  • 93
  • I think what user1894929 meant was to abort (skip) loading a webpage which responds with 'X-Frame-Options' header. – kamituel Feb 28 '13 at 07:18