我没有用 JavaScript 进行过任何编程,所以我什至不确定我是否在正确的轨道上。理想情况下,我想要的是打开多个窗口,在其中搜索特定字符串,然后关闭没有找到该字符串的窗口。
这个函数只是处理一个新窗口中的一页。它打开的页面确实包含我要查找的词,但是当我运行它时,返回未找到字符串。
function open_win() {
var wnd = window.open("http://www.bartleby.com/123/32.html");
if (wnd.find("morning")){
alert("string found");
}
else{
alert("string not found");
}
}
我修改了这段代码以包含延迟让页面加载,但现在搜索功能似乎不起作用。警报永远不会出现。
function open_win() {
var wnd = window.open("http://www.bartleby.com/123/32.html");
setTimeout(function(){
if (wnd.find("morning"))
{
alert("string found");
}
else
{
alert("string not found");
}
},3000);
}
最佳答案
窗口在打开时不包含任何内容。您需要等待它加载。类似的东西
function open_win() {
var wnd = window.open("http://www.bartleby.com/123/32.html");
wnd.addEventListener("load",function(){
if (wnd.find("morning")){
alert("string found");
}
else{
alert("string not found");
}
}
}
关于JavaScript - 打开新页面并在其中搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13148543/