hollyquinn hollyquinn - 1 year ago 102
Javascript Question

Detect if user opens link in new tab and redirect to new url

I am trying to do something that I'm not sure is possible or not?

The user opens the website. If they try to reopen the link in a new tab I need to redirect them to another page.

I've tried using

to see if I can detect a page reload. This works, but if I put my code in the page load event for some reason it runs twice, and it redirects them no matter if it's the first time they opened or if they are opening in a new tab. I need to only happen if they open a new tab.

My Code:

bool IsPageRefresh = false;
if (!IsPostBack)
ViewState["ViewStateId"] = System.Guid.NewGuid().ToString();
Session["SessionId"] = ViewState["ViewStateId"].ToString();
if (ViewState["ViewStateId"].ToString() != Session["SessionID"].ToString()) ;
IsPageRefresh = true;
if (IsPageRefresh == true)
Response.Redirect("~/Pages/EstimateList.aspx", true);
Session["SessionId"] = System.Guid.NewGuid().ToString();
ViewState["ViewStateId"] = Session["SessionId"].ToString();

Do I need to use
to do this? I'm really new to

Answer Source

You can use cookies. When the user first open the page you send a cookie to the computer saying PageOpened=true , when the user opens it in the new tab check if there is a cookie with PageOpened=true, if there is, open your other link in the browser.

You can give your cookies a time limit or destroy them when the first site is closed or when the user opens the new tab etc..

I can't provide you the code as I never worked with c# on web but this should give you an idea on possible routes to the solution.

P.S.: I would also find it irritating as a user but that is your call.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download