通过异步加载或缓存加速外部横幅广告

发布于 2024-12-09 13:38:21 字数 1419 浏览 1 评论 0原文

我有一个漂亮、光滑、优雅的网站,加载速度加倍快,当然,直到横幅广告启用!等待检索会导致页面加载时间长达 10 秒,这对我来说真的很令人沮丧,因为我花了很长时间来优化网站的其余部分。

我尝试通过 ajax 或类似方式加载广告,但效果参差不齐。基于 Flash 的横幅广告和只需从广告服务器获取 html 片段的广告就可以正常工作。然而,有一些(我说的是 PaddyPower)返回 javascript 并使用 document.write() 将 html 广告附加到 DOM。广告永远不会显示。

附属代码:

<script type="text/javascript" src="someadserver.com/impression.aspx"></script>

someadserver.com 返回的脚本

document.write('Normal HTML code')

它一定与跨站点脚本和安全性有关,但我不是这方面的专家。有办法实现我想要的吗?

我不认为缓存是一种选择,因为它会扭曲展示次数/点击率统计数据。

另外,更改任何联属代码也不是一种选择,因为它是客户的网站,我想要一个稳定且可行的解决方案,不依赖于黑客广告代码。

---编辑---

我修改了我的广告类以返回由

之后我在这里使用这段代码:

    <?php foreach(Advert::getCachedAds() as $id => $code): ?>
    <div class="advert <?php echo $id ?>" style="visibility:hidden;">
        <?php echo $code ?>
    </div>
    <?php endforeach; ?>
    <script type="text/javascript">
        $(function(){
            $('.adContainer').each(function(index, object){
                var advert = $('div.' + $(object).attr('id')).html();
                $('div.' + $(object).attr('id')).html('');
                $(object).html(advert);
            });
        });
    </script>

这工作得很好,让我想起了我们在 Netscape 和拨号连接时代使用的 old-skool 图像预加载脚本。

原文

I have a beautifully slick and elegant website that loads in double quick time, until of course the banner ads are enabled! Waiting to retrieve the adds up to 10 seconds on the the page load time which for me is really frustrating as I have spent ages optimising the rest of the site.

I have tried to load the adverts via ajax or similar with mixed success. The flash based banners and the ads which simply get a snippet of html from the ad server work fine. However, there are some (PaddyPower I am talking about you) which return javascript and use document.write() to append the html advert to the DOM. The advert is never displayed.

The affiliate code:

<script type="text/javascript" src="someadserver.com/impression.aspx"></script>

The script returned by someadserver.com

document.write('Normal HTML code')

It must have something to do with cross site scripting and security but I am no expert on this. Is there a way to achieve what I want?

I don't believe caching is an option as it will skew the impressions/clickthrough statistics.

Also changing any affiliate code is not an option as it is a client's website and I want a stable and workable solution that does not rely on hacking ad code.

---EDIT---

I modified my advert class to return the advert code surrounded by <noscript> tags and at the same time save the advert code in a static member. Just after my </body> tag I use this code here:

    <?php foreach(Advert::getCachedAds() as $id => $code): ?>
    <div class="advert <?php echo $id ?>" style="visibility:hidden;">
        <?php echo $code ?>
    </div>
    <?php endforeach; ?>
    <script type="text/javascript">
        $(function(){
            $('.adContainer').each(function(index, object){
                var advert = $('div.' + $(object).attr('id')).html();
                $('div.' + $(object).attr('id')).html('');
                $(object).html(advert);
            });
        });
    </script>

This works quite nicely and reminds me of the old-skool image pre-loader scripts we used to use in the days of Netscape and dial-up connections.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小忆控 2024-12-16 13:38:21

您可以将占位符 div 放在您想要广告的位置,在加载所有实际内容后加载广告,然后将加载的广告移动到位。

这不会提高页面的整体速度,但可以让您看到实际内容,而无需等待广告加载。

You could put place-holder divs where you want your adverts to be, load the adverts after all the actual content has loaded and then move the loaded ads into place.

This will not help with the overall speed of the page but will allow your actual content to be seen without having to wait for the ads to load.

~没有更多了~

关于作者

0 文章
0 评论
24 人气
更多

推荐作者

missyouangeled

文章 0 评论 0

三生一梦

文章 0 评论 0

压抑⊿情绪

文章 0 评论 0

☆獨立☆

文章 0 评论 0

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文