...

View Full Version : Is there a javascript function that returns file name



deadseasquirrel
07-01-2004, 07:51 PM
Is there a javascript function that returns the filename of where that javascript is sitting, be it a .js file or an .htm file?

A1ien51
07-01-2004, 08:56 PM
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

jbot
07-02-2004, 09:34 AM
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.

glenngv
07-02-2004, 12:17 PM
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.

jbot
07-02-2004, 01:39 PM
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

Willy Duitt
07-02-2004, 02:04 PM
Are we talking about something like this?



<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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum