1

I want to find the script path of my own js file in itself.

So I want have as a string "C:\files\MyProject\MyScripts\MyJavaScript.js".

How is that possible?

3
  • It's totally unclear... Commented Sep 5, 2014 at 7:08
  • 2
    possible duplicate of Get script path Commented Sep 5, 2014 at 7:10
  • Why you need script path? If you just want to know where the js file to understand the implementation, you can search this file. Commented Sep 5, 2014 at 7:19

6 Answers 6

2

You can try (Jquery):

var myScriptDetails = $('script');

myScriptDetails will contain details regarding the script, including its location.

Sign up to request clarification or add additional context in comments.

1 Comment

myScriptDetails will contain details of all the script linked on the page. The question here is what will the script file do to find it's own location.
1

Try this solution. I think this is exactly what u want :)

Put this code in each of your linked script file

var scriptEls = document.getElementsByTagName( 'script' );
var thisScriptEl = scriptEls[scriptEls.length - 1];
var scriptPath = thisScriptEl.src;
var scriptFolder = scriptPath.substr(0, scriptPath.lastIndexOf( '/' )+1 );

console.log(scriptPath +"  "+ scriptFolder );// you can save these in any variable also

I tested it with this HTML code:

<!DOCTYPE html>
<html>
  <head> 
      <title>testing...</title>  
      <script type="text/javascript" src="test.js"></script> 
      <script type="text/javascript" src="js/test2.js"></script> 
      <script type="text/javascript" src="../test3.js"></script> 
    </head>  
    <body>
     content area
    </body>
</html>

And found this Output in console:

file:///D:/workspace/dbshell/www/test.js  file:///D:/workspace/dbshell/www/          test.js:6
file:///D:/workspace/dbshell/www/js/test2.js  file:///D:/workspace/dbshell/www/js/   test2.js:6
file:///D:/workspace/dbshell/test3.js  file:///D:/workspace/dbshell/                 test3.js:6

Special thanks to meouw.. Hope this helps..

Comments

1

here is how I made it:

function ScriptPath() {
  var scriptPath = '';
  try {
    //Throw an error to generate a stack trace
    throw new Error();
  }
  catch(e) {
    //Split the stack trace into each line
    var stackLines = e.stack.split('\n');
    var callerIndex = 0;
    //Now walk though each line until we find a path reference
    for(var i in stackLines){
      if(!stackLines[i].match(/http[s]?:\/\//)) continue;
      //We skipped all the lines with out an http so we now have a script reference
      //This one is the class constructor, the next is the getScriptPath() call
      //The one after that is the user code requesting the path info (so offset by 2)
      callerIndex = Number(i) + 2;
      break;
    }
    //Now parse the string for each section we want to return
    pathParts = stackLines[callerIndex].match(/((http[s]?:\/\/.+\/)([^\/]+\.js)):/);
  }

  this.fullPath = function() {
    return pathParts[1];
  };

  this.path = function() {
    return pathParts[2];
  };

  this.file = function() {
    return pathParts[3];
  };

  this.fileNoExt = function() {
    var parts = this.file().split('.');
    parts.length = parts.length != 1 ? parts.length - 1 : 1;
    return parts.join('.');
  };
}

Comments

0

Client-side you can't access the physical path. You can get the src attribute of the script tag though.

Server-side you can get the physical path. For example (C#):

Path.GetFullPath(path)

Comments

0

For finding full path of URL in JavaScript use this location.href

Comments

0

If you can define absolute path of server as constant variable,

you can do it by casting from window.location.href. remove host prefix window.location.host from window.location.href and prepand with server's absolute path.

Try:

 console.log(window.location.href);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.