简体   繁体   中英

How to check Javascripts are disabled by proxy

I do not know weather there is any way to solve this or not, but I'm just asking it because I believe SO is a place of genius people. Anyways, we all know that if we use <noscript></noscript> tag within a HTML document and if any user who are viewing this page and have JavaScript disabled from their browser will see the no script message. But what if JavaScript has been disabled by proxy? Many wifi networks needs to manually input some proxy to use internet and many of them disabled JS on that proxy from the proxy server. In this case if anybody visit the same page, the page will see that JavaScript has been enabled from browser, but disabled from proxy. If there any way to check weather JavaScript has been disabled by any proxy(if using) and showing alert message for this? Also I will be glad if anybody can say that how to implement it with Wordpress and also without wordpress. :)

Thanks.

You can show the message by default and then remove or hide it with JavaScript, eg:

<div id="jsalert">JavaScript is disabled in your environment.</div>
<script>
(function() {
    var elm = document.getElementById("jsalert");
    elm.parentNode.removeChild(elm);
})();
</script>
<!-- Continue your content here -->

If script tags have been stripped by a proxy (which I'm fairly certain is very unusual ; at least, I've never seen it), then of course the script won't be there to be run, and the div will show. If the script is present, it will remove the div .

By following the div with the script immediately (which is perfectly fine , no need for "DOM ready" stuff that will just delay things), the odds of the div "flashing" briefly on the page in the common case (where JavaScript is enabled and not stripped out) are very low. Not zero, but low.

If you believe the proxy doesn't strip out script tags but instead just blocks the downloads of JavaScript files (which would be dumb), you can change the above to use a JavaScript file, but beware that by doing that you either hold up the rendering of your page (if you use <script src="..."> ) or you increase (dramatically) the odds of the div "flashing" on the page briefly (if you load the script asynchronously).

This is just a specific use-case for a general practice called "progressive enhancement" (or sometimes "graceful degradation," but most people prefer the first). That's where you ensure that the page is presented correctly and usefully in the case where JavaScript is not available, and then use JavaScript to add behaviors to the page if JavaScript is enabled. In this case, the "useful" thing you're doing is saying that JavaScript isn't running for some reason, so it's a slightly different thing, but it's the same principle.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM