How to find out if my page is open with Window.Open ()

I have an EventDetails.aspx page with event information that my user can open with Window.Open () from my Timetable.aspx page. On the EventDetails.aspx page, I have a button that allows me to close the window using the following code:

protected void CloseWindow_Click(object sender, EventArgs eventArgs)
{
    Response.Write("<script language='javascript'>window.open('','_self');window.close();</script>");
}

When I open my window with Window.Open () from the Timetable.aspx page, the window close button works fine, however, if I navigate the EventDetails.aspx page myself, I cannot use the close window button - nothing happens!

Is there a way to determine if a user went through with Window.Open () to change the visibility of my button or, even better, is there another way to close a tab that you should not rely on using Window.Open ()?

+4
source share
1 answer

You can transfer data to an open window so that in open windows you can perform a js check for this data, and you can hide or show the buttons according to the data transferred

// Store the return of the `open` command in a variable
var newWindow = window.open('example.com','_self');

// Access it using its variable
newWindow.my_special_setting = "Hello World";

In the window that opens, you can check the data, for example, the code below

window.my_special_setting

Added A JQuery Fiddle To Confirm It Will Work With '_self'

<!DOCTYPE html>
<html>
<body>

<p>Click the button to open a new browser window.</p>

<button onclick="myFunction()">Try it</button>

<script>

window.open("http://www.w3schools.com","_self");

</script>

</body>
</html>
Run codeHide result
+2
source

All Articles