关于作者

Discuss / JavaScript / 修复head内script不断增长

修复head内script不断增长

问题来源

jinglepper

#1 Created at ...
function refreshPrice(data) {
    let p = document.getElementById('test-jsonp');
    p.innerHTML = '当前价格:' +
        data['0000001'].name + ': ' +
        data['0000001'].price + ';' +
        data['1399001'].name + ': ' +
        data['1399001'].price;
}
function checkInHead(x) {
    head = document.getElementsByTagName('head')[0];
    for (let j of head.children) {
        if (j.tagName === 'SCRIPT') {
            if (x.src === j.src) {
                return true;
            }
        }
    }
    return false;
}
function getPrice() {
    let
        js = document.createElement('script'),
        head = document.getElementsByTagName('head')[0];
    js.src = 'http://api.money.126.net/data/feed/0000001,1399001?callback=refreshPrice';
    if (!checkInHead(js)) {
        head.appendChild(js);
    }
}

  • 1

Reply

WARNING: You are using an old browser that does not support HTML5. Please choose a modern browser (Chrome / Microsoft Edge / Firefox / Sarafi) to get a good experience.