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?

PassionateDeveloper
  • 14,558
  • 34
  • 107
  • 176

6 Answers6

2

You can try (Jquery):

var myScriptDetails = $('script');

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

Amiga500
  • 5,874
  • 10
  • 64
  • 117
  • 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. – xxbinxx Sep 06 '14 at 04:46
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..

Community
  • 1
  • 1
xxbinxx
  • 1,527
  • 11
  • 18
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('.');
  };
}
PassionateDeveloper
  • 14,558
  • 34
  • 107
  • 176
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)
Kristoffer Jälén
  • 4,112
  • 3
  • 30
  • 54
0

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

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);
xxbinxx
  • 1,527
  • 11
  • 18
KyawLay
  • 403
  • 2
  • 10