Get js file path

Discussion in 'JavaScript' started by s_ruben, Dec 17, 2010.

  1. #1
    Hello

    How can I get the path of the included js file just from the js file?

    Thank you.
     
    s_ruben, Dec 17, 2010 IP
  2. AcevedoAaron

    AcevedoAaron Peon

    Messages:
    49
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    at first you have to find the name of script you want to download then search it on the current js file i hope it will working out some time its like \xxx\x\x.js i think don't have to explain more
     
    AcevedoAaron, Dec 17, 2010 IP
  3. s_ruben

    s_ruben Active Member

    Messages:
    735
    Likes Received:
    26
    Best Answers:
    1
    Trophy Points:
    78
    #3
    AcevedoAaron, thank you for your reply, but I mean that for example I've included script.js file in index.html

    <script src="/dir/subdir/script.js" type="text/javascript"></script>

    and I want add a code in the script.js file which will alert /dir/subdir/script.js. I think now it is easier to understand what I want.

    Thank you.
     
    s_ruben, Dec 17, 2010 IP
  4. ThePHPMaster

    ThePHPMaster Well-Known Member

    Messages:
    737
    Likes Received:
    52
    Best Answers:
    33
    Trophy Points:
    150
    #4
    Try this:

    
    <script type="text/javascript">
    document.write(document.URL);
    </script>
    
    PHP:
     
    ThePHPMaster, Dec 18, 2010 IP
  5. shofstetter

    shofstetter Well-Known Member

    Messages:
    178
    Likes Received:
    7
    Best Answers:
    1
    Trophy Points:
    120
    #5
    You could give your script tag an id and then extract the src from it like this:

    <script type="text/javascript" id="myscript" src="/dir/subdir/script.js"></script>

    and in your script you could do:

    var my_location = document.getElementById('myscript').src;

    alert(my_location);

    my_location would then = "/dir/subdir/script.js"
     
    shofstetter, Dec 18, 2010 IP
  6. s_ruben

    s_ruben Active Member

    Messages:
    735
    Likes Received:
    26
    Best Answers:
    1
    Trophy Points:
    78
    #6
    Thank you for you replies, but ThePHPMaster, your example didn't do what I want and, shofstetter, I want to do by changing only the js file.
     
    s_ruben, Dec 20, 2010 IP
  7. ThePHPMaster

    ThePHPMaster Well-Known Member

    Messages:
    737
    Likes Received:
    52
    Best Answers:
    33
    Trophy Points:
    150
    #7
    Sorry, just notice that you wanted the sub document URL and not the parent.

    If you have used PHP, Javascript is similar in their includes, except that you can not get the sub url but rather the main one.

    The only solution is to do what shof said, even in the sub file (included script.js):

    
    alert(document.getElementById('myscript').src);
    
    Code (markup):
    As long as that object has an ID, you can track it on any Javascript on that page.
     
    ThePHPMaster, Dec 22, 2010 IP
  8. s_ruben

    s_ruben Active Member

    Messages:
    735
    Likes Received:
    26
    Best Answers:
    1
    Trophy Points:
    78
    #8
    OK. Thank you very much.
     
    s_ruben, Dec 23, 2010 IP