Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    Regular Coder
    Join Date
    Feb 2004
    Posts
    192
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Is there a javascript function that returns file name

    Is there a javascript function that returns the filename of where that javascript is sitting, be it a .js file or an .htm file?

  • #2
    Senior Coder A1ien51's Avatar
    Join Date
    Jun 2002
    Location
    Between DC and Baltimore In a Cave
    Posts
    2,717
    Thanks
    1
    Thanked 94 Times in 88 Posts
    There is not to my knowledge.

    Best thing you can do is use getElementsByTagName and look to see if the script tag has a src attribute.

    Eric
    Tech Author [Ajax In Action, JavaScript: Visual Blueprint]

  • #3
    Senior Coder
    Join Date
    Feb 2004
    Location
    Edinburgh
    Posts
    1,352
    Thanks
    0
    Thanked 0 Times in 0 Posts
    you could always code your own using the prototype method.

    infact, you could do that for anything that a predefined method didn't already exist for. sweet.

  • #4
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,043
    Thanks
    0
    Thanked 251 Times in 247 Posts
    You can find the filename of the current html page using location.pathname property.

    alert(location.pathname);

    If the script is external, I don't think you can get the filename of the js file unless you know that it is the only external js. You'd do it using Alien51's suggestion.

  • #5
    Senior Coder
    Join Date
    Feb 2004
    Location
    Edinburgh
    Posts
    1,352
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by glenngv
    You can find the filename of the current html page using location.pathname property.
    not necessarily.

    it returns the full URL, minus the protocol and search string.

    and besides, if the pathname returns "/homepage" that still doesn't say wot the filename is. The file which sits on the server might be index.htm, index.html, index.shtml, index.stm, index.php, default.asp, etc. but you won't necessarily know because of the way the server is set up: anything could be registered as the default filename - even glenngv.ghtml

  • #6
    Banned
    Join Date
    Sep 2003
    Posts
    3,620
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Are we talking about something like this?

    Code:
    <script src="aaa111.js"></script>
    <script src="aaa112.js"></script>
    <script src="aaa113.js"></script>
    </head>
    <body>
    
    <script type="text/javascript">
     var script = document.getElementsByTagName('script');
      for(var i = 0; i<script.length; i++){
       var path = location.path+script[i].src;
        if(path == 'undefined'){
           alert(script[i].innerHTML);
        }  else{ alert('Script Source: '+path) };
      }
    </script>
    </body>
    .....Willy


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •