Currently, I have the following code:
<html>
<head>
...
</head>
<body>
<div>
<script type="text/javascript">
<!--
document.write('<span>Test</span>');
//-->
</script>
</div>
</body>
</html>
How to replace the JavaScript in Prototype's style: new Element?
I need something similar:
<div>
<script type="text/javascript">
<!--
var span = new Element('span');
document.write(span); // <- THAT IS
</script>
</div>
I mean, I need to place <new Element> in DOM there script is
called ;-) How to do it? Exactly threre the script is called, 'cause I
don't known where it called ;-)
Thank you ;-)
on 2008-06-13 15:02
on 2008-06-13 15:41
See my response to your other question. :-) There are legitimate uses for document.write though. Document.write() will write the output into the HTML source immediately (good for loading dependent libraries or stylesheets), but element injection requires the DOM to be already built. -Fred On Fri, Jun 13, 2008 at 8:02 AM, AlannY <m@alanny.ru> wrote: > </div> > > I mean, I need to place <new Element> in DOM there script is > called ;-) How to do it? Exactly threre the script is called, 'cause I > don't known where it called ;-) > > Thank you ;-) -- Science answers questions; philosophy questions answers.
on 2008-06-13 16:27
It's not helps ;-( Because, I really don't known where to place my new
elements ;-(
So, If using <new Element> style, I need to specify an ID of some div,
for example:
<div id="somediv">
<script>
<!--
var span = new Element('span');
$('somediv').appendChild(span);
//-->
</script>
</div>
But, I don't want to specify an ID, I want just to <write> elements ;-
(
As I can see - it's impossible! Am I right?
on 2008-06-13 16:29
<html>
<head>
<script type="text/javascript">
function init() {
mySpan = new Element('span');
$('importantButton').observe('click', updateMyContent);
}
function updateMyContent() {
mySpan.update('Some important text');
$('canvas').appendChild(mySpan);
}
document.observe('dom:loaded', init);
</script>
</head>
<body>
<div id="canvas"></div>
<button id="importantButton">Press me to see some important text</
button>
</body>
</html>
on 2008-06-13 16:39
I would auggest that you take a different pespective on how you are developing your pages. You should be able to reference any element within your page. That means specifying an ID, or creating an element dynamically. You should strive to keep Javascript separate from your HTML. Bernie
on 2008-06-13 16:42
There is support for this in different browsers, but it's not standard.
In
Firefox, you could say:
Builder.dump();
function outputElement(e) {
return new XMLSerializer().serializeToString(e);
}
document.write(outputElement(TABLE({id: 'mytable'}, [
TR([
TD('cell 1'),
TD('cell 2'),
TD('cell 3')
])
])));
Still have to use document.write(), but you could build up the element
programmatically.
I agree with @blechler though, this really isn't the recommended
approach
for doing dynamic HTML development.
-Fred
On Fri, Jun 13, 2008 at 9:26 AM, AlannY <m@alanny.ru> wrote:
>
>
> But, I don't want to specify an ID, I want just to <write> elements ;-
> (
>
> As I can see - it's impossible! Am I right?
--
Science answers questions; philosophy questions answers.