Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get script path

Tags:

javascript

css

In CSS, any image path is relative to the CSS file location.

f.ex if I put the CSS file in /media/css/mystyles.css and use something like

.background:url(../images/myimage.jpg); 

The browser will look for the image in /media/images/myimage.jpg which makes sense.

Is it possible to do the same thing in javascript?

F.ex if I include /media/js/myscript.js and put this code in there:

var img = new Image(); img.src = '../images/myimage.jpg'; 

Th image is not found, since browser is using the HTML file as a starting point, instead of the script location. I would like to be able to use the script location as a starting point, just like CSS does. Is this possible?

like image 597
David Hellsing Avatar asked Jan 29 '10 10:01

David Hellsing


People also ask

How do I get a script path?

To get the full path of the script we need to use the $myInvocation command. This is an automatic variable and it is only invoked when the script or the function is executed. $MyInvocation. MyCommand.

How do I find the path of a shell script?

How do I find out the current directory location and shell script directory location in Bash running on Linux or Unix like operating systems? basename command – Display filename portion of pathname. dirname command – Display directory portion of pathname.

What is shell script path?

The PATH environment variable is a colon-delimited list of directories that your shell searches through when you enter a command. Program files (executables) are kept in many different places on the Unix system. Your path tells the Unix shell where to look on the system when you request a particular program.

How do I run a full path in a shell script?

To use full path you type sh /home/user/scripts/someScript . sh /path/to/file is different from /path/to/file . sh runs /bin/sh which is symlinked to /bin/dash . Just making something clear on the examples you see on the net, normally you see sh ./somescript which can also be typed as `sh /path/to/script/scriptitself'.


2 Answers

Searching the DOM for your own <script> tag as above is the usual method, yes.

However, you usually needn't search too hard: when you're in the body of the script — run at include-time — you know very well which <script> element you are: the last one. The rest of them can't have been parsed yet.

var scripts= document.getElementsByTagName('script'); var path= scripts[scripts.length-1].src.split('?')[0];      // remove any ?query var mydir= path.split('/').slice(0, -1).join('/')+'/';  // remove last filename part of path  function doSomething() {     img.src= mydir+'../images/myimage.jpeg'; } 

This doesn't hold true if your script has been linked with <script defer> (or, in HTML5, <script async>). However, this is currently rarely used.

like image 161
bobince Avatar answered Oct 04 '22 12:10

bobince


On more recent browsers, you can use the document.currentScript property to obtain the HTMLScript element corresponding to that script, then query its src property.

Can I use indicates support by 70% of the web’s users, at the time of this writing. Apparently Internet Explorer doesn’t support it, but Edge does. MDC lists support as Chrome 29+, Edge, Firefox 4.0+, Gecko 2.0+, Opera 16+ and Safari 8+. The comment by @mjhm already pointed out this feature, but back then it sounded very experimental.

like image 41
MvG Avatar answered Oct 04 '22 12:10

MvG