How to not load javascript files when javascript is disabled

[SOLVED]

I use to create pages, usually without javascript, and only then add javascipt functions. That way, I can set all the divs with tools that require javascript with the display: none, and then in the first line on javascript to make it visible:

<script type="text/javascript"> $(document).ready(function(){ $('#jsdivs').show(); }); </script> 

In doing so, I avoid people who, for whatever reason, have disabled their browsers.

Following this spirit, I would like to find a way not to load javascript by default, and then if javascript is enabled, there are instructions for downloading javascript files.

I am currently loading jQuery like this (for caching reasons, many people may already cache):

 <script src="http://code.jquery.com/jquery-1.4.4.js"></script> 

And another file with all other javascript is minimized. Everything is at the end, immediately before the HTML tag tag.

I have seen many opinions here and here , but none of them will help me with this. First, I would like to know if this is good. Thank you very much.

UPDATE:

  • @ Lèse majesté showed me an old discussion about how browsers don't download javascript files when they are disabled . Since I did not think the information was convincing enough , I conducted several tests by disabling javascript in Firefox through the “Web developer” plugin, and YSlow (another FF plugin for Firebug) continues to show all javascript information, not to mention that "Live Headers" (another fantastic FF plugin that shows real-time headers) also showed that the _utmc cookie was present in the request (via the Google Analytics script).

  • @Marcel Korpel made a very good comment (albeit a snob), which paved the way for a real solution. he said that Google Analytics uses the "method to dynamically enable ga.js. The closing tag (and the immediately opening tag) is necessary for the browser to update the DOM tree and actually load and execute the added script."

  • With information about it, I could find this PHP code (I had to put the PHP code in Pastie.org because Stackoverflow was messing with it):

    http://pastie.org/1487432

What prints:

 <script language="javascript" type="text/javascript"> document.write('\<script src\="js\/minified\.js" type\="text\/javascript"\>\<\/script\>'); </script> 

This time, YSlow and Live Headers say nothing about the javascript file.

+4
source share
2 answers

Most browsers do not download associated .js files if JavaScript is disabled. See this question:

Does the browser load JS files if the user has disabled JS?

+3
source

You can add javascript files dynamically, but the content will not be evaluated and will not be available to you (since javascript is downloaded and parsed once), so this approach will not work , as you can see here .

You can also use server-side scripts and not write script tags if you can pass this information (javascript disabled) back to the server by query string parameter, form field or whatever you like.

However, consider the complexity and whether you save anything.

Honestly, I don’t know how browsers behave with respect to these files, if javascript is turned off - it is reasonable to assume (although you should always check) that if javascript is turned off, javascript files will not be requested either.

Update:

As you can see here , most browsers do not download js files if javascript is disabled.

+2
source

Source: https://habr.com/ru/post/1336582/


All Articles