admin管理员组文章数量:1516870
In Internet Explorer <9, at least, linking to a blocked resource (due to firewalls, WebSense, etc.) will return an error. For instance, if Facebook is blocked, linking to the Facebook SDK JavaScript file will result in an error in Internet Explorer.
Is it possible to test if a website is blocked in JavaScript, before linking to the external JavaScript file, for the following purposes:
- Prevent a JavaScript error from showing up in Internet Explorer
- If code depends on the external file, it can be prevented from running, to cause further JavaScript errors relating to the lack of the external library
In Internet Explorer <9, at least, linking to a blocked resource (due to firewalls, WebSense, etc.) will return an error. For instance, if Facebook is blocked, linking to the Facebook SDK JavaScript file will result in an error in Internet Explorer.
Is it possible to test if a website is blocked in JavaScript, before linking to the external JavaScript file, for the following purposes:
- Prevent a JavaScript error from showing up in Internet Explorer
- If code depends on the external file, it can be prevented from running, to cause further JavaScript errors relating to the lack of the external library
- Similar question: stackoverflow./questions/2027849/… – LoveAndCoding Commented Nov 21, 2011 at 6:59
- Two of the other answers in the question @Ktash mentions might also be useful to others: stackoverflow./questions/2027849/… uses window.onerror and stackoverflow./questions/2027849/… uses onload. – ToolmakerSteve Commented Aug 12, 2014 at 0:06
2 Answers
Reset to default 3Looking at the facebook SDK, looks like it gets injected asynchronously by inserting a script element into the head. This means all you have to do is create the callback handler and if facebook is blocked then that will never be called. You shouldn't get any browser script error. So you should be able to do something like:
<div id="fb-root"></div>
<script>
var FACEBOOK_BLOCKED = true;
window.fbAsyncInit = function() {
FACEBOOK_BLOCKED = false;
FB.init({
appId : 'YOUR_APP_ID', // App ID
channelURL : '//WWW.YOUR_DOMAIN.COM/channel.html', // Channel File
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
oauth : true, // enable OAuth 2.0
xfbml : true // parse XFBML
});
// Additional initialization code here
};
// Load the SDK Asynchronously
(function(d){
var js, id = 'facebook-jssdk'; if (d.getElementById(id)) {return;}
js = d.createElement('script'); js.id = id; js.async = true;
js.src = "//connect.facebook/en_US/all.js";
d.getElementsByTagName('head')[0].appendChild(js);
}(document));
</script>
For a more general use case, a similar approach will work with any script that you can call using a JSON-P approach. This, of course, requires that the server takes a callback function as a parameter (or automatically calls a pre-named function):
// Load some SDK Asynchronously
(function(d){
var js = d.createElement('script');
js.src = "http://www.example.?callback=myfunc";
d.getElementsByTagName('head')[0].appendChild(js);
}(document));
var SCRIPT_LOADED = false;
var myfunc = function() {
SCRIPT_LOADED = true;
}
UPDATE
Just came across this method, you might want to give it a shot:
function loadScript(sScriptSrc, oCallback) {
var oHead = document.getElementById('head')[0];
var oScript = document.createElement('script');
oScript.type = 'text/javascript';
oScript.src = sScriptSrc;
// most browsers
oScript.onload = oCallback;
// IE 6 & 7
oScript.onreadystatechange = function() {
if (this.readyState == 'plete') {
oCallback();
}
}
oHead.appendChild(oScript);
}
you can use jquery's getScript function, it has a success function that is only executed if the script has been loaded , check it out here : http://api.jquery./jQuery.getScript/
本文标签:
版权声明:本文标题:internet explorer - Detect with JavaScript that a script is blocked (by web filtering, firewall, etc.) - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://www.betaflare.com/web/1745225841a2648603.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


发表评论