a.html
<form name="form1" method="post" action="">
<a href="javascript:void(null)" class="add" onClick="open('b.html','','resizable=1,scrollbars=1,status=no,toolbar=no,menu= no,width=500,height=400,left=150,top=50')">Increase</a>
<input type="text" name="text1">
</form>
b.html
<script language="Javascript" type="text/javascript">
function returnValue()
{
window.opener.document.all.text1.value=document.getElementById("returnText").value;
window.close();
}
</script>
<input type="button" name="Submit" value="Submit" onclick="returnValue();">
<input name="returnText" type="text" id="returnText">
</p>
Supplement: Usage of window.opener
In general usage, window.opener is only used to solve the problem of not prompting a pop-up window when closing the window, and there is generally less understanding of its deeper understanding. In fact, window.opener refers to the window that calls the window.open method.
It is mainly used to resolve partial submissions at work. This kind of cross-page operation is very helpful for work.
If you open a page in the main window and want the main window to refresh, use this. The window.opener that opens the page is equivalent to the window of the main window.
To refresh the main window you can use
window.opener.location.reload();
If you use a virtual directory: such as struts *.do will prompt you to try again, you can change it to window.opener.yourformname.submit()
That'll be fine
2〉
There is a situation in the application,
Open window B in window A, close window B after completing operations in window B, and automatically refresh window A at the same time
function closeWin(){
hasClosed = true;
window.opener.location="javascript:reloadPage();";
window.close();
}
function window.onbeforeunload(){
if(!hasClosed){
window.opener.location="javascript:reloadPage();";
}
}
</script>
The above code will prompt an error when closing window B, saying that Object is missing. The correct code is as follows:
function closeWin(){
hasClosed = true;
window.opener.location="javascript:reloadPage();";
window.opener=null;
window.close();
}
function window.onbeforeunload(){
if(!hasClosed){//If the closeWin method has been executed, this method will not be executed.
window.opener.location="javascript:reloadPage();";
}
}
</script>
The reloadPage method is as follows:
function reloadPage() {
history.go(0);
document.execCommand("refresh")
document.location = document.location;
document.location.reload();
}
PS: Since we need to support normal closing and forced closing of windows to capture events, we use the global variable hasClosed.
==============================================
In addition, when the parent window is a frame, problems may occur when refreshing the parent window:
The page cannot be refreshed without resending the information.
Later modified as follows:
window.opener.parent.document.frames.item('mainFrame').location.href = window.opener.location.href;
There is no need to execute the built-in reload() method. Be careful not to add this sentence superfluously:
window.opener.parent.document.frames.item('mainFrame').location.reload();
================================================== ======================================
Finally, in order to support refreshing the normal parent window and the frame parent window at the same time, the code is as follows:
function closeWin() {
hasClosed = true;
<%if(null != frame){%>
window.opener.parent.document.frames.item('mainFrame').location.href = window.opener.location.href;
<%}else{%>
window.opener.location = "javascript:reloadPage();";
<%}%>
//window.opener.top.mainFrame.location="javascript:reloadPage();";
//self.opener.frames.mainFrame.location.reload(true);
window.opener = null;
window.close();
}
function window.onbeforeunload(){
if (!hasClosed) {
<%if(null != frame){%>
window.opener.parent.document.frames.item('mainFrame').location.href = window.opener.location.href;
<%}else{%>
window.opener.location = "javascript:reloadPage();";
<%}%>
window.opener = null;
}
}
Usage of window.opener
window.opener returns a reference to the window that created the current window. For example, if you click a link on a.htm to open b.htm, then we plan to enter a value on b.htm and assign it to a.htm. In a textbox with an id of "name", it can be written as:
window.opener.document.getElementById("name").value = "Input data";
Don't have a good understanding of window.opener in javascript.
Why can't it be used in a frame? Why can't the parent window of a pop-up window be on a page inside the frame? So how to operate the parent window in the frame through a pop-up window?
opener.parent.frames['frameName'].document.all.input1.value try this :)
If you want to change the page in the frame to other pages in the same frame or the page in the parent frame, use parent.
window.opener refers to the parent page of the page opened by window.open.
The window.frames object can reference pages in an iframe or a page in a frameset.
This can be done
window.frames[0].document.getElementById('xx');
This can be done
window.frames[0].document.body.innerHTML;
frm = window.parent.window.frames['uploadFrame'];
frmDocument = frm.document;
frm.sb(3); //sb is a function in the uploadFrame page
For Firefox
If you encounter an error: parent.document.frames has no PRperties
Just replace it with the following code. This code is compatible with IE and ff. frm = window.parent.window.frames['uploadFrame']; In fact, the frames collection is not hung on the document but under the window object.
Note that there are restrictions on modifying the page in the frame in this way, that is, it must be under the same domain, otherwise it cannot be accessed. If it is under the same domain, but the subdomain names are different, then add a sentence to the js and html files involved.
document.domain = xxx.com [fill in your domain name here]
document.getElementById('iframeid').contentWindow.document.getElementById('someelementid');
ask:
In the parent window window.open() a child window. and define a variable i.
Enter a value j in the sub-window and then window.opener.i=j;
This can be passed on. But I added window.close(); at the end of the sub-window and the value cannot be passed.
Please tell me if there is any way to solve this problem. Let me pass the value before closing the child window.
The code is as follows:
Parent window: parent.jsp
<script>
var i;
window.open('<%=contextPath%>/usermanage/newscontrol/cd.jsp);
</script>
<input type="button" onclick="alert(i)">
Subwindow:cd.jsp
<script>
function subm(){
window.opener.i=5;
window.close();
}
</script>
<input type="button" onclick="subm()">
Best answer you can put one in the parent window
<input id="fromChild" type="hidden" />
<input type="button"
onclick="alert(document.getElementById('fromChild').value)">
In the subwindow:
function subm(){
window.opener.document.getElementById('fromChild').value=5;
window.close();
}
This can be done
<head>
<script language=javascript>
function windowclose()
{ window.opener=null;
window.close();
}
</script>
</head>
<body>
<input id="Button1" type="button" value="button" onclick="windowclose()" />
</body>
-