添加script标签、添加事件

时间:2023-03-08 16:21:40
添加script标签、添加事件

添加script标签

var _hmt = _hmt || [];
    (function () {
        var hm = document.createElement("script");
        hm.src = "//hm.baidu.com/hm.js?e72ef138e02a09642f378f0fe01c2017";
        var s = document.getElementsByTagName("script")[0];
        s.parentNode.insertBefore(hm, s);
    })();

添加事件

var addEvent = function(obj, type, fn) {
    if (obj.attachEvent) {
        obj.attachEvent('on' + type, function() {
            fn.call(obj);
        })
    } else {
        obj.addEventListener(type, fn, false);
    }
}
            
addEvent(window, 'scroll', function() {
    //code……
});