Forum Moderators: open

Message Too Old, No Replies

Using regexp to disable links to current page

Dynamically implementing usability guideline

         

JonBoy

8:45 pm on Jan 27, 2004 (gmt 0)

10+ Year Member



There's a well know usability guideline about not have active links that point to the current page. The idea is that such links (a) make users doubt they are where they think they are, and (b) waste their time if they do click on them.

My pages are dynamic and make heavy use of templates etc., so I don't want to create a separate menu for each page with just that link disabled.

I was thinking there must be a way to get my asp code to find links that point to the current page and disable them. I think a regular expression would be the way to do this, but I'm a real novice at them and don't know the syntax.
Can anyone help?

What it needs is to
At minimum: find any href="this URL" inside an <a> tag and replace "this URL" with nothing

Ideally: take out the whole <a> tag and the following </a> (in order to remove the associated formatting as well.

Can anyone help with these regular expressions? :)

WebJoe

10:11 pm on Jan 27, 2004 (gmt 0)

10+ Year Member



I have a similar functionality on my site, where the navigation bar is an asp-inluded file, the same for every page so if I add a page I only have to edit the navigation in one file.
So first I have to figure out "where" the navigation is embedded, I do that with another inluded file with the following code:

<%

Dim sFullPath

Dim lPathLength

Dim iCounter

Dim ptrCharacterStart, ptrCharacterEnd

Dim sCurrentCharacter, sCurrentDirectory

Dim FileName, SubZone, MainZone

[/code]
[code]sFullPath = Request.ServerVariables("Path_Info")

[/code]
[code]'--------------------------------------------------

' Here we just snip the leading / away.

' reduce length because we just snipped the above char.

' set where we start looking. 

'--------------------------------------------------

[/code]
[code]lPathLength = Len(sFullPath)

sFullPath = Mid(sFullPath,2,lPathLength)

lPathLength = (lPathLength -1)

ptrCharacterStart = 1

[/code]
[code]'--------------------------------------------------

' This loop is the actual script that parses the current URL.

'--------------------------------------------------

[/code]
[code]For iCounter = 1 to lPathLength

 sCurrentCharacter = Mid(sFullPath,iCounter,1)

 If sCurrentCharacter = "/" Then

 ptrCharacterEnd = iCounter

 sCurrentDirectory = Mid(sFullPath,ptrCharacterStart,ptrCharacterEnd-(ptrCharacterStart-1))

 ptrCharacterStart = (iCounter + 1)

 End If

Next

[/code]
[code]FileName = Right(sFullPath,(lPathLength-ptrCharacterEnd))

MainZone = Replace(sCurrentDirectory,"/","") ' Current Directory

SubZone = Left(FileName,Len(FileName)-4) ' Here we just snip the 4char extension away.

%>

I named this file

NameLocation.inc
and place it in the directory
/include/
and adde to every page this line inside the heaer:
<!--#include File='/include/NameLocation.inc'-->

Then I go and edit my navigation, from for example:

<a href="/folder/file.asp">linktexthere</a>

to

<% If MainZone = "folder" and SubZone = "file" Then %>

<a href="/folder/file.asp">

<% End If %>

linktexthere

<% If MainZone = "folder" and SubZone = "file" Then %>

</a>

<% End If %>

of course in that example you could discuss if the html should be outputted with a Response.Write to not mix asp and html too much (helps the readability) and all the white spaces it produces, but this way you can look at it in any WYSIWYG html editor and see what you'll get, and it'll do the trick

JonBoy

10:49 am on Jan 28, 2004 (gmt 0)

10+ Year Member



Hi Joe,

thanks for that but unfortunately it won't work for me.

Your code takes the route of "only ADD in link if it does not point to current page". But I need to do the reverse: "REMOVE link from HTML page (passed through code as a single string) if it points to current page".

This is because I'm using a content management system, so my menus etc. are stored as blocks of HTML in a database - I can parse them during the output process, but I can't intersperse them with ASP code in the fashion that your code does.

I should have been clearer in my original post :(

Jonathan

Xoc

4:55 pm on Jan 28, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I won't solve your problem, but I'll point out a slight alternative. In one application, I don't remove the links, but I do change them so they don't look like links. In the CSS for the web site, I have this:

.currentpage
{
cursor: default;
text-decoration: none;
color: Black;
}

When I see a link for the current page, I change it to add the currentpage class:

<a href="/thecurrentpage.asp" class="currentpage">This links to the current page</a><br>
<a href="/someotherpage.asp">This links to some other page</a>

So while there is a hyperlink that links to the current page, it doesn't look like a hyperlink.

Jakob Nielsen's point about not linking to the current page is that users get disconcerted that they click on a hyperlink and nothing happens. Well, in this case, all the visual clues are that this isn't a hyperlink, so if you click on it and nothing happens, then all is well with the world. I think I addressed his real point, without having to actually remove the hyperlink altogether, which is substantially more difficult.

JonBoy

5:14 pm on Jan 28, 2004 (gmt 0)

10+ Year Member



Hi Xoc,

Your solution to the style problem is fine for me.

But the real challenge is how to implement it dynamically, e.g. if you have a single menu block and you don't want to modify by hand for each page.

Or perhaps I'm just too lazy! :)