Discuss / JavaScript / 避免点击时head中的script递增

避免点击时head中的script递增

Topic source

十八年后

#1 Created at ... [Delete] [Delete and Lock User]

//本想用布尔值判断但是之后无法进行刷新,所以采用删除节点的方式

        function refreshPrice(data) {//回调
            var p = document.getElementById('test-jsonp');
            p.innerHTML = '当前价格:' +
                data['0000001'].name +': ' + 
                data['0000001'].price + ';' +
                data['1399001'].name + ': ' +
                data['1399001'].price;
        }

        function getPrice() {                
            var
                js = document.createElement('script'),
                head = document.getElementsByTagName('head')[0];
            js.src = 'http://api.money.126.net/data/feed/0000001,1399001?callback=refreshPrice';
            console.log(head.lastChild.src===js.src);
            //若head最后一个节点内容为股票url则先删后加
            if(head.lastChild.src===js.src){
                head.removeChild(head.lastChild);
                head.appendChild(js);
            }else{
                head.appendChild(js);
            }                  
        }

  • 1

Reply