而不是脚本src地址属性直接放源

时间:2017-03-23 14:02:41

标签: javascript shopify

我有一个有效的代码:

if (typeof Checkout === 'object') {
if (typeof Checkout.$ === 'function') {
    (function(src) {
        var tagName = 'script',
            script = document.createElement(tagName);
        script.src = src;
        var head = document.getElementsByTagName('head')[0];
        head.insertBefore(script, head.childNodes[0]);
    })('https://www.conversionpirate.com/pirate-countdown.js');
}

}

但是我不想引用转换网站,因为我可以直接粘贴那些简单的js。问题是我不知道该怎么做 - 我试图将pirate-countdown.js代码放在该方法中但却失败了。我确信它很简单,但需要了解它。有人可以帮我这个吗?

1 个答案:

答案 0 :(得分:0)

正常工作只需添加main__header classtime id的html元素作为beleow:



<script src="https://www.conversionpirate.com/pirate-countdown.js"></script>

<div class="main__header">
</div>
<div id="time">

</div>
&#13;
&#13;
&#13;

此外,如果你仍然面临的问题直接将他们的代码放入你的js文件中,或者只是创建新的js文件并在本地引用它:

见下文snipet:

&#13;
&#13;
function create(htmlStr) {
    var frag = document.createDocumentFragment(),
        temp = document.createElement('div');
    temp.innerHTML = htmlStr;
    while (temp.firstChild) {
        frag.appendChild(temp.firstChild);
    }
    return frag;
}

function startTimer(duration, display) {
    var start = Date.now(),
        diff,
        minutes,
        seconds;
    function timer() {
        diff = duration - (((Date.now() - start) / 1000) | 0);

        minutes = (diff / 60) | 0;
        seconds = (diff % 60) | 0;

        minutes = minutes < 10 ? "0" + minutes : minutes;
        seconds = seconds < 10 ? "0" + seconds : seconds;

        display.textContent = minutes + ":" + seconds; 

        if (diff <= 0) {
            start = Date.now() + 1000;
        }
    };
    timer();
    setInterval(timer, 1000);
}

window.onload = function () {
	
	var fragment3 = create('<div style="background:#fff5d2;padding:10px 20px;border:1px solid #e3df74; font-size:14px; color:#2c2c2c; font-weight:bold;-webkit-border-radius: 5px; -moz-border-radius: 5px; border-radius: 5px; margin:10px 0px 20px 0px">Your order is reserved for <span id="time"></span> minutes!</div>');
	document.getElementsByClassName('main__header')[0].appendChild(fragment3);


	var ten = 60 * 10,
	display = document.querySelector('#time');
  
	startTimer(ten, display);
};
&#13;
<div class="main__header">
</div>
<div id="time">

</div>
&#13;
&#13;
&#13;