tags:

views:

86

answers:

5
+1  Q: 

Get script path

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?

+1  A: 

(If base [Rubens's answer] doesn't work for you. Edit: Apparently he removed it, but I thought it was relevant; see base on the W3C site.)

It's possible, but it's a pain. :-) You have to search the DOM for the script tag that imported your script, and then grab its src value. That's how script.aculo.us does its module auto-loading; you can refer to the scriptaculous.js file for an example.

T.J. Crowder
Not such a big pain. I posted a short script below that can do this.
yannis
@yannis: Yeah, you're right, it's not that much of a hassle.
T.J. Crowder
A: 

You can flip through <head>'s childNodes, find your <script> tag, get the url of said script, and compute the url of your image.

Alsciende
Which is to say, he can do what I suggested in my answer. ;-)
T.J. Crowder
A: 

One way is to put the path in the script itself:

var scriptPath = <?PHP echo json_encode(dirname($_SERVER['SCRIPT_NAME'])); ?>;
strager
+1  A: 

As other posters mentioned you need to compute your base url for the script first, you can the script below to find it.

// find the base path of a script
var settings = {};
settings.basePath = null;

if (!settings.basePath) {
  (function (name) {
    var scripts = document.getElementsByTagName('script');

    for (var i = scripts.length - 1; i >= 0; --i) {
      var src = scripts[i].src;
      var l = src.length;
      var length = name.length;
      if (src.substr(l - length) == name) {
        // set a global propery here
        settings.basePath = src.substr(0, l - length);

      }
    }
  })('myfile.js');
}

log(settings.basePath);
yannis
A: 

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].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.

bobince