如何判断我的页面是否使用Window.Open()打开

时间:2016-03-31 21:06:07

标签: javascript c# asp.net

我有一个EventDetails.aspx页面,其中包含我的用户可以使用我的Timetable.aspx页面中的Window.Open()打开的事件的详细信息。在EventDetails.aspx页面上,我有一个按钮,允许我使用以下代码关闭窗口:

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

当我从Timetable.aspx页面打开带有Window.Open()的窗口时,关闭窗口按钮工作正常,但是如果我独立导航到EventDetails.aspx页面,我无法使用关闭窗口按钮 - 没有任何反应!

有没有办法可以确定用户是否已使用Window.Open()进行导航,以便我可以更改按钮的可见性,或者更好的是,还有另一种方法可以关闭不依赖于我的选项卡使用Window.Open()?

1 个答案:

答案 0 :(得分:2)

您可以将数据传递到打开的窗口,这样在打开的窗口中您可以检查js中的数据,您可以根据传递的数据隐藏或显示按钮

window.my_special_setting

在打开的窗口中,您可以检查数据,如下面的代码

<!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>

添加了一个Jquery小提琴以确认它将与'_self'一起使用

    jQuery.fn.scrollTo = function (elem) {
        var b = $(elem);
        this.scrollTop(b.position().top + b.height() - this.height());
    };