We help IT Professionals succeed at work.

How do I provide a javascript or iframe timeout when external server is unavailable?

Medium Priority
919 Views
Last Modified: 2013-11-19
I have a website that provides widgets that people put on their blog sidebars.  They do so by using an external script tag:

<script type='text/javascript' source='http://mysite.com/widget.aspx'></script>

Occasionally our server will go down which will cause all our users blogs to timeout for about 30 seconds while the browser tries to load the external script.  Obviously our users don't appreciate this.

Note we can't use the script "defer" attribute because we are writing content to the document.

I have researched the problem and found mention of people using iframes with javascript settimeout helper functions to accomplish this.   What would this look like?  Should I just AJAX (xmlhttprequest) to do this?
Comment
Watch Question

IT Expert
CERTIFIED EXPERT
Top Expert 2009
Commented:
Perhaps
<div id="contentContainer"></div>
<script type='text/javascript' source='http://mysite.com/widget.aspx'></script>
<script type="text/javascript">
var img = new Image()
img.src='http://www.mysite.com/logo.gif?'+Math.random();
img.onload=function() {
 document.getElementById('contentContainer').innerHTML=stuffFromYourOtherScript()
}
</script>

and not use document.write

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

Commented:
The solution above probably works - but I went ahead and used an AJAX call instead.  When the page has finished loading I do a JSONP AJAX call to our servers.  If the servers are down, the AJAX call just fails and no one gets mad.
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.