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>
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.
What you have there is accurate, it'll run whatever is runnable in coolstuff.js, i.e. 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.
精彩评论