Loading JavaScript Into Div Dynamically

前端 未结 6 1205
难免孤独
难免孤独 2021-01-22 15:32

I need to load JavaScript code (which I don\'t have control over) from a URL into a div. The JavaScript code is a bunch of document.write() statements. Once the document.write()

6条回答
  •  天命终不由人
    2021-01-22 16:20

    document.write() after your document is loaded will clear your document and start a new one. You don't document.write() into a div that already exists unless it's inline javascript that is included in that div and is loaded as your document loads/renders.

    So, that leaves you two options:

    1) You can create an empty iframe and load the JS into that iframe and let it render into that iframe. If you have no cooperation from the JS, you will likely have to poll to see when the rendering appears to be done. If you create the iframe properly, you may be able to set up a monitor to see when the iframe is loaded.

    2) You can include the JS inline into your div and it will render into your div as your page is loaded. This would require baking the div and the script file into your own page HTML like this:

    If you did it this way, then you would know that it was done rendering when your own document was done loading.

提交回复
热议问题