Jump to content

Current page reference


kurt.santo

Recommended Posts

I would like to come up with a JavaScript (also try at same time a server-side solution) to replace in the nav bar the </a> for the current page with a </strong>. How can JavaScript check for the current page? I would think it checks the file inside the url, but not sure about code to get the file ...Any hints appreciated. Would be a great solution for a small site I am working on (no server side technology)...Kurt

Link to comment
Share on other sites

You can try this:

var obj;var i;var L = document.getElementsByTagName("a");//Step 1: get the link in questionfor(i=0;i<L.length;i++) {   if(L[i].href.indexOf(location.pathname) != -1) {	  obj = L[i];   }}//Step 2: Obtain data from the linkvar Ldata = obj.firstChild.nodeValue; //obtains text from the first child//Step 3: Create the <strong> elementS = document.createElement("strong");T = document.createTextNode(Ldata);S.appendChild(T);//Step 4: Insert it into the correct location;obj.parentNode.insertBefore(S,obj);//Step 5: Delete the <a> elementobj.parentNode.removeChild(obj);

Link to comment
Share on other sites

You can try this:
var obj;var i;var L = document.getElementsByTagName("a");//Step 1: get the link in questionfor(i=0;i<L.length;i++) {   if(L[i].href.indexOf(location.pathname) != -1) {	  obj = L[i];   }}//Step 2: Obtain data from the linkvar Ldata = obj.firstChild.nodeValue; //obtains text from the first child//Step 3: Create the <strong> elementS = document.createElement("strong");T = document.createTextNode(Ldata);S.appendChild(T);//Step 4: Insert it into the correct location;obj.parentNode.insertBefore(S,obj);//Step 5: Delete the <a> elementobj.parentNode.removeChild(obj);

Ingolme,I tried your script (under http://www.jalp.co.uk/testing/replace/home.htm), but it does not work. Tried few things, but without results. What does the line " if(L.href.indexOf(location.pathname) != -1) " do? Do not get this bit of the code. I kind of understand that it does check for the url, but that is where it ends...Kurt (the one who does not have a clue about JavaScript)
Link to comment
Share on other sites

Hmm, I told it to seach the links for the path of the current pageGiven the url "http://www.domain.com/folder/file.html"location.pathname returns the string "/folder/file.html".Translation to english of this line

if(L[i].href.indexOf(location.pathname) != -1)

If the position of location.pathname in the HREF of this <a> tag is not -1 (indexOf returns -1 if the string isn't found)---If you want to get the filename and not the full path you'll need to extract it from the path, here's my method:

pos = location.pathname.lastIndexOf("/");str = location.pathname.substr(pos+1);if(L[i].href.indexOf(str) != -1)

Link to comment
Share on other sites

Hmm, I told it to seach the links for the path of the current pageGiven the url "http://www.domain.com/folder/file.html"location.pathname returns the string "/folder/file.html".Translation to english of this line
if(L[i].href.indexOf(location.pathname) != -1)

If the position of location.pathname in the HREF of this <a> tag is not -1 (indexOf returns -1 if the string isn't found)---If you want to get the filename and not the full path you'll need to extract it from the path, here's my method:

pos = location.pathname.lastIndexOf("/");str = location.pathname.substr(pos+1);if(L[i].href.indexOf(str) != -1)

Thanks for explanation. Get the point. Still not working, problem being that "obj is null or undefined". Do not get the problem as you have "obj = L;" in your code. Why is that?KurtEdit:Just installed the Firebug plugin for JSdebugging and checked few more details.It gives L correctly as an array of "[a home.htm, a about.htm, a more.htm]", but says that "i =0" (on all three pages). It should have returned 1, shouln't it? I mean this is the check for the current link?
Link to comment
Share on other sites

Thanks for explanation. Get the point. Still not working, problem being that "obj is null or undefined". Do not get the problem as you have "obj = L;" in your code. Why is that?KurtEdit:Just installed the Firebug plugin for JSdebugging and checked few more details.It gives L correctly as an array of "[a home.htm, a about.htm, a more.htm]", but says that "i =0" (on all three pages). It should have returned 1, shouln't it? I mean this is the check for the current link?
I assign the link L[i ] to the variable obj so that I can reference it in the rest of the script. If I didn't do that [b]i[/b] would change and I couldn't reference that object anymore with L[ i].EDIT:the variable i should increase with each link there is on the page while it's searching for the one that contains the current page location.
Link to comment
Share on other sites

I assign the link L[i ] to the variable obj so that I can reference it in the rest of the script. If I didn't do that [b]i[/b] would change and I couldn't reference that object anymore with L[ i].EDIT:the variable i should increase with each link there is on the page while it's searching for the one that contains the current page location.
That is what I mean. Your code look great! I really do not understand why it has a problem and does not do what it should...Kurt
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...