Suja Shyam
Suja Shyam

Reputation: 971

Window opened using window.open reopens when browser back button is clicked

I created an asp.net web application which has a linkbutton and hyperlink in the default.aspx. Hyperlink is set navigationurl -"www.google.com". Linkbutton opens the same url in a new tab using javasript's window.open.

Default.aspx

    <asp:LinkButton ID="LinkButton1" runat="server" onclick="LinkButton1_Click">Data</asp:LinkButton><br />
<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl="http://www.google.co.in">google</asp:HyperLink>

Default.aspx.cs

protected void LinkButton1_Click(object sender, EventArgs e)
    {
       string url = "http://www.google.com";
        ScriptManager.RegisterStartupScript(Page, typeof(Page), "OpenWindow", "window.open('" + url + "');", true);
    }

Steps to reproduce my query: 1. Clicks LinkButton. This opens google in new window/tab. 2. Click Hyperlink. This navigates to google. 3. Click Browser Back button. This time the browser navigates back to default.aspx, simultaneously google opens in new window/tab.
T want this not to happen.

Upvotes: 0

Views: 1239

Answers (3)

Sid M
Sid M

Reputation: 4364

Add this codefunction in your head tag

<script type="text/javascript">
        function myfun() {

            window.open("http://www.google.co.in");
            return false;
        }
    </script>

and in your body

<asp:LinkButton ID="LinkButton1" runat="server" OnClientClick="return myfun();">Data</asp:LinkButton><br />
<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl="http://www.google.co.in">google</asp:HyperLink>

Note: Avoid any request to server until and unless its not required,you can also do the same with the help of <a> tag by keeping target="_blank"

Upvotes: 0

user3036372
user3036372

Reputation: 24

here's the code: add the code to the page which you don't want the user to return using back.

if(history.length>0) history.go(+1);

call it on body load

basically i increment the browser history.

Upvotes: 0

LadyKiller
LadyKiller

Reputation: 1

Use Hyperlink Button instead of link button and use target="_blank" and url ="www.google.com" this will open url in new window
No need to use java script to open a new Window .... Also It will Resolve Your Problem

<asp:HyperLink ID="HyperLink2" Target="_blank" NavigateUrl="http://www.google.co.in" runat="server"  >Data</asp:HyperLink><br />

<asp:HyperLink ID="HyperLink1" Target="_self" runat="server" NavigateUrl="http://www.google.co.in">google</asp:HyperLink>

Upvotes: 0

Related Questions