网络广告js备忘【2】

内部代码,因为广告采用iframe方式加载,所以分为内外代码

var adinfo = {
    advpostionid: "",
    pageid: "",
    userid:"",
    adcss:"at"
}
var win = window;//缩减代码
var dom = win.document;
//广告业务配置对象
adconfig = { 
    //外部配置项
configitem: [{ name: "autosense_view_adids", val: "" }, { name: "autosense_view_adpositions", val: "" }, { name: "autosense_view_keywordids", val: "" }, { name: "autosense_view_groupindexes", val: "" }, { name: "autosense_view_unionid", val: "" }, { name: "autosense_view_adsiteid", val: "" }, { name: "autosense_view_addomainid", val: "" }, { name: "autosense_view_url", val: "" }, { name: "autosense_view_ip", val: "" }, { name: "autosense_view_areacodeid", val: "" }, { name: "autosense_view_hitkeywordids", val: "" }, { name: "autosense_view_hitkeywordpids", val: "" }],
    kv: new Object,
    //载入配置
    loadConfig: function () {
        var configitems = adconfig.configitem;
        for (var i = 0; i < configitems.length; i++) {
            if (win[configitems[i].name]) {
                adconfig.configitems[i].val = win[configitems[i].name];
                adconfig.kv[configitems[i].name] = adconfig.configitems[i].val;               
            };
        };
    }
};
function addEvent(domobj, eventType, handler, argsObject) {
    var eventHandler = handler;
    if (argsObject) {
        eventHandler = function (e) {
            handler.call(argsObject, e);//调整事件执行时的对象
            return true;
        };
    };
//和外部事件绑定不同,因为采用和外部相同的绑定方式后总是会造成事件执行两次,不知道为什么缩写代码会有这样的问题
 if (window.attachEvent)
        domobj.attachEvent("on" + eventType, eventHandler);
    else
        domobj.addEventListener(eventType, eventHandler, false);
};
function clickadv(ev) {
    // alert("ie6 is ok");
    var url = "";
    if (!this.original) {
        this.original = this.href;//保存原始href
    };
    url = this.original;//this指向点击的a标签,和事件绑定时的方式有关
    ev = ev || window.event;
    var mx, my;
    if (ev.pageX || ev.pageY) {
        mx = ev.pageX, my = ev.pageY
    } else {
        mx = ev.clientX + document.body.scrollLeft - document.body.clientLeft;
        my = ev.clientY + document.body.scrollTop - document.body.clientTop;
    }
    url = url + "&mx=" + mx + "&my=" + my;
    this.href = url;
   // alert(url);
};
var _$c = function (cssname) {
    if (dom.getElementsByClassName)//低版本浏览器不支持此方法
        return dom.getElementsByClassName(cssname);
    else {
        var all = dom.all;
        var objs = new Array;
        for (var i = 0; i < all.length; i++) {
            if (all[i].className == cssname)
                objs.push(all[i]);
        };
        if (objs.length == 0)
            objs = null;
        return objs;
    };
};
function adBindEvent() {
    //var advs = _$c(adinfo.adcss);
    var advs = dom.getElementsByTagName("A");
    if (advs) {
        for (var i = 0; i < advs.length; i++) {
            if (advs[i]) {
                addEvent(advs[i], "click", clickadv, advs[i]);
            };
        };
    };
};
function getAdPositionInfo() {
    var pairs = location.search.substring(1).split("&");
    var count = 0;
    for (var i = 0; i < pairs.length; i++) {
        if (count == 2) break;
        var pos = pairs[i].indexOf('=');
        if (pos == -1) continue;
        var name = pairs[i].substring(0, pos);
        if (name == "posid") {
            adinfo.advpostionid = pairs[i].substring(pos + 1);
            count++;
            continue;
        };
        if (name == "pgid") {
            adinfo.pageid = pairs[i].substring(pos + 1);
            count++;
            continue;
        };
        if (name == "userid") {
            adinfo.userid = pairs[i].substring(pos + 1);
            count++;
            continue;
        };
    };
};
function getxy(domobj) {
    var x = 0, y = 0;
    if (domobj.getBoundingClientRect) {
        var box = domobj.getBoundingClientRect();
        var D = document.documentElement;
        x = box.left + Math.max(D.scrollLeft, document.body.scrollLeft) - D.clientLeft;
        y = box.top + Math.max(D.scrollTop, document.body.scrollTop) - D.clientTop
    }
    else {
        for (; domobj != document.body; x += domobj.offsetLeft, y += domobj.offsetTop, domobj = domobj.offsetParent) { }
    }
    return { x: x, y: y };
};
//配置脚本执行过程
var actionconfig = [{ name: "initadconfig", ev: getAdPositionInfo, isaction: true }, { name: "bindEvent", ev: adBindEvent, isaction: true }];
//执行
for (var runindex = 0; runindex < actionconfig.length; runindex++) {
    if (win[actionconfig[runindex].name])
        actionconfig[runindex].isaction = win[actionconfig[runindex].name];
    if (actionconfig[runindex].isaction)
        actionconfig[runindex].ev();
}

Firefox使用DOM规范,所以\n这样的不可见字符也会作为dom的一个元素

例如

<div id="t1"><div>1</div></div>

其中t1的DOM子元素是1个

然而

<div id="t1"><div>1<div>
</div>
t1的DOM子元素是2个,因为不可见字符\n也被作为了一个dom元素
IE不存在这个问题。而且,只要是使用DOM规范的浏览都和火狐一样的效果。

posted on 2014-02-18 14:54  AI001  阅读(166)  评论(0编辑  收藏  举报

导航