?
Solved

Pass URL file name into JavaScript variable

Posted on 2011-02-15
9
Medium Priority
?
1,386 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 82

Expert Comment

by:leakim971
ID: 34896380
remove "var" in << var s.prop5 = 'home'; >>
0
 

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 2000 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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
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…
Suggested Courses

762 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