We help IT Professionals succeed at work.
Get Started

Preventing JS script element from hanging

monoceros
monoceros asked
on
121 Views
Last Modified: 2015-03-03
Hi,
Recently hit an issue on a mobile e-commerce site where a very slow response from 3rd party site caused the execution of our JS to be delayed for long enough that the site appeared to be down (we load product information using JS/ajax).

The basic flow is:

in head we load our site JS
in the body we load a script for click tracking using (all identifying parameters removed to protect the 3rd party:
	<script type='text/javascript'>
			document.write('<script src="http://the.trackingcompany_X.com/x/y?"></script>');
	</script>

Open in new window


This then returns with further JS that includes
var part_0 = '';part_0 += "<"+"script type=\"text/javascript\" src=\"http://tracking.company_Y.com/releases/collector.js\"><"+"/script>\n";part_0 += "<"+"script type=\"text/javascript\">\n";part_0 += "window.company_Y.collectorController.recordEngagementEvent(\'menu-xxxxx-page\', 0);\n";part_0 += "<"+"/script>\n";document.write(part_0);

Open in new window


The issue is that http://tracking.company_Y.com/releases/collector.js was not responding in a timely manner and so the rest of the page JS execution is delayed and the site seemed to be down.

So we are looking to add some defensive programming into the site. I would like to wrap the outer script with some defensive programming.

Would something like (obviously without the alerts)
<script type="text/javascript">
var jsLoaded = false;
setTimeout("callback()", 2000);
function callback() {
    if (!jsLoaded) {
        alert("Javascript not loaded after 2 seconds!");
    } else {
        alert('Loaded');
    }
}
</script>
<script type="text/javascript" id="foo" src="url.js" onload="jsLoaded=true"></script>

Open in new window


... be well supported across mobile browsers?

Is there a better way?
Comment
Watch Question
Software Architect
CERTIFIED EXPERT
Commented:
This problem has been solved!
Unlock 1 Answer and 2 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE