0

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>

4
  • ... and then do what with that link? Commented Jul 8, 2010 at 20:43
  • Inside the href would be whatever the previous page was. So /fare/index.html Commented Jul 8, 2010 at 20:45
  • which directory? always the last one? Commented Jul 8, 2010 at 20:46
  • Yeah it's acting as sort of a breadcrumb to the most recent page Commented Jul 8, 2010 at 20:51

4 Answers 4

1

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]
Sign up to request clarification or add additional context in comments.

Comments

1
url = window.location.href // Not particularly necessary, but may help your readability
url.match('/fare/(.*)/index.html')[1] // would return "pass"

6 Comments

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()
I am having more of an issue of actually putting that javascript in the HTML as well.
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 javascriptkit.com/javatutors/dom2.shtml: var txt = document.createTextNode(" This text was added to the DIV."); document.getElementById('myDiv').appendChild(txt);
Yeah I tried the example and it's not working. Could my javascript not be working due to working locally? That can't be.
Nevermind it was a dumb error. I guess I will just take whats here and figure out which is the best solution. Thanks!
|
0

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.

6 Comments

Right I tried this document.getElementById('waBackButton').innerHTML="Good"; as a test but didn't work with the link
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.
Yeah it looks like this [code]<a id="waBackButton" href="#" ></a>[/code]
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?
Not sure how to start with that, I've never had this strange issue before. It seems no javascript is working locally.
|
0

This may get you started:

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.