Reputation: 24399
Can anyone help me. I don't use Client-side Javascript often with HTML.
I would like to grab the current url (but only a specific directory) and place the results between a link.
So if the url is /fare/pass/index.html
I want the HTML to be <a href="#" id="whatever">pass</a>
Upvotes: 0
Views: 2044
Reputation: 89402
This may get you started:
var linkElement = document.getElementById("whatever");
linkElement.innerHTML = document.URL.replace(/^(?:https?:\/\/.*?)?\/.*?\/(.*?)\/.*?$/i,"$1");
Upvotes: 0
Reputation: 21563
This is a quick and dirty way to do that:
//splits the document.location.href property into an array
var loc_array=document.location.href.split('/');
//have firebug? try a console.log(loc_array);
//this selects the next-to-last member of the array.
var directory=loc[loc.length-2]
Upvotes: 1
Reputation: 2416
url = window.location.href // Not particularly necessary, but may help your readability
url.match('/fare/(.*)/index.html')[1] // would return "pass"
Upvotes: 1
Reputation: 1073
There may be an easier answer, but the simplest thing I can think of is just to get the current URL with window.location
and use some type of parsing to get which directory you are looking for.
Then, you can dynamically append the HTML to your page.
Upvotes: 0