Forum: Rails Spinoffs (closed, excessive spam) How to do <document.write> in Prototype style?

950af1c36225d95580d463ec728439cc?d=identicon&s=25 AlannY (Guest)
on 2008-06-13 15:02
(Received via mailing list)
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 ;-)
F490e41cd5de1393a9f6958b70dae6ab?d=identicon&s=25 Frederick Polgardy (Guest)
on 2008-06-13 15:41
(Received via mailing list)
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.
950af1c36225d95580d463ec728439cc?d=identicon&s=25 AlannY (Guest)
on 2008-06-13 16:27
(Received via mailing list)
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?
8c5868aade15ca78a6b4dcdf3cc59bf7?d=identicon&s=25 blechler (Guest)
on 2008-06-13 16:29
(Received via mailing list)
<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>
8c5868aade15ca78a6b4dcdf3cc59bf7?d=identicon&s=25 blechler (Guest)
on 2008-06-13 16:39
(Received via mailing list)
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
F490e41cd5de1393a9f6958b70dae6ab?d=identicon&s=25 Frederick Polgardy (Guest)
on 2008-06-13 16:42
(Received via mailing list)
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.
950af1c36225d95580d463ec728439cc?d=identicon&s=25 AlannY (Guest)
on 2008-06-13 18:17
(Received via mailing list)
OK, Alright ;-)
I understand you all ;-) Thank a lot ;-)

I have found already a better way with HTML abstraction ;-)

Thank you all ;-)
This topic is locked and can not be replied to.