我正在尝试更新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(
"<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")
}
在html页面中,我将
parent.a
设置为“Something”,然后alert(a)
显示“Something”。然后,我再次在
alert(a)
函数内部Close()
,它在html页面外部,但现在alert(a)
再次显示“Nothing”。你知道这是什么原因吗?
最佳答案
将子函数中的parent.a
更改为a
:
"<head>"+
"<script> function closeWindow(){a =\"Something\"; alert(\"a has been updated\"); window.close();}</script>"+
"</head>"+