Asp: hyperlink

I use the asp: hyperlink button to open the Terms and Conditions popup. The code for the hyperlink is

<asp:HyperLink ID="HyperLink4" Target="_blank" NavigateUrl="javascript:window.open('test.aspx');" ForeColor="#F58022" runat="server">Terms and Conditions</asp:HyperLink> 

When I click this URL in the browser, it opens my page test.aspx But along with test.aspx; it opens another page and the page URL is "JavaScript: window.open ('test.aspx');" On the body of this unwanted page is [object].

Could you suggest me how to get rid of this unwanted page.

thanks

+4
source share
5 answers

Usage: -

 <asp:HyperLink ID="HyperLink4" Target="_blank" NavigateUrl="javascript:window.open('test.aspx'); return false;" ForeColor="#F58022" runat="server">Terms and Conditions</asp:HyperLink> 

Strike>

The problem is that window.open returns a window object. One of the goals of the javascript: protocol javascript: was to allow javascript code to generate HTML content, which is the expression following the protocol. Then you are taken to a new page containing this HTML code.

In your case, because you have Target = "_ blank", a new page opens, and the object is returned by your expression (new window, open window.open), calls its toString () method, and this is what maps to it additional window.

Edit

I hit the code because it does not work. The correct solution is provided by silky . However, I am not deleting the answer, because the explanation of what is happening in the interrogation code is worth it. Therefore, the solution is valid: -

 <asp:HyperLink ID="HyperLink4" href="#" onclick="window.open('test.aspx'); return false;" ForeColor="#F58022" runat="server">Terms and Conditions</asp:HyperLink> 

Target no longer needed, it is not used. It remains as a HyperLink control, as there may be other reasons that the OP needs this as a control on the page.

+5
source

Is there a reason you need to use the HyperLink control?

Instead, you can simply use the standard HTML link (or HtmlAnchor ) and use the client side onclick event to trigger JavaScript:

 <a id="HyperLink4" runat="server" href="test.aspx" target="_blank" onclick="window.open('test.aspx');return false;" style="color:#F58022">Terms and Conditions</a> 
+4
source

Do it:

 NavigateUrl="javascript:window.open('test.aspx'); return false;" 

Best practice, however, is to put this in an OnClientClick

 NavigateUrl="#" OnClientClick="window.open('text.aspx'); return false" 

- change:

 <asp:LinkButton ID="HyperLink4" Target="_blank" NavigateUrl="#" OnClientClick="window.open('text.aspx'); return false" ForeColor="#F58022" runat="server">Terms and Conditions</asp:LinkButton > 

Updated for comments.

+2
source
  <asp:HyperLink ID="HyperLink4" Target="_blank" NavigateUrl="javascript:void window.open('test.aspx');" ForeColor="#F58022" runat="server">Terms and Conditions</asp:HyperLink> 
+1
source

remove Target = _Blank, you do not need it, javascript opens a new window already ...

Tip: Know why it works differently, view the source code in a browser, and verify that the web control creates in HTML expressions.

0
source

All Articles