Skip navigation.

HttpModule And HttpHandler Template Generator In BetaAll recent postsMicrosoft Press-UK Got Hax0r3d

Accessible Hyperlinks In ASP.NET

I wonder if there's anyone out there who enjoys pop-ups. Pop-ups have become a nuisance of the same magnitude as spam, except that everyone has learned to tame them. Google has a blocker. MSN has a blocker. WinXP SP2 adds a popup blocker of its own to the Internet Explorer. Both Opera and Mozilla Firefox have built-in pop-up blockers.

Besides being a pain in the neck pop-ups cause a couple of common problems:

  • They mess up the browser's Back button. Everybody is used to using the Back button. Give it back to them.
  • They make matters even worse for blind surfers because they might not realize a new window just popped up.

Following are the three most common approaches to opening new windows:

  1. <a href="javascript:window.open (...)"
  2. <a href="..." target="..."
  3. <a href="..." onclick="window.open (...)"

Let's take a closer look at each one an talk about its pros and cons.

javascript:window.open

This one is the worst. Links rendered this way violate accessibility rules. Here's why:

  • Text-based browsers, such as Lynx, don't support JavaScript so your link is a waste for them.
  • According to www.thecounter.com 11% of people have JavaScript turned off for one reason or another. Again, to them your link is a waste.
  • Google, the largest blind user in the world, doesn't index JavaScript and JavaScript-based links. Therefore it won't follow them.
  • You can't bookmark this link.
  • You can't open the link in a new window yourself. You're stuck with one choice and once choice only.

I wanted to also list something as an advantage of this approach, but I can't think of one.

target=_something

Not as bad but still not good enough. Reasons?

  • In the XHTML 1.0 standard the target attribute is deprecated. Therefore your pages will not validate against XHTML Strict. It is guaranteed this attribute will be solid gone in the future. The target attribute is still there in the XHTML Transitional DTD, but you're really indulging yourselves.
  • The new window still doesn't have history and therefore the Back button is not operational.

onclick=window.open

This one is the lesser evil. Aaron Boodman suggested a really neat solution:

<a href="http://google.com/"
 onclick="window.open(this.href, 'popupwindow', 
'width=400,height=300,scrollbars,resizable'); 
return false;">Link text goes here<a>

The advantages of these links are many:

  1. You can simply follow them.
  2. You can choose to open them in a new window by right-clicking.
  3. You can bookmark them.

Hyperlinks in ASP.NET

To take Aaron's code a step further let's build a simple ASP.NET hyperlink control.

<asp:Hyperlink Runat="server" NavigateUrl="http://google.com/"
onclick="window.open (this.href, 'popupwindow', 
'width=400,height=300,scrollbars,resizable'); 
return false;">Link text goes here<asp:Hyperlink>

You won't see the onclick event in IntelliSense, but don't worry—it'll get rendered fine.

Conclusion

Whichever option you implement remember this much: you are robbing a user of choice! It's up to the user, not the designer, to open a link in a new window. If you absolutely have to use a pop-up window give ample warning.

Comments

Comment permalink 1 Praphulla |
Actually i have been searching for this type of performance better code
I used the same code in a user control.But when i click on the link, its just redirecting to the new page but not getting popped in a new window with the expected window height,width

What might be the problem here

Thanks in advance
Comment permalink 2 Milan Negovan |
I can think of only two things: pop-up blocker and disabled JavaScript.
Comment permalink 3 Indrani |
Sir,
when we click on the properties of hyperlink, and click on navigate url link, then after selecting a file from the browse button, its taking the path starting with file:///c:\abc.txt ..... some thing like this. when we run the application and click on hyperlink, that particular file is not opening, the reason may be since its not http://...But the problem this problem is in windows XP with SP2, so whats the solutions for that problem, when the option for file:/// is given , it should as well work. Please help me in this matter.
thank you
Comment permalink 4 Milan Negovan |
Windows treats protocols (http://, file///, etc) differently. There's always an application registered with each "pluggable protocol". The file:/// protocol gets a whole different treatment than http://. My gut feeling tells me you can't pop up a window with the file:/// protocol.
Comment permalink 5 John Yoder |
Great article. I'm a very seasoned programmer but new to the .net world. The window.open option was just what I was looking for!
Comment permalink 6 dwight rau |
the original post was very useful, thanks.
this.hlImageNLink.Attributes.Add("onclick","window.open (this.href, 'popupwindow', 'width=700,height=500,scrollbars,resizable');return false;");
Comment permalink 7 Brad Burns |
I am a seasoned .NET developer, and still, I have to say that it's insight like this, into the small intricacies that makes blogs like this worth reading and coming back to.

Well done. :)

~Brad
Comment permalink 8 Andres C. |
I agree that the article was very insightful, and it also provided me the solution I was looking for.
I should also say that Microsoft should have taken situations like this into account and make it easier to create links that open a new window and where you can control the new window's properties. I would imagine it is a common practice to do this, and doing it through the GUI would make more sense than having to search through the Net for a solution.
Comment permalink 9 Sig Handelman |
Thanks very much, this works. I am now trying to communicate from the click back to my code-behind. Any hints on how I should write a routine in the little Jscript inside the ASP of this example.
Comment permalink 10 Jason |
If you are using databinding in ASP.NET 2.0, you have to use this syntax or the page won't build.



Apparently you have to use the single tick (') around the navigateUrl for it to work.
Comment permalink 11 Jason |
Apparently the code didn't show up. The syntax should look like this (without the spaces):

< asp:Hyperlink ID="hyperlink1" Runat="server" NavigateUrl='< %# Eval("IDField", "~/mypage.aspx?id={0}") % >' onclick="window.open (this.href, 'popupwindow', 'width=400,height=300,scrollbars,resizable'); return false;" Text='< %# Eval("DisplayField") % >' >< /asp:Hyperlink >
Comment permalink 12 Shamaa |
Didnt know that about Target. Thanks for the tip. Back to good 'ol coding...
Comment permalink 13 jatin |
Respected Sir,

There is problem with the asp.net hyperlink control in windows xp, to click on hyperlink for open any file like doc,pdf,txt , any file should not open so please help and solved this problem.

problem with hyperlink like file///d:/abc.doc not any response
Comment permalink 14 Prashant |
There is problem with the asp.net hyperlink control in windows xp, to click on hyperlink for open any file like doc,pdf,txt , any file should not open so please help and solved this problem.
Comment permalink 15 pallavi |
i need to open a website in a different window(popupwindow) from a asp.net application not on click of the hyperlink but on page load..on page load the required webpage shud open in the popup window..is it possible to do?
please help me in this matter..
Comment permalink 16 Milan Negovan |
You can put the following code in PageLoad:

Page.ClientScript.RegisterStartupScript (
this.GetType (),
"Show popup",
"window.open (...);",
true);
Comment permalink 17 pallavi |
thank u milan...its working with some small modifications..
thanx
Comment permalink 18 ThirstyMind |
This was a great tip. Thanks. I created a site in asp.net. Wile the popup worked fine in IE, it opened the window within the parent in FF. After this suggestion in both the browsers, it behaves as expected.
Comment permalink 19 Jef Claes |
Great article!
Comment permalink 20 Jon |
Just what I needed! Thanks!
Comment permalink 21 smd |
i need to open new window when i click on a link.but if there is link i dont need a blank popup window to open.behind the name of my column there is the url id..so if there is an url on clicking a new windown opens..if there is no url it shudnt open...pls advice


onclick="window.open (this.href, 'popupwindow',
'left=600,width=500,height=500,scrollbars,resizable');
return false;"
Text='<%# Eval("Name") %>' ToolTip='Click Here'>

Emails and Notifications

Would you like to be notified when somebody responds to this post?  Would you like to have these comments emailed to you?

Submit your comment

Please enter only text since all HTML tags except hyperlinks will be stripped. Hyperlinks will become live links. Any comments with flaming or offensive language will be deleted. Be courteous to other posters. Thank you.

Your name (required):
Your email (optional):
Your site's URL (optional):
Enter this number
Type in the number above:
Comment (required):