1 回答

TA贡献1995条经验 获得超2个赞
请注意,远程 URL 不会立即加载。返回时
window.open()
,窗口始终包含about:blank
. 实际获取 URL 会延迟并在当前脚本块执行完毕后开始。窗口创建和引用资源的加载是异步完成的。
由于Window
似乎有一个onload
处理程序,我会简单地尝试将窗口更改部分包装到一个事件处理程序中:
function searchNameButton() //this function is called when the user clicks the search by name button.
{
name = document.getElementById("nmSearch").value;
wnd = window.open("search.html"); //opens a new window, can be accessed through this wnd variable
matchIndexes = [];
for (i = 0; i < pokemon.length; i++) //do the actual search here
{
if (pokemon[i][1].toLowerCase().includes(name.toLowerCase())) //converts to lowercase so that the search is case-insensitive
{
matchIndexes.push(i);
}
}
wnd.onload = function()
{
//now to populate the new page, similar to how it was done before
itemList = wnd.document.getElementsByClassName("pd");
console.log(matchIndexes);
for (i = 0; i < itemList.length; i++)
{
itemList[i].innerHTML = generateString(pokemon[matchIndexes[i]]);
}
};
}
但是,我还没有真正尝试过这个,所以它可能会或可能不会工作。如果没有,我会在处理程序的最开头添加一些虚拟日志以查看它是否被调用,然后添加一行console.log(itemList);以查看getElementsByClassName()调用是否找到任何内容。
添加回答
举报