The _ FILE _ constant like PHP does not exist in javascript files for us to obtain the path of the current FILE. After research, we can use the following methods:
1. Get the current JS file path in jQuery
It is relatively simple to obtain the path of the current JS file in jQuery. You only need the following line of code.
Var _ FILE _ = $ ("script"). last (). attr ("src ");
Note: We usually place this line of code at the beginning of the file so that the file is executed immediately when it is loaded. In this way, the current file is exactly the last script in the script element on the page. We should never put this line of code
The Code is as follows: |
Copy code |
$ (Document). ready (); $ (Function (){}); |
Run in, because if you put it in these statements, the DOM object on the page has been loaded, and the current script is not necessarily the last script, resulting in incorrect paths.
2. Native javascript gets the current JS file path
In Native javascript, there are two methods to obtain the current JS file path. The first approach is the same as in jQuery:
The Code is as follows: |
Copy code |
Var _ FILE __, scripts = document. getElementsByTagName ("script "); _ FILE _ = scripts [scripts. length-1]. getAttribute ("src "); |
The second method is to use the Exception Handling Mechanism of the browser, but this method does not support IE10 or earlier versions of IE:
The Code is as follows: |
Copy code |
Var _ FILE __; Try { Throw Error ("An Error occurred while obtaining the JS path "); } Catch (ex ){ If (ex. fileName) // Firefox _ FILE _ = ex. fileName; Else if (ex. stack) // Chrome or IE10 _ FILE _ = (ex. stack. match (/ats + (.*?) : D +/) | ['','']) [1]; Else if (ex. sourceURL) // Safari _ FILE _ = ex. sourceURL; } |
When obtaining the current file path in native javascript, we recommend that you use the first method, which is compatible with all browsers. The second method is for reference only.
Now let's look at a complete instance.
The Code is as follows: |
Copy code |
<! Doctype html> <Html lang = "en"> <Head> <Meta charset = "UTF-8"/> <Meta content = "IE = 8" http-equiv = "X-UA-Compatible"/> <Title> obtain the JS file path by situ zhengmei </title> <Meta http-equiv = "content-type" content = "text/html; charset = UTF-8"/> <Script src = "/javascripts/dom-1.0.js? 1270565974 "type =" text/javascript "> </script> </Head> <Body> <Div> get the JS file path by situ zhengmei </div> </Body> </Html>
|
The following is the content of the JS file. Of course, the content is only discussed in this blog post:
The Code is as follows: |
Copy code |
Dom = {}; Dom. getBasePath = function (){ Var result = "" Try { A = B } Catch (e ){ If (e. fileName) {// firefox Result = e. fileName; } Else if (e. sourceURL) {// safari Result = e. sourceURL; } Else if (e. stacktrace) {// opera Var m = e. stacktrace. match (/() ins + (.*? : // S +)/m ); If (m & m [1]) Result = m [1] } } If (! Result) {// IE and chrome Var scripts = document. getElementsByTagName ("script "); Var reg =/dom ([.-] d) *. js (W | $)/I For (var I = 0, n = scripts. length; I <n; I ++ ){ Var src = !! Document. querySelector? Scripts [I]. src : Scripts [I]. getAttribute ("src", 4 ); If (src & reg. test (src )){ Result = src Break; } } } Return result. substr (0, result. lastIndexOf ('/') + 1 ); } Alert (dom. getBasePath (); // http: // localhost: 3000/javascripts/ |
Add related methods
// Obtain the full path of the current file
The Code is as follows: |
Copy code |
<Script language = "javascript"> Alert (window. location. href ); Alert (window. location ); Alert (location. href ); Alert (parent. location. href ); Alert (top. location. href ); Alert (document. location. href ); Alert (document. URL ); </Script> // Obtain the current directory <Script type = "text/javascript"> // Method 1 Var str = location. href; Var arr = str. split ("/"); Delete arr [arr. length-1]; Var dir = arr. join ("/"); Alert (dir ); |
Method for obtaining relative paths
The Code is as follows: |
Copy code |
Function getRelativePath () { Var url = location. href; // The current url Var path = url. length-url. replace (// g, ""). length-4; // Hierarchy is the length of the url containing/-the length of the uncontained/minus the number of project headers/(such as the http://hi.baidu.com/zdz8207) Var str = ""; For (var I = 0; I <path; I ++) { Str + = "../"; // returns the result of combining the string with a relative path. } Return str; } // Method 2 Alert (location. href. substring (0, location. href. lastIndexOf ('/'))); </Script> // Get the current file name <Script language = javascript> Var filename = location. href; Filename = filename. substr (filename. lastIndexOf ('/') + 1 ); Alert (filename ); </Script> |