简体   繁体   English

仅在加载整个页面后执行JS

[英]Execute JS only after the entire page loaded

I am trying to optimize a piece of JS (it's a company details - address, phone number etc. verification badge). 我正在尝试优化一个JS(这是公司详细信息-地址,电话号码等验证徽章)。 I really don't need it, but it pulls info from two different CDN's, which adds up to my page speed. 我确实不需要它,但是它从两个不同的CDN中提取信息,这增加了我的页面速度。

The line looks like this: 该行如下所示:

<script src="https://d20iczrsxk7wft.cloudfront.net/botwverified/badge.js?id=1234567"></script>

It is possible (jquery is already loaded for other stuff) to make this appear, or load, only after the page completely loaded? 是否有可能(已经为其他内容加载了jquery),使其仅在页面完全加载后才显示或加载? Probably JS? 大概是JS? I know nothing about JS. 我对JS一无所知。 My page loads in 1.5 second and this badge takes an extra second load. 我的页面加载时间为1.5秒,此徽章需要额外的时间加载。

Thank you! 谢谢!

You can use jQuery to load the script dynamically. 您可以使用jQuery动态加载脚本。

<script>
$(document).ready(function() {
    $.getScript("https://d20iczrsxk7wft.cloudfront.net/botwverified/badge.js?id=1234567");
});
</script>

The ready() function calls the function after the page is loaded. 页面加载后, ready()函数将调用该函数。

如果您使用Javascript,则window.onload将完成此工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM