views:

595

answers:

6

Hi,

I am using asp:hyperlink button to open a Terms and Condition pop up window. 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 browser then it opens up my test.aspx page But along with test.aspx; it opens up another page and the url of page is - "javascript:window.open('test.aspx');" On the body of this unwanted page - [object].

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

Thanks

+2  A: 

Make it:

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

Better practice, however, is to put this in OnClientClick

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

-- edit:

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

Updated per comments.

Noon Silk
I don't think the `HyperLink` control has an `OnClientClick` event/property, but the point about using the client-side `onclick` event rather than the `href` is a good one.
LukeH
NavigateUrl="javascript:window.open('test.aspx'); return false;"not working....?
Muhammad Akhtar
your provided both solution not working, Plz check
Muhammad Akhtar
Noon Silk
+2  A: 
AnthonyWJones
I have tested, its not working??
Muhammad Akhtar
plz check my solution
Muhammad Akhtar
Silky provided solution also not working.
Muhammad Akhtar
It's good practice to provide the `href` and `target` properties anyway, in case JavaScript isn't enabled.
LukeH
@Luke: why provide a target that is never used? Providing href is more than a good practice, in some browsers the anchor fails to behave as an anchor without a href.
AnthonyWJones
@Anthony: The `href` and `target` *will* be used if JavaScript is disabled. Having `href="test.aspx"` and `target="_blank"` should give the closest non-JS approximation of the JS behaviour. ie, opening `test.aspx` in a new window.
LukeH
@Luke: I guess that depends on your audience. In many intranet (and for that matter public) sites non-JS just is a non-starter and you may was handle that way earlier. Certainly if all you are doing is window.open then you may well not bother with onclick at all
AnthonyWJones
@Anthony: That's very true, but my clients usually demand that (a) users shouldn't be arbitrarily excluded from their sites for not having the "right" browser, not having JavaScript enabled etc; and (b) the sites should degrade as gracefully as possible when the user's browser isn't fully functional. The clients accept that certain things aren't possible when JS is unavailable but wouldn't be very happy if a simple link just *didn't do anything at all* with JS switched off.
LukeH
@Luke: Thats fine. The question and my answer is not addressing __your__ clients needs. However __your__ clients are do not necessarily define what is universally "best practice". "Best practice" can only be measured in the context of purpose.
AnthonyWJones
A: 
 <asp:HyperLink ID="HyperLink4" Target="_blank"
        NavigateUrl="javascript:void window.open('test.aspx');"
        ForeColor="#F58022" runat="server">Terms and Conditions</asp:HyperLink>
adatapost
+2  A: 

Is there any reason why you need to use the HyperLink control?

You could probably just use a standard HTML link (or an HtmlAnchor control) instead and use the client-side onclick event to fire your 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>
LukeH
A: 

plz try using this...

<asp:HyperLink ID="HyperLink4" NavigateUrl="javascript://" onclick="javascript:window.open('test.aspx');"
        ForeColor="#F58022" runat="server" Text="Terms and Conditions"></asp:HyperLink>
Muhammad Akhtar
onclick should return false in order to cancel navigation and javascript: prefix superflous in the onclick event.
AnthonyWJones
A: 

remove Target=_Blank, you dont need it, the javascript opens a new window already...

Tip: to know why it acts differently, view source in the browser and check what the web control produces in HTML terms

Ayyash