1 回答

TA貢獻1995條經(jīng)驗 獲得超2個贊
請注意,遠程 URL 不會立即加載。返回時
window.open()
,窗口始終包含about:blank
. 實際獲取 URL 會延遲并在當前腳本塊執(zhí)行完畢后開始。窗口創(chuàng)建和引用資源的加載是異步完成的。
由于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()調用是否找到任何內容。
添加回答
舉報