[英]When passing data from parent to child with window.open, why do I get 'Cannot read property 'elements' of undefined'?
Bear with me, I've new to Javascript. 忍受我,我是Java的新手。
Here's my parent script: 这是我的父脚本:
var title = document.title;
var url = window.location.href;
var popForm;
function get_elements() {
formObject = popForm.document.forms["link_form"];
formObject.elements["id_title"].value = title;
formObject.elements["id_url"].value = url;
}
function bookmark() {
popForm = window.open("form.html","aWindow","height=500,width=400");
popForm.window.onload=get_elements()
}
And my child just look slike this: 我的孩子看起来像这样:
Title:</br>
<input id="id_title" type="text" name="title" maxlength="50" />
</p>
Description:</br>
<textarea id="id_descrip" rows="10" cols="40" name="descrip"></textarea>
</p>
Url: <input id="id_url" type="text" name="url" maxlength="100" />
</p>
<input type="submit" value="Submit" />
I'm trying to create a little 'share' bookmarklet but cannot pass the data from the parent document to the popup's elements. 我正在尝试创建一个“共享”书签,但是无法将数据从父文档传递到弹出窗口的元素。 The error in Chrome's console is
Uncaught TypeError: Cannot read property 'elements' of undefined
Chrome控制台中的错误是
Uncaught TypeError: Cannot read property 'elements' of undefined
What am I missing?! 我想念什么?!
popForm.window.onload=get_elements();
^ This is executing get_elements, not assigning it to the onload event. ^这正在执行get_elements,而不是将其分配给onload事件。
popForm.window.onload=get_elements;
^ This is how you assign a function to the onload handler, but this is something you would do in the head of a document, not as you are trying to do. ^这是将函数分配给onload处理程序的方式,但这是在文档开头执行的操作,而不是您尝试执行的操作。
popForm = window.open("form.html","aWindow","height=500,width=400");
popForm.window.onload = get_elements;
^ In this code you have a race condition. ^在此代码中,您具有竞争条件。 In some cases
popForm.window
will be loaded by the 1st line before the 2nd line is executed. 在某些情况下,
popForm.window
将在执行第二行之前由第一行加载。 The onload event will only fire once, so if you don't assign the handler before it fires, the 2nd line useless. onload事件只会触发一次,因此,如果您在触发之前未分配处理程序,则第二行无效。 In other cases
popForm.window
will not even exist yet when the 2nd line executes, making it impossible to assign a function to the onload
event handler. 在其他情况下,当第二行执行时,
popForm.window
甚至将不存在,从而无法将函数分配给onload
事件处理程序。
The normal method of handling this situation, if you have control over the contents within the child window, is to put this code inside the child: 如果您可以控制子窗口中的内容,则通常的处理方法是将以下代码放在子窗口中:
formObject = document.forms["link_form"];
formObject.elements["id_title"].value = window.opener.title;
formObject.elements["id_url"].value = window.opener.url;
If you do not have control over the contents of the child window, you need to wait until the document within the iframe has fully loaded. 如果您无法控制子窗口的内容,则需要等待直到iframe中的文档完全加载为止。 There is no way to know this from the parent except to keep checking over and over.
除了不断检查之外,没有办法从父母那里知道这一点。 Use code like below.
使用如下代码。
popForm = window.open("form.html","aWindow","height=500,width=400");
function waitForChild() {
if (! popForm.window ) setTimeout(waitForChild, 1000);
else get_elements();
}
waitForChild();
Passing a function means not calling it: 传递函数意味着不调用它:
popForm.window.onload = get_elements; // no parens
Currently you're calling the function and assigning the return value ( undefined
). 当前,您正在调用该函数并分配返回值(
undefined
)。 popForm
has not been loaded at that time so popForm.document.forms
is empty. popForm
尚未加载popForm.document.forms
,因此popForm.document.forms
为空。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.