Code to get the currently executing script filename in the browser _javascript tips

Source: Internet
Author: User
Background
A colleague raised the question of how to get the current file name in a JavaScript file that was dynamically inserted in the browser.

In addition to the server output a filename, there should be only the following three ways to get it in the script.

Solution A
The universal solution can only be used for page static scripts tag introduction or single dynamic loading.
Copy Code code as follows:

var scripts = document.getelementsbytagname (' script ');
var filename = scripts[scripts.length-1].src;

To dynamically insert multiple script labels:
Copy Code code as follows:

Loadscript (' B.js?param=1 ')
Loadscript (' a.js?param=2 ')
Loadscript (' b.js?param=3 ')
Loadscript (' a.js?param=4 ')

/* Output
A.js >>> http://localhost:800/io/a.js?param=4
A.js >>> http://localhost:800/io/a.js?param=4
B.js >>> http://localhost:800/io/a.js?param=4
B.js >>> http://localhost:800/io/a.js?param=4
*/

Solution B
Abnormal type, can only work in Firefox:
Copy Code code as follows:

try {
throw new Error ();
}
catch (Exception) {
Console.log (Exception.filename);
}

Solution C
My solution, the operation of the source code:
Copy Code code as follows:

Requirescript (' A.js? ') +date.now (), function (TEXT,SRC) {
Console.log (' text: ', text);
Globaleval (' (function () {\nvar __filename = ' + src + '; \ n ' + text + ' \ n;}) ();');
})

Browser output:
Copy Code code as follows:

<script> (function () {
var __filename = "a.js?1310971812334";
var scripts = document.getelementsbytagname (' script ');
Console.log (' a.js ', ' >>> ', scripts[scripts.length-1].src);
Console.log (__filename);
;}) ();</script>

Benefits: Reliable, cacheable, can be deferred, extensible.
Limit: 1 The variable name is specified as "__filename"; 2) homology policy.
And the idea that this load strategy is used to load popular coffeescript such as:

Copy Code code as follows:

Requirescript (' Script.coffee ', function (TEXT,SRC) {
if (Iscoffeescript (SRC))
Globaleval (Coffeescript.compile (text));
})

Link

Cross-origin Resource Sharing

Passing JavaScript arguments via the src attribute

Coffeescript

View or download

https://gist.github.com/1088730

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.