1, get the current JS file path in jquery
In jquery to get the current JS file path is relatively simple, only need the following line of code to be done.
var __file__ = $ ("script"). Last (). attr ("src");
Note that we typically place this line of code at the beginning of the file so that it executes immediately when the file is loaded, so that the current file is exactly the last script in the script element on the page. We must not put this line of code into
The code is as follows |
Copy Code |
$ (document). Ready (); $ (function () {}); |
, because if you put it in these statements, the page's DOM object has already been loaded, and the current script is not necessarily the last script, causing the path to get incorrect.
2, native JavaScript get current JS file path
In native JavaScript, there are two ways to get the current JS file path. The first kind of thinking 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 approach is to take advantage of the browser's exception handling mechanism, but this method does not support IE10 for the following versions of IE browsers:
The code is as follows |
Copy Code |
var __file__; try { Throw error ("Get JS path error"); }catch (ex) { if (ex.filename)//firefox __file__ = Ex.filename; else if (ex.stack)//chrome or IE10 __file__ = (Ex.stack.match (/ats+ (. *?):d +:d +/) | | ['','']) [1]; else if (Ex.sourceurl)//safari __file__ = Ex.sourceurl; } |
When getting the current file path in native JavaScript, it is recommended that you use the first method, which is compatible with all browsers, and that the second method is for informational purposes only
Now look at a complete instance
code is as follows |
copy code |
<!doctype html> & nbsp; <meta charset= "Utf-8"/> <meta content= "Ie=8" X-ua-compatible "/> <title> Get the path of the JS file by Masaki </title> <meta http-equiv= "Content-type" content= "Text/html;charset=utf-8"/> <script Src= "/javascripts/dom-1.0.js?1270565974" type= "Text/javascript" ></SCRIPT> <BODY> <div> Get the path of the JS file by Masaki </div> </body |
The following is the content of JS files, of course, now put out only for this blog discussion of things:
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/ |
To supplement some relevant methods
Get current file full path
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> Get current Directory method <script type= "Text/javascript" > Method One var str = location.href; var arr = str.split ("/"); Delete Arr[arr.length-1]; var dir = arr.join ("/"); Alert (dir); |
Ways to get relative paths
code is as follows |
copy code |
function Getrelativepath () { var url = location.href;//Current URL var path = Url.length-url.replace (///g, ""). length-4; //level for URL contains/length-no included/length minus item Header/number (e.g. http://hi.baidu.com/zdz8207/) var str = ""; for (var i = 0; i < path; i++) { str + = ". /";//The string combined into a relative path returns } return str; } //Method two Alert (location.href.substring 0,location.href.lastindexof ('/')); </script> //Get current file name <script language=javascript> var Filename=location.href; Filename=filename.substr (Filename.lastindexof ('/') +1); Alert (filename); </script> |