Operate the parent window object in modaldialog
1, Cannot be usedWindow. Parent
Window. ParentIs Used inFrameIn the dialog box, it cannot be used to operate the parent window object.
2And correct practices
CallModaldialogBy passing Parameters
Example:
Requirement
The parent window isA.htmlThe subwindow page isB .html.A.htmlText Box inIDIsTest1In the displayed dialog box, clickA.htmlTo "subwindow value ".
Implementation
When the dialog box is openedTest1As a parameter to the subwindow, get the parameter in the subwindow, and set the parameter object (that isA.htmlPassed inTextObject)ValueSet the property value to "subwindow value"
Note: OnlyID, Cannot be passedName
A.htmlCodeAs follows:
<HTML>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312">
<Title> a.html </title>
</Head>
<Body>
<Input type = text id = test1 value = ''>
<Input type = button value = "OK" onclick = 'window. showmodaldialog ("B .html", test1) '>
</Body>
</Html>
B .htmlThe Code is as follows:
<HTML>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312">
<Title> B .html </title>
<Script language = JavaScript>
Function func1 (){
// Obtain the parameters passed by the parent window
VaR ptextid = Window. dialogarguments;
If (ptextid! = Undefined ){
// Change the value of the object passed in the parent window to "Child Window value"
Ptextid. value = "subwindow value ";
// Close the subwindow
Window. Close ();
}
}
</SCRIPT>
</Head>
<Body>
<Input type = button value = "OK" onclick = func1 ()>
</Body>
</Html>
If you want to operate on a large number of parent window objects, you can alsoWindowOr upload the parameter Doc ument as a parameter to the subwindow.
Example:
Requirement
A.html Add ID For AForm " Form , Form In ID Is Test2 In B .html In addition to the above operations Test2 To "subwindow value ". 2 ", And Form Submit C.html .
Implementation1
SetA.htmlIn the dialog box, the function is changed to the following method::
Window. showmodaldialog ("B .html", too many Doc ument);
SetB .htmlMediumFunc1 ()As follows::
Function func1 (){
VaR pdoc = Window. dialogarguments;
If (pdoc! = Undefined ){
Pdoc. All. test1.value = "subwindow value ";
Pdoc. All. test2.value = "subwindow value 2 ";
Pdoc. All. AForm. Action = "c.html ";
Pdoc. All. AForm. Submit ();
}
}
Implementation2
Because there are many operations on the parent window in the Child Window, execScript can also be used.
SetA.htmlIn the dialog box, the function is changed to the following method::
Window. showmodaldialog ("B .html", window);
AddJavascriptThe function is as follows:
Function func (){
Test1.value = "subwindow value ";
Document. All. test2.value = "subwindow value 2 ";
AForm. Action = "c.html ";
AForm. Submit ();
}
SetB .htmlMediumFunc1 ()As follows::
Function func1 (){
VaR pwin = Window. dialogarguments;
If (pwin! = Undefined ){
VaR codestr = "func ();"
Pwin.exe cscript (codestr, "JavaScript ");
Window. Close ();
}
}
How to Use showmodaldialog and showmodelessdialog-reprint
1. What are the differences between showmodaldialog and showmodelessdialog?
Showmodaldialog: after being opened, the input focus is always maintained. You cannot switch to the main window unless the dialog box is closed. Similar to the running effect of alert.
Showmodelessdialog: after opening, you can randomly switch the input focus. It has no effect on the main window (it can be blocked at most. : P)
2. How can I leave a new window in the superconnection between showmodaldialog and showmodelessdialog?
Add <base target = "_ Self"> to the opened webpage. This sentence is generally placed between <HTML> and <body>.
3. How can I refresh the content in showmodaldialog and showmodelessdialog?
In showmodaldialog and showmodelessdialog, you cannot press F5 to refresh or bring up a menu. This can only rely on JavaScript. The following is the relevant code:
<Body onkeydown = "If (event. keycode = 116) {reload. Click ()}">
<A id = "reload" href = "filename.htm" style = "display: none"> reload... </a>
Replace filename.htm with the webpage name and place it in the webpage you opened. Press F5 to refresh the page. Note that this should be used with <base target = "_ Self">, otherwise press F5 to bring up a new window.
4. How to use JavaScript to turn off the window opened by showmodaldialog (or showmodelessdialog.
<Input type = "button" value = "close" onclick = "window. Close ()">
Also use <base target = "_ Self">. Otherwise, a new ie window will be opened and closed.
5. Data Transmission skills of showmodaldialog and showmodelessdialog.
(Author's note: I wanted to write it in the form of a question and answer, but I couldn't figure out how to ask this, so I had .)
This is troublesome. I have changed it several times, but I cannot explain it in the white space (the language level is getting worse and worse). I have to explain it in an example.
Example:
Now you need to read or set a variable var_name in a showmodaldialog (or showmodelessdialog ).
General transfer method:
Window. showmodaldialog ("filename.htm", var_name)
// Pass the var_name variable
When showmodaldialog (or showmodelessdialog) is read and set:
Alert (window. dialogarguments) // read the var_name variable
Window. dialogarguments = "oyiboy" // set the var_name variable
This method can be met, but what if you want to operate var_name and then change var_id? You cannot perform the operation again. This is the limitation of this transfer method.
The following is the recommended transfer method:
Window. showmodaldialog ("filename.htm", window)
// No matter what variables you want to operate, only the window object of the main window is passed directly.
When showmodaldialog (or showmodelessdialog) is read and set:
Alert (window. dialogarguments. var_name) // read the var_name variable
Window. dialogarguments. var_name = "oyiboy" // set the var_name variable
You can also operate the var_id variable.
Alert (window. dialogarguments. var_id) // read the var_id variable
Window. dialogarguments. var_id = "001" // set the var_id variable
You can also operate any object in the main window, such as the elements in the form object.
Window. dialogarguments. form1.index1. value = "this is the value of the index1 element"
6. Multiple showmodelessdialog operations.
Because it is very hard to say, I am just a little lazy and use the Code directly. If I don't understand it, I will send a letter directly (oyiboy # 163.net( change # To @ when using IT @)) ask me.
The main function of the following code is to move another showmodelessdialog location in a showmodelessdialog.
Some JS Code of the main file.
VaR s11_showmodelessdialog('control .htm', window, "dialogtop: 1px; dialogleft: 1px") // open the control window
VaR S2 = showmodelessdialog ('about: blank ', window, "dialogtop: 200px; dialogleft: 300px") // open the controlled window
control some code of .htm.
The above key parts are:
Window naming method: var s11_showmodelessdialog('control .htm', window, "dialogtop: 1px; dialogleft: 1px ")
Variable access method: window. dialogarguments. s2.dialogtop
This example is just about the location operation between showmodelessdialog and showmodelessdialog. Through this principle, the showmodelessdialog controls each other's display pages and transmits variables and data. It depends on your efforts.