Javascript 从html页面内部更新父变量
我试图更新html页面中的父Javascript 从html页面内部更新父变量,javascript,jquery,html,google-chrome-extension,parent-child,Javascript,Jquery,Html,Google Chrome Extension,Parent Child,我试图更新html页面中的父var a,我正在硬编码,然后在外部向它发出警报,但值没有改变。 这在我的content.js中 var a; function popUpWindow() { a="Nothing"; alert(a);// Displays "Nothing" myWindow = window.open("Accept", "myWindow", "width=450, height=300"); myWindow.document.write(
var a
,我正在硬编码,然后在外部向它发出警报,但值没有改变。
这在我的content.js中
var a;
function popUpWindow() {
a="Nothing";
alert(a);// Displays "Nothing"
myWindow = window.open("Accept", "myWindow", "width=450, height=300");
myWindow.document.write(
"<html>"+
"<head>"+
"<script> function closeWindow(){parent.a =\"Something\"; alert(\"a has been updated\"); window.close();}</script>"+
"</head>"+
"<body>"+
"<form>"+
"<div id=leadinformation>"+
"<p id=titleParagraph>You are about to create a new Lead:</p>"+
"First Name....."+ "<input type=text id=firstname value="+firstname+">" +"<br>"+
"Last Name....."+ "<input type=text id=lastname value="+lastname+">" +"<br>"+
"Title:..............."+ "<input type=text id=position value="+positions[0]+">" +"<br>"+
"Company:......"+ "<input type=text id=company value="+companies[0]+">" +"<br>"+
"Email:............"+ "<input type=text id=email value="+email+">" +"<br>"+
"Phone:..........."+ "<input type=text nid=phonenumber value="+phonenumber+">" +"<br>"+"<br>"+
"<div>"+
"<button id=Accept onClick=closeWindow() >Accept</button>"+
"<button id=Close onClick=closeWindow() >Close</button>"+
"</form>"+
"</div>"+
"</body>"+
"</html>"
);
myWindow.document.getElementById('Accept').onclick=addToLeads;
myWindow.document.getElementById('Close').onclick=Close;
}
function Close() {
alert(a);
//Displays "Nothing" (Should have been "Soomething")
}
var-a;
函数popUpWindow(){
a=“没有”;
警报(a);//显示“无”
我的窗口=窗口。打开(“接受”,“我的窗口”,“宽度=450,高度=300”);
myWindow.document.write(
""+
""+
“函数closeWindow(){parent.a=\'Something\'警报(\'a已更新\');window.close();}”+
""+
""+
""+
""+
“您将要创建一个新的潜在客户:
”+
“名字…”“+”“+”
”+
“姓氏…”“+”“+”
”+
“标题:……”+“+”
”+
“公司:……”+“+”
”+
“电子邮件:………”+”“+”
”+
“电话:…………”+“+”
“+”
”+
""+
“接受”+
“结束”+
""+
""+
""+
""
);
myWindow.document.getElementById('Accept')。onclick=addToLeads;
myWindow.document.getElementById('Close')。onclick=Close;
}
函数关闭(){
警报(a);
//显示“无”(应为“烟熏”)
}
在html页面中,我将父.a
设置为“某物”,我将警报(a)
设置为显示“某物”。然后我在
Close()
函数中再次发出alert(a)
,该函数位于html页面之外,但现在alert(a)
再次显示“Nothing”
您知道这可能是什么原因吗?在子函数中将
父函数a
更改为a
:
"<head>"+
"<script> function closeWindow(){a =\"Something\"; alert(\"a has been updated\"); window.close();}</script>"+
"</head>"+
“”+
“函数closeWindow(){a=\'Something\'alert(\'a已更新\');window.close();}”+
""+
您没有创建iframe,因此我认为这里的父项是错误的。请尝试使用window.opener.a
。处于警报状态(“a已更新”);被解雇?是的,它被解雇了;我随后检查了警报(a),并将其设置为“Smthing”,这很好,但在页面之外,它返回为“Nothing”。此代码是否包含在另一个函数中,还是在全局范围内?popUpWindow()当用户单击我之前创建的按钮并将其插入当前DOM时,会触发函数,所以您的弹出窗口尝试从内容脚本调用函数?这些是孤立的上下文,因此无法工作。我现在尝试更改它,但效果与以前完全相同。我尝试使用您的建议,但效果与以前相同。如果我在设置其值之前发出警报(windoe.opener.a),它将显示“未定义”:(