【发布时间】:2019-06-17 01:58:33
【问题描述】:
正在清理我的错误和折旧的网页,
我遇到了这个警告信息:
jquery-3.4.1.js:9725
[弃用] 主线程上的同步 XMLHttpRequest 已弃用,因为它会对最终用户的体验产生不利影响。
如需更多帮助,请查看https://xhr.spec.whatwg.org/。
我要正确处理JavaScript的权利:
- 将脚本和链接保存在一个独立的区域中,即
- 页面加载后运行
但我不知道该怎么做:
- 从内联 JS 引用转换为 JS 文件
- 内部 JS 格式应该是什么样子
我一直在使用:
<head>
<script>$(document).ready(function () { $('<script/>', { type: 'text/javascript', src: 'assets/js/set-background.js' }).appendTo('head'); }); </script>
</head>
使用 JS 文件:
...
var body = document.getElementsByTagName("body")[0];
body.className += " change-" + idx;
...
如果脚本是以下情况,则不会发生警告:
<body>
...
<script src="assets/js/set-background.js"></script>
...
</body>
试过了,
<head>
<script>$(function(){'assets/js/set-background.js'}) </script>
</head>
<head>
<script type="text/javascript" src="assets/js/set-background.js"></script>
</head>
<body class="container" onload="assets/js/set-background.js">
我看过其他开发者的讨论:
- javascript - How to run a function when the page is loaded? - Stack Overflow
- Pure JavaScript equivalent of jQuery's $.ready() - how to call a function when the page/DOM is ready for it - Stack Overflow
- javascript - $(document).ready equivalent without jQuery - Stack Overflow
- How to run a function in jquery - Stack Overflow
我也一直在阅读:
【问题讨论】:
-
这是正确的:
-
这个不行,问题是我需要在页面加载后运行脚本。
标签: javascript jquery html onload document-ready