Welcome to WebmasterWorld Guest from

Forum Moderators: open

Message Too Old, No Replies

Javascript : Filename extract and insert in a src syntax problem

4:33 am on Sep 4, 2002 (gmt 0)

New User

10+ Year Member

joined:Aug 31, 2002
votes: 0

This script extracts html name and give it to a src with a js file in an other domain.
There is a syntax problem in this script which I couldnt find:

<script language="JavaScript"><!--
var fileName =location.pathname.substring(location.pathname.lastIndexOf('/')+1);
document.write('<script src="http://www.abc.com/path/'+fileName+'.js"><\/script>');

What is the correct syntax?

4:44 am on Sept 4, 2002 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Jan 21, 2002
votes: 0

Firstly, doesn't location.pathname have back-slashes instead of forward-slashes? Try lastIndexOf('\')+1

Secondly, when it works, I think you'll get the following URL in your src attribute (note the unwanted ".htm"):

Thirdly, get rid of the HTML comments (I heard somewhere that some browsers don't like them): <!-- and //-->


1:55 pm on Sept 6, 2002 (gmt 0)

Preferred Member

10+ Year Member

joined:June 7, 2002
votes: 0

Pathnames on the WWW are UNIX-based, so they do use forward slashes.

You could try escaping all the slashes and the < in your strings:

var fileName = location.pathname.substring(location.pathname.lastIndexOf('\/')+1);
document.write('\074script src=\042http:\/\/www.abc.com\/path\/' + fileName + '.js\042>\074\/script>');

That should ensure the code will validate if it's an embedded script.

There's no actual syntax error that I can find. Perhaps you could describe what actually happens and what error message you are getting.

4:41 pm on Sept 6, 2002 (gmt 0)

New User

10+ Year Member

joined:Aug 31, 2002
votes: 0

Thanks for your help guys.

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members