gpt4 book ai didi

javascript - 在页面加载结束时运行 Javascript

转载 作者:行者123 更新时间:2023-12-02 13:44:43 29 4
gpt4 key购买 nike

我正在尝试请求 Ajax 中的页面和普通的 GET 请求。我的基本布局是这样的:

<html>
<head>
//css file
</head>

**************
<body>

<script> </script> // this script needs to be loaded at the end of the page load in both GET and AJAX

</body>

*****************
//js files (jQuery)

</html>

*中的区域由ajax加载

如果我发出 GET 请求,我会得到完整的页面。如果我发出 Ajax 请求,我只能得到正文部分(没有 jQuery)。

现在,body 标记包含一个 script 标记。

<script type="text/javascript">
window.onload = function() {
$.getJSON("/loc", function (data) {
// .......
});
};
</script>

当我发出 GET 请求时,此代码运行良好。该脚本在页面加载后执行。但是当我发出 Ajax 请求时,脚本不会执行,因为页面已经加载。

所以我尝试了:

<script type="text/javascript">
$(document).ready(function () {
$.getJSON("/loc", function (data) {
.....
});
});
</script>

现在,这可以在 Ajax 中正常工作,但不适用于 GET 请求,因为 jQuery 位于页面末尾。

script 标签位于 body 标签内,jquery 位于 body 标签之后,所以如果我将它移到 jQuery 之后。因此,我需要为我的页面进行 2 种设计:1 种用于我的 Ajax 请求,1 种用于我的 GET 请求。如果不是在 Ajax 中,我最终将加载 jQuery 脚本两次。

所以我的问题是我可以有一个同时运行 Ajax 和 GET 请求的脚本吗?

最佳答案

now this works fine with ajax but doesn't work with GET request because jquery is at the end of the page

将所有脚本(和脚本标签)放在 jquery 之后,一切都会正常工作。

关于javascript - 在页面加载结束时运行 Javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41504360/

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