简体   繁体   中英

Getting parameter value from Javascript dynamic script

I have this piece of JS code:

(function() {
    var s = document.createElement('script');
    s.id = 'LeadiDscript_campaign';
    s.type = 'text/javascript';
    s.async = true;
    s.src = (document.location.protocol + '//d1tprjo2w7krrh.cloudfront.net/campaign/aa545531-face-feed-cafe-4c5e1302580a.js');
    var LeadiDscript = document.getElementById('LeadiDscript');
    LeadiDscript.parentNode.insertBefore(s, LeadiDscript);
});

It returns parameters that I can only access/view from Firebug. The parameter name is 's'. How can I access that parameter so that I can view it and display it as an HTML element?

If you want to access a variable from a private scope within the global scope you'll need to bind it to the global scope or a child of it.

(function() {
        var s = document.createElement('script');
        window.s = s;
        ...
});

Then later, somewhere in your code, in a completely different scope, you can access it using either window.s or just s if there are no local variables of that name.

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