简体   繁体   中英

Loading a JavaScript file dynamically

I was asked to add this code to my pitch pages by the vendor I sell through:

<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>

The code should let the page load within a header that has a red logo by clickbank. When I added the code in the head section nothing happened.

Next I tried to isolate the problem by posting on a blank html page (away from drupal) which is http://www.2knowmyself.com/testpage.htm .

But the frame doesn't show up.

What's wrong in here? Given clickbank claim the code is perfect.

Here's what your code does:

<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>

Summing it up , it bassically sends the vendor's ID and current time to the given server, and expects a JavaScript file in return from it; it'll then load this file into your HTML document.

Currently, it seems not to be working because this server is getting the information from your page but not sending the JavaScript file back to it. When they adjust it to answer with the right file, you'll see it run accordingly.

EDIT: (to answer your Final Question)

Up to this point, I can see that their server isn't sending the expected JS file back at your page, so it doesn't work. If you want to check this by yourself, please use a JS debugger or a network monitor in your browser (most of the modern webbrowsers come with these built-in, try pressing F12 then reloading the page).

If you want to check whether iframes work on your server, you may contact its administrator or try to embed an iframe in the page yourself. Paste the following code into the document. If you see SO homepage, it works. Otherwise, it'll show nothing. If you see Your browser does not support iframes. , then you might have to update your web browser and check it again.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM