简体   繁体   中英

Render after script tag

Can I place something like the code below in a html page, and have the coolstuff.js render content to the page? I think i've seen it done...what is this technique called? Any pointers?

<form id="form1" runat="server">
<div>
    <script src="coolstuff.js"
            param1="Monkeys" param2="Go" param3="Nutz"
            type="text/javascript"/>   
</div>
</form>

What you have there is accurate, it'll run whatever is runnable in coolstuff.js, ie if it's all function definitions for example, it'll define all the functions from there on out but no function will be called unless it's specifically done so in coolstuff.js; just to be clear.

The technique isn't called anything, I'd just hope you're not pushing out a lot of content through that, if you are you should stop and think of a better way of doing it.

Yes you can have a JS script place elements in the DOM of an HTML page.

The script doesn't know anything about its "position" in an HTML page: you'll have to "place" your elements yourself by referencing a position in the DOM.

In your example above, you'll have to find the position relative to the "script" tag with your script and then add childs etc. You can use the getEementsByTagName to find your "script" tag for example.

Even better, use getElementById with <script id="someid" ...> to find the relative position.

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