views:

55

answers:

4

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>

A: 

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.

a.feng
Right I tried this document.getElementById('waBackButton').innerHTML="Good"; as a test but didn't work with the link
Bry4n
Hmm, really? I just tried this on a link and innerHTML worked for me. Are you sure you have an element with ID 'waBackButton'? Try doing `document.getElementById('waBackButton')` in some kind of developer JavaScript console to make sure it returns a valid element.
a.feng
Yeah it looks like this [code]<a id="waBackButton" href="#" ></a>[/code]
Bry4n
Not sure what to tell you, that works perfectly for me using the Webkit Javascript console. Did you try doing any debugging in the console, as I suggested earlier?
a.feng
Not sure how to start with that, I've never had this strange issue before. It seems no javascript is working locally.
Bry4n
Note that I want this to happen on load so im not placing it in any function
Bry4n
+1  A: 
url = window.location.href // Not particularly necessary, but may help your readability
url.match('/fare/(.*)/index.html')[1] // would return "pass"
andyortlieb
Maybe this doesn't completely solve your problem, but without being given more code, or at least your framework, I'm not sure what the best way would be to build the link. This also might be way too specific if your url is more dynamic, in such case you may want to use url.split('/') instead of match()
andyortlieb
I am having more of an issue of actually putting that javascript in the HTML as well.
Bry4n
I can't quite tell where you're stuck. You'll probably start out with an empty element like <div id='breadcumb' />. Then somewhere in your javascript, you can break up the url and generate the links and append them to that element. See the example at http://javascriptkit.com/javatutors/dom2.shtml:var txt = document.createTextNode(" This text was added to the DIV.");document.getElementById('myDiv').appendChild(txt);
andyortlieb
Yeah I tried the example and it's not working. Could my javascript not be working due to working locally? That can't be.
Bry4n
Nevermind it was a dumb error. I guess I will just take whats here and figure out which is the best solution. Thanks!
Bry4n
ah the onload needed to be placed in the body tag.
Bry4n
+1  A: 

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]
Alex JL
A: 

This may get you started:

var linkElement = document.getElementById("whatever");
linkElement.innerHTML = document.URL.replace(/^(?:https?:\/\/.*?)?\/.*?\/(.*?)\/.*?$/i,"$1");
David Murdoch