Jerry2
Jerry2

Reputation: 3085

Jquery/javascript detection if iframe can be loaded

I have iframes with OpenX ads I server from another server/domain.

When something happens to this ad server the main server doesn't load whole contents because the domain that openx loads in iframe is not responding. I always thought that iframe works independently from the main site but it doesn't if the domain doesn't answer at all...

Anyway can main site detect somehow that a url in iframe is not responding and skips loading iframe and show the rest of the site?

Upvotes: 0

Views: 937

Answers (5)

Jerry2
Jerry2

Reputation: 3085

How about if I don't have iframe just javascript originating from different domain? If the domain is not responding javascript holds the page back not to load. Is there a way to prevent that?

Upvotes: 0

Marko
Marko

Reputation: 72230

How about loading the iFrame once the website is loaded? It's pretty easy to do this using jQuery (or even plain javascript using the window.load event).

So rather than wanting to 'detect' whether the iFrame has loaded, you can load it AFTER the rest of the website has completed loading. (sorry for excessive use of word 'load')

In jQuery, you can simply attach the url to the iFrame on the document.ready event.

A blank iFrame

<iframe id="iframe-ad" width="200"></iframe>

Simple jQuery to load the URL on document.ready

<script type="text/javascript">
    $(document).ready(function() {
        $("#iframe-ad").attr("src", "http://www.google.com");
    });
</script>

Upvotes: 1

Mike Ruhlin
Mike Ruhlin

Reputation: 3556

Might not make for the best experience, but you can make a local redirect file. something like:

<iframe src="http://www.mydomain.com/redir?url=http://www.theirdomain.com/ads"/>

then the redir page just returns

<script>
    location.href = "${url}";
</script>

That way as long as your server is responding, everything else will continue as normal while the iframe redirects?

Upvotes: 0

eriksv88
eriksv88

Reputation: 3526

It is not possible, because of Same origin policy, there are some "gaps" in some browsers. But this is not going to recommend!

Upvotes: 0

Hersheezy
Hersheezy

Reputation: 1234

Unfortunately there is no easy way to do this unless they are served from the same domain. I know there is a way to get let the javascript inside the iframe perform some actions on the parent document it is contained in, but I am not really sure how...

Upvotes: 0

Related Questions