What is the hash (#) after the .js file?

what is the hash (#) value here, how does it relate to the .js file:

<script src="foo.js#bar=1"></script> 
+7
source share
4 answers

The hash after the script is used by the built-in script for configuration. For example, see the above example (facebook):

 1. window.setTimeout(function () { 2. var a = /(connect.facebook.net|facebook.com\/assets.php).*?#(.*)/; 3. FB.Array.forEach(document.getElementsByTagName('script'), function (d) { 4. if (d.src) { 5. var b = a.exec(d.src); //RegExp.exec on the SRC attribute 6. if (b) { 7. var c = FB.QS.decode(b[2]); //Gets the information at the hash 8. ... 

In the script, each <script> line 3 tag is checked for occurrences of line 5 of line hash 2 for an attribute. Then, if a hash exists, line 6 hashdates, line 7 is retrieved and the function continues.

+4
source

I am not doing anything in terms of loading the script. I assume that the script itself searches for its own script tag and selects the piece after the hash (bar = 1) and uses it to somehow customize its behavior. To do this, they will probably have to iterate over the script tags and match the src attribute.

+3
source

It is probably used in a .js referenced file reading the source URL and extracting the parameter (e.g. using window.location ) and parsing what comes after # ).

+2
source

The part after the hash in the URL is known as the fragment identifier . If present, it indicates a part or position in a shared resource or document. When used with HTTP, it usually indicates the section or location on the page, and the browser can scroll to display that part of the page.

In relation to the JavaScript file, the author of the program most likely uses it as a method to pass arguments to the file. However, this method should not be used. URLs may contain query strings that serve the same purpose.

However, it is never recommended that you insert arguments into the URL of the JavaScript file, because for every other set of parameters the URL is cached again, which is a waste of memory. Instead, it's best to set the query string to the URL of the HTML page that contains the script itself. This is because JavaScript has a built-in property to access the query string on the web page: location.search . You can learn more about this here .

0
source

All Articles