javascript 使用 jQuery 将脚本放在 BODY 标签的末尾

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15626240/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 01:26:46  来源:igfitidea点击:

Place script before the end of BODY tags with jQuery

javascriptjquerygoogle-chrome-extension

提问by Kyle Sterling Collins

This is the script I want before the body tag:

这是我在 body 标签之前想要的脚本:

<script type="text/javascript">
  var vglnk = { api_url: '//api.viglink.com/api',
                key: '89dcd0a12ff35d227eaaaff82503030b' };

  (function(d, t) {
    var s = d.createElement(t); s.type = 'text/javascript'; s.async = true;
    s.src = ('https:' == document.location.protocol ? vglnk.api_url :
             '//cdn.viglink.com/api') + '/vglnk.js';
    var r = d.getElementsByTagName(t)[0]; r.parentNode.insertBefore(s, r);
  }(document, 'script'));
</script>

I want this code to be where I've put "HERE"

我希望这个代码是我放“这里”的地方

<html>
<head>
</head>
<body>
some html and stuff
HERE
</body>
</html>

How would I go about this in jQuery? (I'm doing this from an extension. Mainly in Chrome, but also FF, and IE.)

我将如何在 jQuery 中解决这个问题?(我是从扩展程序中做到这一点的。主要是在 Chrome 中,但也有 FF 和 IE。)

Any help is appreciated.

任何帮助表示赞赏。

回答by frisco

You need the content scriptto do the insert on every page you want.

您需要内容脚本在您想要的每个页面上进行插入。

The code of the content script is really simple and doesn't need jquery.

内容脚本的代码非常简单,不需要jquery。

var code = "your script code here";
var script = document.createElement("script");
script.setAttribute("type", "text/javascript");
script.appendChild(document.createTextNode(code));
document.body.appendChild(script);

As it will only be called once you don't even need to define a function. You can debug the code using debugger on any web the content script is attaching (F12) you will see your code in the content script tab.

因为它只会在您甚至不需要定义函数时被调用。您可以在内容脚本附加的任何 Web 上使用调试器调试代码 (F12),您将在内容脚本选项卡中看到您的代码。

回答by Tryah85

I had the same issue regarding the best place to add jQuery: to the header or before the body tag? The answer is that it does not matter.

关于添加 jQuery 的最佳位置,我有同样的问题:到标题还是在 body 标签之前?答案是没关系。

The whole page (or DOM) needs to initialize or load in order to accomplish what you are doing. AND.. The more information within the body the more reliance you need to make sure the document is loaded.

整个页面(或 DOM)需要初始化或加载才能完成你正在做的事情。AND.. 正文中的信息越多,您就越需要确保加载文档。

The 2 sentences above are redundant because:

上面两句话是多余的,因为:

All jQuery ui/basic syntax/widgets, etc are triggered with: $(document).ready( function() { $("#some_id").click( function { MORE CODE HERE }); });

所有 jQuery ui/基本语法/小部件等都是通过以下方式触发的: $(document).ready( function() { $("#some_id").click( function { MORE CODE HERE }); });

The code above means that the the full html page (or 'document') needs to be loaded before jQuery can run, aka initialized.

上面的代码意味着需要在 jQuery 运行之前加载完整的 html 页面(或“文档”),也就是初始化。

There is an order that jQuery needs to be loaded for a ui to work. The actual library needs to be first, then the ui. The library can be downloaded from jquery.com and uploaded to the designers web space or through a CDN (CONTENT DISPLAY NETWORK) to save on bandwidth. Here is an example of how the order should be:

需要加载 jQuery 才能使 ui 工作。需要首先是实际的库,然后才是 ui。该库可以从 jquery.com 下载并上传到设计人员的网站空间或通过 CDN(内容显示网络)以节省带宽。以下是订单应该如何的示例:

<script src="http://code.jquery.com/jquery-1.6.4.min.js" ></script> <script src="http://code.jquery.com/mobile/1.0/jquery.mobile-1.0.min.js" ></script>

<script src="http://code.jquery.com/jquery-1.6.4.min.js" ></script> <script src="http://code.jquery.com/mobile/1.0/jquery.mobile-1.0.min.js" ></script>

Notice the library is the first line, then the UI, in this case I loaded jquery mobile.

注意库是第一行,然后是 UI,在本例中我加载了 jquery mobile。

In con conclusion it does not matter, a preference mostly. More here https://forum.jquery.com/topic/unclear-where-document-ready-goes#14737000004139019

总之,没关系,主要是偏好。更多在这里https://forum.jquery.com/topic/unclear-where-document-ready-goes#14737000004139019