Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

detect and suppress errors loading javascript file

Tags:

javascript

I want to source a javascript file from facebook http://connect.facebook.net/en_US/all.js

The organization I work for has a firewall that blocks access to Facebook, it just goes to an html page that says "Access Denied blah blah blah"

I want to be able to put a javascript src tag <script src="http://... "> </script> and detect and suppress the warnings when the browser tries to evaluate the html as javascript.

Anyone know how?

like image 291
Quinn Wilson Avatar asked Sep 03 '10 19:09

Quinn Wilson


2 Answers

Looks like jQuery.getScript is what you need as was mentioned. Or you can manually execute:

$.ajax({
  url: url,
  dataType: 'script',
  success: function(){document.write('<script src="http://... "> </script>');}
});

And append your html on successful load with the <script></script> tag.

like image 65
dmitko Avatar answered Nov 07 '22 11:11

dmitko


With the standard <script> tag, not possible. There's nothing really running at the time when the script's src is hit and content downloaded, so you can't wrap that in a try/catch block. There's some tips here on how to dynamically load scripts. Maybe the browsers will add some stuff to the DOM element created there which you can check for.

like image 21
Marc B Avatar answered Nov 07 '22 10:11

Marc B