是否可以在页面上最后加载一个javascript广告,以防止加载速度慢? [英] Is it possible to load a javascript ad last on the page to prevent slow load times?

查看:53
本文介绍了是否可以在页面上最后加载一个javascript广告,以防止加载速度慢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一种方法可以在页面的最后添加一个由javascript加载的广告,但是该广告是否已位于内容中?主要目的是防止javascript广告减慢页面加载速度,因此,如果有其他方法可以实现此目的,请务必共享.

Is there a way to include a javascript-loaded advertisement last on the page, but have it positioned in the content? The primary purpose is to keep the javascript ad from slowing down the page load, so if there are other methods to achieve this please do share.

推荐答案

defer 属性 ,您可以放置​​脚本块以将其执行推迟到页面完成加载为止.

There is the defer attribute you could put on script blocks to defer its execution until the page completes loading.

<script src="myscript.js" type="text/javascript" defer>
// blah blah
</script>

我不确定关于使用此属性的一般建议.

I am not sure about the general recommendation about using this attribute though.

正如@David所指出的,对XHTML使用 defer ="defer"

As @David pointed out, use defer="defer" for XHTML

而且您始终可以将代码放入 window.onload 事件中,以便在页面加载后后执行它们.

And you can always put the code inside the window.onload event so that it executes after the pages load:

window.onload = function () {
    // ad codes here

};

但是后面的方法可能会带来一些问题,您可能需要先对其进行测试.

But the later approach may pose some problems, you might want to test it out first.

有关Dea Edwards的此博客文章的更多信息,撰写了著名的 javascript包装器.

More information on this blog post by Dean Edwards, the guy who wrote the famous javascript packer.

编辑:如果广告代码本身就是问题所在,则可以始终通过将上述方法与通过 document.write 进行脚本注入的方法相结合来推迟加载:/p>

If the size of ad code itself is the problem, you can always defer the loading by combining the above methods with script injection via document.write:

function loadScript(src) {
    document.write('<script src="' + src + '" type="text/javascript"></script>');
}

因此生成的代码可能看起来像这样:

So the resulting code might look something like this:

<script type="text/javascript" defer>

function loadScript(src) {
    document.write('<script src="' + src + '" type="text/javascript"></script>');
}

loadScript('http://ads.example.com/advertisements.js');

</script>

但是正如我所说,这也取决于您获得的特定广告代码.由于位置 document.write 会将内容写入可能不是您想要的位置.可能需要进行一些修改.

But as I've said, it depends on the specific ad code you get too. Because of the position document.write will write stuffs to might not be the location you want. Some modifications might be neccessary.

这篇关于是否可以在页面上最后加载一个javascript广告,以防止加载速度慢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆