简体   繁体   English

通过JavaScript从子窗口更改父窗口onclick值

[英]Change Parent Window onclick Value From Child Window Via JavaScript

Setup: 设定:

I'm opening a new window via js that is a classic asp page with one form (yes, classic asp, I know). 我正在通过js打开一个新窗口,这是具有一种形式的经典asp页面(是的,我知道经典asp)。 That form posts to a processing page where I Response.Write() some inline js to manipulate form elements on the parent page like so. 该表单将发布到处理页面,在该页面中,我Response.Write()会使用一些内联js来操作父页面上的表单元素,如下所示。

.Write ("<script language=""javascript"">" & vbcrlf)    
.Write ("var stopButton = opener.document.getElementById(""timerStop"");" & vbcrlf)
.Write ("stopButton.style.display = ""inline"";" & vbcrlf)
.Write ("window.close();" & vbcrlf)
.Write ("</script>" & vbcrlf)

Problem: 问题:

All of the above works fine, when I try to change the onclick value of the stopButton element: 当我尝试更改stopButton元素的onclick值时,以上所有方法均能正常工作:

.Write ("<script language=""javascript"">" & vbcrlf)
.Write ("var stopButton = opener.document.getElementById(""timerStop"");" & vbcrlf)
.Write ("stopButton.style.display = ""inline"";" & vbcrlf)
**.Write ("stopButton.onclick = function () {alert(""test"");};" & vbcrlf)**
.Write ("window.close();" & vbcrlf)
.Write ("</script>" & vbcrlf)

And then click on the button after the window closes I get an error in FF and nothing happens in IE: 然后在窗口关闭后单击按钮,我得到了FF错误,而IE中没有任何反应:

uncaught exception: [Exception... "Component returned failure code: 0x80004005 (NS_ERROR_FAILURE) [nsIDOMWindowInternal.alert]" nsresult: "0x80004005 (NS_ERROR_FAILURE)" location: "JS frame :: /timer/activity_proc.asp?a=start :: anonymous :: line 8" data: no] 未捕获的异常:[异常...“组件返回失败代码:0x80004005(NS_ERROR_FAILURE)[nsIDOMWindowInternal.alert]” nsresult:“ 0x80004005(NS_ERROR_FAILURE)”“位置:” JS框架:: /timer/activity_proc.asp?a=start: :匿名:: 8行:数据:否]

Is this because the child window no longer exists? 这是因为子窗口不再存在吗? I should note that the button does have an onclick value prior to me attempting to change its value. 我应该注意,在尝试更改按钮值之前,该按钮确实具有onclick值。

I think this is because the child window no longer exists. 我认为这是因为子窗口不再存在。

There is a way of creating functions in javascript that's called closures. 有一种在javascript中创建函数的方法,称为闭包。 Essentially, a function " belongs " or " is stored " in the content where it's declared. 本质上,函数“ ”或“ 存储 ”在声明它的内容中。 In this case, on the child window. 在这种情况下,在子窗口上。

If you have a function that does the work on your parent window and call that instead of assigning directly, it should work. 如果您有一个可以在父窗口上工作的函数,然后调用它而不是直接分配,那么它应该可以工作。 ie: 即:

on the parent document: 在父文档上:

assignStopButtonListener = function(){
    var stopButton = document.getElementById(""timerStop"");
    stopButton.style.display = "inline";
    stopButton.onclick = function () {alert("test");};
}

on your popup document 在您的弹出式文件中

.Write ("<script language=""javascript"">" & vbcrlf)
.Write ("opener.assignStopButtonListener();" & vbcrlf)
.Write ("</script>" & vbcrlf)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM