Link to home
Create AccountLog in
Avatar of PlumInternet
PlumInternet

asked on

Pass URL file name into JavaScript variable

Hello,

We would like to pass the file name found within the URL into a Javascript Variable.

For example if the URL is:

http://www.abc.com/folder/file-something.aspx
Or
http://www.abc.com/folder/folder/file-something.aspx

Then
s.prop5 = 'file-something';

The tricky part is that we need to also include an if statment to populate a default value if the home page is loaded in the URL.

For example if the URL is

http://www.abc.com

Then s.prop5 = 'home';

Thanks :)
 
Avatar of leakim971
leakim971
Flag of Guadeloupe image

Use :


var s.prop5 = 'home';
var start = location.pathname.lastIndexOf("/");
if(start>0) {
   s.prop5 = location.pathname.substr(start+1);
}

Open in new window

Avatar of PlumInternet
PlumInternet

ASKER

Hi leakim971,

Thanks for the snippet, we have dropped it into our dev site: http://beta.mazero.com

and we are receiving an error Expected ';'

Any ideas ?

Thanks
remove "var" in << var s.prop5 = 'home'; >>
Thanks that has removed the javascript error, but it is passing through the file extension as well ".html" we are hoping to truncate this from the passed variable. As per our example above.

Thanks
ASKER CERTIFIED SOLUTION
Avatar of leakim971
leakim971
Flag of Guadeloupe image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Or :
var end = location.pathname.lastIndexOf(".")
s.prop5 = 'home';
var start = location.pathname.lastIndexOf("/");
var end = location.pathname.lastIndexOf(".");
if(start>0) {
   s.prop5 = location.pathname.substring(start+1, end);
}

Open in new window

Hi leakim971,

Thanks for your prompt support :)
You're welcome my friend! Thanks for the points!
Be careful with the first proposition (accepted solution) if your filename have more than one dot in it : my.file.name.html will return << my >>