Original: JavaScript in the external JS file to get the full path of their own way
Sometimes we need to introduce an external JS file, this JS file will need to use their own path or directory, do not ask how so sick of the demand, the development of a variety of wonderful needs for a long time!
Some people think of the first time is location.href, but brother, the path of the citation page ah. such as a.html:
In this case, the value we get with Location.href is the path to the a.html file itself, not the path we need for c.js.
So what?
I began to think of a method, that is, through the file name of JS to match, such as I write a plugin called dragondean.js, then I can refer to the page (a.html) all the script tag to get, We then get the JS file path we need by matching the SRC attribute with Dragondean.
But then there's the problem:
1. Our plugin dragondean.js cannot be renamed at the time of use.
2. A plug-in JS file repeated references can cause errors
There is always no sense of sureness in this way ...
Later..... I read a piece of article: http://blog.sina.com.cn/s/blog_715fa5c00100pwrj.html
The idea used in the browser to the Web page loading sequence of a skill, after testing is indeed feasible!
The principle is that the page document in the parsing when the side of the load parsing, when parsing to script to download the contents of the SRC and parse or parse the contents of the script, and then go down to parse. When parsing to the current script we can get all the script tags that have been loaded, and the last one is what we need to get.
The method code used is as follows:
Get the full path of itself:
function getmysrc () { var scriptsrc = document.getelementsbytagname (' script ') [ document.getElementsByTagName (' script '). length-1].src; return scriptsrc;}
Get your own file name:
function Getmyscriptname () { var scriptsrc = document.getelementsbytagname (' script ') [ document.getElementsByTagName (' script '). length-1].src; var jsname = scriptsrc.split ('/') [Scriptsrc.split ('/'). Length-1]; return Jsname;}
Get the directory path where you are:
function Getmypath () { var scriptsrc = document.getelementsbytagname (' script ') [ document.getElementsByTagName (' script '). length-1].src; var jsname = scriptsrc.split ('/') [Scriptsrc.split ('/'). Length-1]; return scriptsrc.replace (jsname, ');}
All of these things are borrowed from the previous blog post, but did a refinement! Thanks again for the original author!
The way to get the complete path of the external JS file in JavaScript