以编程方式确定窗口是独立的还是孤立的 [英] Programmatically determine whether a window is standalone or orphaned

查看:111
本文介绍了以编程方式确定窗口是独立的还是孤立的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要弄清楚窗口是否作为独立窗口打开,而不是它是孤立的。让我说明一下我的意思。

I have a need to figure out whether a window was opened as a standalone vs. whether it was orphaned. Let me illustrate what I mean.

假设我有两个ASP页面,我将其称为PARENT.asp和CHILD.asp。此外,还有一个重定向页面,我将其称为CHILD-REDIRECT.asp。

Let's say I have two ASP pages that I'll call PARENT.asp and CHILD.asp. Additionally, there is a redirect page that I'll call CHILD-REDIRECT.asp.

单击CHILD.asp上的某个按钮时,它会重定向到CHILD-REDIRECT .asp,包括需要以某种方式运行的客户端Javascript代码,具体取决于它是否具有父代(基于window.opener == null)。为了便于说明,我会说Javascript代码是一个布尔变量,我称之为IsOrphan。

When a certain button on CHILD.asp is clicked, it redirects to CHILD-REDIRECT.asp, which includes client-side Javascript code that needs to run a certain way, depending on whether or not it has a parent (based on window.opener == null). For sake of illustration, I'll say the Javascript code is a Boolean variable that I'll call IsOrphan.

所以,我打开一个新的浏览器窗口然后导航到CHILD.asp。然后,我单击重定向到CHILD-REDIRECT.asp的按钮,然后启动Javascript。因为我将其作为独立页面打开,IsOrphan = FALSE。

So, I open a new browser window and I navigate to CHILD.asp. I then click the button that redirects to CHILD-REDIRECT.asp and kicks off the Javascript. Because I opened it as a standalone page, IsOrphan = FALSE.

现在,我打开一个新浏览器并导航到PARENT.asp。此页面包含一个window.open调用,在新窗口中打开CHILD.asp。当CHILD.asp打开时,我关闭PARENT.asp,以便CHILD.asp现在处于孤立窗口。所以现在当我点击按钮并且页面重定向到CHILD-REDIRECT.asp时,我需要IsOrphan = TRUE。

Now, I open a new browser and navigate to PARENT.asp. This page includes a window.open call that opens CHILD.asp in a new window. While CHILD.asp is open, I close PARENT.asp so that CHILD.asp is now in an orphaned window. So now when I click the button and the page redirects to CHILD-REDIRECT.asp, I need IsOrphan = TRUE.

有关如何执行此操作的任何想法?我正在寻找通过CHILD.asp onload事件上的window.opener为父设置变量;然而,由于这些页面的复杂性,这并不像听起来那么容易。就目前而言,Javascript检查window.opener以启动代码,但当然,它不知道独立页面和孤立页面之间的区别。我希望那里有人知道一个好的/更好的方法来做到这一点。

Any thoughts as to how to do this? I'm looking into setting a variable for the parent via window.opener on the CHILD.asp onload event; however, because of the complexity of these pages, this isn't as easy as it sounds. As it stands now, the Javascript checks window.opener to kick off the code, but of course, it doesn't know the difference between a standalone page and an orphaned page. I'm hoping that someone out there knows of a good/better way to do this.

提前感谢您的帮助。

推荐答案

我让这个工作了。

在我的CHILD.asp页面中,我设置了以下内容:页面顶部:

In my CHILD.asp page, I set up the following at the top of the page:

<script>
   var isChild;
   if (window.opener == null) { isChild = false } else { isChild = true }
</script>

这会在页面加载时实例化isChild客户端变量。

This instantiates the isChild client-side variable when the page loads.

我的服务器端经典ASP代码包含如下内容:

My server-side classic ASP code contains something that looks like this:

response.write("javascript: if (1=1) { window.location.href='CHILD-REDIRECT.asp?ID=" & someID & "';")

我必须承认我的一个挂断是如何将其发送到服务器端代码。然而,我意识到这将是一个没有实际意义的点,因为客户端值应该在呈现时存在。

I have to admit that one of my hangups was how to send this to the server-side code. However, I then realized that this would be a moot point, because the client-side value should be there when it rendered.

所以,我重写了这样:

response.write("javascript: if (1=1) { window.location.href='CHILD-REDIRECT.asp?ID=" & someID & "&isChild=' + isChild;")

在CHILD-REDIRECT.asp,我补充说:

In the CHILD-REDIRECT.asp, I added:

var boolChild = <%request.QueryString("isChild")%>

现在,我有一个标志,指示页面是否由父母生成。稍后在代码中,我有这个:

So now, I have a flag that indicates whether or not the page was generated by a parent. Later in the code, I have this:

if (window.opener == null && boolChild) { 
   // do something 
}

果然,它正在做我需要的东西做。

Sure enough, it's doing what I need it to do.

这篇关于以编程方式确定窗口是独立的还是孤立的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆