Solved

Pass URL file name into JavaScript variable

Posted on 2011-02-15
9
1,380 Views
Last Modified: 2012-06-27
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 :)
 
0
Comment
Question by:PlumInternet
  • 6
  • 3
9 Comments
 
LVL 82

Expert Comment

by:leakim971
ID: 34896312
Use :


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

Open in new window

0
 
LVL 82

Expert Comment

by:leakim971
ID: 34896331
More info about the location object here : http://www.w3schools.com/jsref/obj_location.asp
Clipboard02.jpg
0
 

Author Comment

by:PlumInternet
ID: 34896365
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
0
 
LVL 82

Expert Comment

by:leakim971
ID: 34896380
remove "var" in << var s.prop5 = 'home'; >>
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:PlumInternet
ID: 34896398
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
0
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
ID: 34896420
For example :
s.prop5 = 'home';
var start = location.pathname.lastIndexOf("/");
if(start>0) {
   s.prop5 = location.pathname.substr(start+1).split(".")[0];
}

Open in new window

0
 
LVL 82

Expert Comment

by:leakim971
ID: 34896426
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

0
 

Author Comment

by:PlumInternet
ID: 34896474
Hi leakim971,

Thanks for your prompt support :)
0
 
LVL 82

Expert Comment

by:leakim971
ID: 34896496
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 >>
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now