There’s one thing I want to do with javascript, but don’t know how. In a perfect world it would look like this:
<p>My very cool page!</p>
<script type="text/javascript">
document.write('<div>some content</div>');
</script>
And this script would insert <div>some content</div> right before (or after, or instead of) script tag. But in the real world, document.write starts writing html anew, removing any static content in the page (<p> tag, in this case).
This is simplified example, but should give you the idea. I know that I can statically put <div id="some_id"></div> before script tag and insert html in it from js, but I wanna be able to use multiple instances of this snippet without changing it (generating random id manually) each time.
I’m ok to use jquery or any other existing library as well. Is there any way to achieve this? Thanks!
To close the question, here’s how it has worked out in the end.
The trick was to store each widget’s data in html tag, not in javascript. User inserts content like this
User also links script from our server. When page’s loaded, script does
$('div.my_widget').eachand populates contents of each widget depending on itsfeed_idand other attributes.Thanks to everyone (and +1 to Gaby and Kerry for attempts to help with such vague problem)