gpt4 book ai didi

javascript - 无法修改 HTML 时如何更改脚本顺序?

转载 作者:行者123 更新时间:2023-11-30 17:18:05 24 4
gpt4 key购买 nike

当前:

<head>
<script src="Custom.JS">
<script src="jQuery.JS">
</head>

需要:

<head>
<script src="jQuery.JS">
<script src="Custom.JS">
</head>

我无法编辑文档,但可以编辑脚本文件。有什么办法吗?

最佳答案

这是一个糟糕的情况,根本原因应该被修复,但是..有一些“方法”。

这是我要介绍的一个。它假设 jQuery.js 不能(也不应该)被修改并且应该仍然包含所有相关的 jQuery 代码,但是 Custom.js 是公平游戏并且它运行的代码可以被异步处理。

考虑 Custom.js 是否像这样(根据需要修改/修复):

(function (fn) {
if (window.jQuery) {
// jQuery already loaded, don't reload/reevaluate it
fn(window.jQuery);
} else {
// Use of document.write because document.head is not supported before IE9.
// (If IE9+ was the minimum version I would recommend doing the script
// insertions via DOM and using onload, as these would be available then.)
window.__CustomJs_OnLoad = function () {
// This now runs after jQuery is loaded (from the injected script)
del window.__CustomJs_OnLoad;
fn(window.jQuery);
};
// Script.onload could be used IE8+, but why not go all-the-way awesome?
// These scripts are guaranteed to run synchronously in document-order.
document.write("<script src=jQuery.js></sc" + "ript>");
document.write("<script>__CustomJs_OnLoad()</sc" + "ript>");
}
}(function ($) {
// The rest of Custom.js here - which should already be nicely wrapped
// in a function/module anyway, no?
}));

关于javascript - 无法修改 HTML 时如何更改脚本顺序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25695824/

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