Pages

Thursday, March 24, 2011

Asp:Hyperlink : javascript: window.open and Target=_Blank

The forgotten basic of web development: "To open link in the new window do I need to use Target element in asp:hyperlink or should I use JavaScript ?"

Page1.aspx
<asp:HyperLink runat="server" ID="l1" Text="l1" Target="_blank"/>
 <asp:HyperLink runat="server" ID="l2" Text="l1" Target="_blank"/>
The result: Click on the first link will open the new window. Click on the second linwill open the url  in the previous opened window.

Page2.aspx
 <asp:HyperLink runat="server" ID="l1" Text="l1" onclick="javascript:window.open(this.href);return false;"/>
 <asp:HyperLink runat="server" ID="l2" Text="l1" onclick="javascript:window.open(this.href);return false;"/>
The result: Click on the first link will open a new window. Click on the second link will open the second new window. Every click will open a new window.

 P.S.
     One more basic -  Why we need set "return false;? - JavaScript Onclick Return False  - Anywhere you put an onclick event with a javascript call, if you add return false then the default behavior (what would have happened without the onclick event) will be disregarded.
   ...and don't forget to put ; in the end.
    





No comments:

Post a Comment