views:

32

answers:

2

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>
+1  A: 

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.

jldupont
ahhh! so just add id tag and use getElementById....
Adrian
+1  A: 

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.

mjsabby
Define "a lot" please? Alternatives for reusing code? Flash? surely that's 'worse'. In this instance I want to code up a simple image carousel from a data driven source, other things planned too. Simple JS seems the perfect answer here...minimal html clutter on the page...
Adrian