[thelist] Using RegExp to replace an <a> tag

Brad Dobyns bdobyns at dhr.state.ga.us
Thu Mar 13 11:54:28 CST 2003


Hello there,

I am trying to get a better handle on Javascript so I need some assistance with a particular problem. I need to have a link tag disabled when a user, who has Javascript turned on, goes to any page on the site. I have several links on a page so, it needs to find the pathname with an <a> tag that is similar to the current location.

Here is what I have so far:

<table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
     <td><font face="Arial, Helvetica, Sans-serif" size="1"><a href="/programs/ems/includes/javatest2.htm">JavaScript Test</a></font></td>
  </tr>
  <tr>
     <td><font face="Arial, Helvetica, Sans-serif" size="1"><a href="/programs/ems/oath.shtml">EMT Oath</a></td>
  </tr>
  <tr>
     <td><font face="Arial, Helvetica, Sans-serif" size="1"><a href="/programs/ems/faq.shtml">Frequently Asked Questions</a></td>
  </tr>
  <tr>
     <td><font face="Arial, Helvetica, Sans-serif" size="1"><a href="/programs/ems/patches.shtml">Patches and Rockers</a></td>
  </tr>
  <tr>
     <td><font face="Arial, Helvetica, Sans-serif" size="1"><a href="/programs/ems/offices.shtml">Regional Offices</a></td>
  </tr>
  <tr>
     <td><font face="Arial, Helvetica, Sans-serif" size="1"><a href="/programs/ems/links.shtml">Related Links</a></td>
  </tr>
</table>
<p></p>
<script language="JavaScript">
<!--
    var dir = (location.pathname.substring(0,location.pathname.lastIndexOf('/')+4));
    var url = location.href.substring(dir.length,location.href.length+1);
    document.write(url + "<br>") //url = current page user is on
    for(var i=0; i < document.links.length; ++i)
    document.write("<br>" + document.links[i].pathname);
    //if (document.links[i].pathname == url)
    //{
    //     document.write("<br>It works!!");
    //} else {
    //     document.write("<br>No...it doesn't.");
    //}
// -->
</script>

The reason the if...else statement is commented out is because I get an error that tells me the value of document.links[i].pathname is null. If I can get it to not be null, what I would like for the if...else statement to do is to use RegExp to replace the instance where (document.links[i].pathname == url) within the table above (leaving the <font> tag alone) with something like:

newLinkPath = (+document.links[i].text+)

That way the link is now showing up as regular text and not a link IF the user has Javascript turned on. If the user has JS turned off, it should display the <a> tag as normal. Can anyone help me please? I realize this is a stupid way of doing this but when the boss speaks about making sure that ALL users can use the pages, you don't have a choice. If what I have said is not clear I will try to simplify. Thank you.

Brad



More information about the thelist mailing list