Get Location of Currently Running Script

Posted By: Ian on Feb 01, 2014 in JavaScript, Snippets
Last modified on January 29th, 2015 at 9:31 pm,

Do you need to get the URL or folder location of the currently executing script?

Here’s how i did it!

    var b = (b = document.getElementsByTagName("script")) && b[b.length - 1],
        c = (c = b.src) && c.substring(0, c.lastIndexOf('/')) + '/';
    alert(c);

Why would you need to do this?

Well if you have a script you distribute which is setup to dynamically load all it’s CSS and other components the location to each of these will need to be set each time someone installs it or defined in the script itself but this way it does it on it’s own!

Demo

Read about the JavaScript Object Methods used
lastindexof and substring

AnonymousIan.J.Gough

Did this help you? Please Let me know by commenting below even if it’s just to say “Thanks”

leave a comment

About This Site

Dreams are built from lines of code well it's true!
If you can dream it you can usually code it.

I have learned so much from other people on the Internet and this site is for me to give back some of what i have learned and hope people can now learn from me.
Have fun and surf safely,
Ian.J.Gough

protected by copyscape duplicate content check

Categories


Powered by Banner Bar 125