简体   繁体   English

使用window.open将数据从父级传递到子级时,为什么会显示“无法读取未定义的属性”元素”?

[英]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.

相关问题 'TypeError: Cannot read property of x of undefined' 将数据从父组件传递到子组件但其他组件正在工作 - 'TypeError: Cannot read property of x of undefined' when passing data from parent to child component but others are working TypeError:无法读取未定义的属性“ stateUpdate”(哪个函数)/将数据从父级传递到子级 - TypeError: Cannot read property 'stateUpdate' (Which is a function) of undefined / Passing data from Parent to child 为什么在定义数据时会出现“无法读取未定义的属性”错误? - Why do I get a "cannot read property of undefined" error when the data is defined? HandsonTable,将数据传递到子窗口和window.open - HandsonTable, passing data to child window, and window.open 将 axios 获取的道具从父级传递给子级返回“类型错误:无法读取未定义的属性 &#39;x&#39;” - Passing axios fetched prop from Parent to Child returns “TypeError: Cannot read property 'x' of undefined” 为什么我无法读取未定义的属性“toString” - Why do I get Cannot read property 'toString' of undefined 为什么得到“无法读取该对象的未定义属性...”? - Why do I get "Cannot read property…of undefined with this object? 为什么会出现“无法读取未定义的属性” props”? - Why do I get 'Cannot read property 'props' of undefined'? window.open的异常行为,如果我通过window.open更改子窗口的位置,则子窗口未从父页面映射 - Unexpected behavior of window.open, child window isn't mapping from parent page if i change the child window location through window.open TypeError: Cannot read property 'map' of undefined 当我尝试 map 通过从父组件传递到子组件的道具时显示 - TypeError: Cannot read property 'map' of undefined is showing when I'm trying to map over the props passed from parent component to child component
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM