It seemed that the original author had written all the links (or written scripts to generate the links from document names in some cases) using the target property in the anchor tag and had set the value to _new, thus:
<a href="http://www.somesite.com" target="_new">A Link</a>
However, when you opened a new window by clicking on a link, _new is the assigned ID of the window, so the next link with a target of _new would end up being opened in that same window thus nobbling whatever you were viewing. The solution is to have a unique target reference for each link. But how do you guarantee a totally unique ID for each link with minimum effort, especially when many of the links are created dynamically? Step forward the GUID (Globally Unique IDentifier). I found a snippet which allows you to generate a system GUID very simply in ASP as follows:
<%
Function CreateGUID
Dim objGuid
Dim sGUID
Set objGuid = Server.CreateObject("scriptlet.typelib")
sGUID = objGuid.guid
Set objGuid = Nothing
End Function
>%
Great. Except there was a problem. Every time I used my returned value, when the page rendered, chunks of HTML which should have been written out by the VBS code after the link were missing. Eventually, I happened into a solution, but I had no idea why it worked. After much searching, I discovered that everyone else was using the same solution as me but with no explanation.
Eventually I found an answer on webmaster-talk.com from a chap called Tommy Hanks who had had the same problem and asked the same question. He discovered that the .guid method adds an ASCII 0 to the end of the string which is interpreted as a string terminator. This completely nerfs anything after it. Quite why the hell it's there, I don't know but the solution is as follows:
<%
Function CreateGUID
Dim objGuid
Dim sGUID
Set objGuid = Server.CreateObject("scriptlet.typelib")
sGUID = Left(objGuid.guid, 38)
Set objGuid = Nothing
End Function
>%
Notice the addition of the Left function to the objGuid.guid call which gives you the GUID itself and leaves off the annoying string terminator. Now, the links look like this in the source code:
<a href="http://www.somesite.com" target="<%=CreateGUID%>">A Link</a>
Which, when rendered, gives you:
<a href="http://www.somesite.com" target="{F2E09822-A527-406A-B11D-014C98B703C5}">A Link</a>
Thereby guaranteeing that each link will open in it's own window. Et voila - unique ID's in ASP.
No comments:
Post a Comment