【问题标题】:Load Ads after the page Loaded With jQuery在使用 jQuery 加载页面后加载广告
【发布时间】:2010-06-28 17:10:12
【问题描述】:

我为 jQuery 编写了一个插件,将广告 JavaScript 的输出复制到容器 Div。

所以我将 Ads JS 放在页面底部(这样它们就不会降低我的页面加载速度)在不建​​议的 Div 中,如下所示:

<div id="ad_loader_4" class="ads_loader"></div>

这些 div 的 id 指向容器 div。 容器 div 看起来像:

<div id="ad_4"></div>

jQuery插件等待页面结束加载,然后抓取在不可见div中创建的所有元素,并将它们附加到容器div中。

jQuery 插件看起来像:

(function($) {  
// jQuery plugin definition  
$.fn.adsLoader = function(params) {  
    // merge default and user parameters  
    params = $.extend( {}, params);  
    // traverse all nodes  
    this.each(function() {  
        // express a single node as a jQuery object  
        var $t = $(this);  
        // find id  
        var id = $t.attr('id');
        id = id.substring(10,id.length);
        $t.children().not('script').appendTo("#ad_"+id);
    });  
    // allow jQuery chaining  
    return this;  
};  
})(jQuery);

该插件在 FF 和 Chrome 和 IE8 中运行良好...在 Adsense 和其他一些广告程序上...但问题开始于 IE7...由于某种原因,有时广告会加载到容器中,有时会加载他们不是……

我的插件有什么问题?

【问题讨论】:

    标签: javascript jquery jquery-plugins adsense


    【解决方案1】:

    我简化了一点。

    (function($) {
    // jQuery plugin definition
    $.fn.adsLoader = function() {
        // traverse all nodes
        this.each(function() {
            // get ad id and replace
            var id = this.id.substr(10);
            $("#ad_"+id).replaceWith(this);
        });
        // allow jQuery chaining
        return this;
    };
    })(jQuery);
    

    注意:如果您要隐藏底部 div,则可能需要在用它们替换空 div 后显示它们。

    【讨论】:

    • 这行不通,因为所有的广告程序都在其中使用了 document.write javascript,所以它会写在所有页面上...这就是为什么我告诉插件转义脚本标签的原因。 ..
    猜你喜欢
    • 2011-06-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-16
    • 1970-01-01
    • 2023-04-08
    • 1970-01-01
    相关资源
    最近更新 更多