簡體   English   中英

動態加載JavaScript文件

[英]Loading a JavaScript file dynamically

我通過銷售商被要求將此代碼添加到我的推銷頁面中:

<script>
    (function() {
        var p = '/?vendor=2knowmysel&time=' + new Date().getTime();
        var cb = document.createElement('script'); cb.type = 'text/javascript';
        cb.src = '//header.clickbank.net' + p;
        document.getElementsByTagName('head')[0].appendChild(cb);
    })();
</script>

該代碼應使頁面通過clickbank加載到帶有紅色徽標的標頭中。 當我在頭部添加代碼時,什么也沒發生。

接下來,我嘗試通過在空白html頁面(遠離drupal)上發布http://www.2knowmyself.com/testpage.htm來隔離問題。

但是框架沒有出現。

這是怎么了 鑒於clickbank聲稱該代碼是完美的。

這是您的代碼的作用:

<script>
    // the following line creates an anonymous immediately-invoked function
    (function() {
        // this will return a string named 'p', which contains the vendors ID and current time
        var p = '/?vendor=2knowmysel&time=' + new Date().getTime();
        // this creates a new 'script' tag for HTML, name it 'cb', and tells the code it's for JavaScript
        var cb = document.createElement('script'); cb.type = 'text/javascript';
        // this will take a url with the address and the query string, which you named 'p' earlier, and set it as the source for 'cb'
        cb.src = '//header.clickbank.net' + p;
        // now you'll insert 'cb' to the HTML, so it'll load the JavaScript file into it
        document.getElementsByTagName('head')[0].appendChild(cb);
    // the function won't run automatically upon declaration, so you use parenthesis to tell it to run
    })();
</script>

總結一下 ,它大聲地將供應商的ID和當前時間發送到給定的服務器,並期望從中返回一個JavaScript文件。 然后將其加載到您的HTML文檔中。

當前,它似乎無法正常工作,因為此服務器正在從您的頁面獲取信息,但未將JavaScript文件發送回該頁面。 當他們調整它來回答正確的文件時,您會看到它相應地運行。

編輯:(回答您的最終問題)

到目前為止,我可以看到他們的服務器沒有將預期的JS文件發送回您的頁面,因此它不起作用。 如果要自己檢查,請在瀏覽器中使用JS調試器或網絡監視器(大多數現代的網絡瀏覽器均內置這些功能,請嘗試按F12鍵然后重新加載頁面)。

如果您要檢查iframe是否可以在您的服務器上使用,可以聯系其管理員或嘗試自己將iframe嵌入頁面中。 將以下代碼粘貼到文檔中。 如果您看到SO主頁,則可以使用。 否則,它什么也不會顯示。 如果您看到Your browser does not support iframes. ,那么您可能必須更新網絡瀏覽器並再次檢查。

<iframe src="http://stackoverflow.com" width="300" height="300">
    <p>Your browser does not support iframes.</p>
</iframe>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM