gpt4 book ai didi

javascript - 如何重定向页面,等到完全加载然后查询

转载 作者:行者123 更新时间:2023-11-28 10:38:07 27 4
gpt4 key购买 nike

我正在尝试编写一个代码,该代码可以重定向到某个页面,然后等待该页面完全加载,然后在这个新页面上执行一些查询。例如,我想重定向到 google.com,然后在搜索栏中注入(inject)“hello”。

我尝试了一些 sleep /等待函数或事件监听器,但没有成功。

<!DOCTYPE html>
<html>
<head>
<title>query</title>
</head>
<body>
<script type="text/javascript">
window.location = 'https://google.com';
document.querySelector('input[name="q"]').value = 'hello';
</script>
</body>
</html>

我想等待新页面充满电,然后执行查询,但使用上面的脚本它只会访问 google.com 并且不会在搜索栏中注入(inject)“hello”。

最佳答案

如果您使用 jQuery,则可以使用以下 3 种之一。

<script>
$(function() {
// Code...
});

jQuery(document).ready(function($) {
// Code...
});

$(document).ready(function() {
// Code...
});
</script>

如果没有检查这个答案:

Pure JavaScript equivalent of jQuery's $.ready() - how to call a function when the page/DOM is ready for it

关于javascript - 如何重定向页面,等到完全加载然后查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57349198/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com