www.webdeveloper.com
Results 1 to 12 of 12

Thread: Load a script during runtime and add to body

  1. #1
    Join Date
    Jun 2014
    Posts
    13

    Load a script during runtime and add to body

    I need to load during runtime a script, and add it to the body of the page.

    Code:
    <script type="text/javascript" src="myUrl"></script>
    Each time the script is loaded it creates a new "window" in the middle of the screen in the layer above anything else, therefore it has to be added to the body of the page.

    I have to be able to load it on demand whenever I need.

    So far I wasn't able to add the loaded script as a new element.
    Unfortunately I deleted the code, but unless it's something that's commonly accomplished you can probably guess what I did wrong.

    Any ideas?

  2. #2
    Join Date
    Mar 2009
    Posts
    467
    Your stated requirements seem rather vague, but here's an example that might give you some direction:

    Code:
    //function to dynamically load script
    function doLoad(urlAndName){
        var hd, scr;
        hd = document.getElementsByTagName('head');
        hd = hd[0];
        scr = document.createElement('script');
        scr.src = urlAndName;
        hd.appendChild(scr);
    }
    
    //contents of downloaded script
    
    (function(){
        var first, div, content;
        first = document.body.firstChild;
        content = '<h1>Hello</h1>';
        div = document.createElement('div');
        document.body.insertBefore(div, first);
        div.innerHTML = content;
    })();

  3. #3
    Join Date
    Jun 2014
    Posts
    13
    doLoad() is more or less what I tried previously.

    The thing is, I'm trying to load a 3rd party script so I don't have access to its source.

    What I was trying to do last time was append the child to body instead of head in doLoad().

    The error I'm getting in developer tools:

    Failed to execute 'write' on 'Document': It isn't possible to write into a document from an asynchronously-loaded external script unless it is explicitly opened.
    Is it possible to explicitly open it? If not, what would be your solution?

  4. #4
    Join Date
    Mar 2009
    Posts
    467
    I really am confused by what you are saying. Show your code, or the relevant parts of it.

  5. #5
    Join Date
    Jun 2014
    Posts
    13
    I'll put it that way:

    Try to load during runtime into the body of a page a script which source code you can't access.

  6. #6
    Join Date
    Mar 2009
    Posts
    467
    I'll put it that way:

    Try to load during runtime into the body of a page a script which source code you can't access.
    If its actually a script you can ALWAYS access the source code. All you need is the URL, as in "http://nowhere.com/myScript.js" It doesn't matter if its a 3rd party script.
    Now, if you're trying to use this procedure to pull down just any type of file, such as an HTML file, off a third party server this is something different altogether, and its not going to work without some cooperation from some server, either yours or the third parties.

  7. #7
    Join Date
    Jun 2014
    Posts
    13
    Here's my source:
    https://www.trialpay.com/dispatch/1b...oduct=&amount=

    (Ignore the variables left out, it should work)

    How would you do it?

  8. #8
    Join Date
    Mar 2009
    Posts
    467
    If you look at what the thing at the url does, it essentially wants to create an iframe the source of which incorporates the variables sent in the original url and acts through the evil document.write() to completely overwrite your original page.

    You would do better to simply dynamically create iframes and place them in your document.

  9. #9
    Join Date
    Jun 2014
    Posts
    13
    It seems it's a bit more problematic to do than it sounds. Think we could talk on skype?

  10. #10
    Join Date
    Mar 2009
    Posts
    467
    It seems it's a bit more problematic to do than it sounds. Think we could talk on skype?
    Email me if you want to. This website provides the means to do so.

  11. #11
    Join Date
    Jun 2014
    Posts
    13
    I don't see a way to PM you nor email you on this website, checked your profile. I'm either missing something or am limited because I'm a new user.

    Anyway, sending you an email is pretty much like talking in here, I wanted to solve the problem quickly in a real time chat rather than discuss it for a few days over here.

  12. #12
    Join Date
    Jun 2014
    Posts
    13
    Thanks, I managed to create and remove dynamic iframes like you said.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles