Document.Write()

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Eric Mitchell

    Document.Write()

    Hello all,

    I am using the document.write( ) method to create new content on the same
    page, however... I need to create a new button using this method (button in
    HTML). Complicating the matter worse, I have a javascript function that
    needs to be called in the onClick event of the button. Is there any way of
    doing this? Here is my code, which gives me an error because the function is
    read as an object, and there is no object...

    <script language="javas cript">
    function firstFunction() {
    var buttonType="<in put type=button value=Proceed name=contOn
    onclick=secondF unction();>"
    document.write( buttonType);
    }

    function secondFunction( ){
    document.write( "Test string");
    }
    </script>

    The first function (firstFunction( )) is initiated by a button later in the
    code that I didn't feel was necessary to provide. However, as you can see in
    the third line of code, in the onClick method of the button... JavaScript
    reads secondFunction( ) as an object, not a function. How can I fix this?
    Thank you!

    -Eric


  • Evertjan.

    #2
    Re: Document.Write( )

    Eric Mitchell wrote on 24 jan 2005 in comp.lang.javas cript:
    [color=blue]
    > <script language="javas cript">
    > function firstFunction() {
    > var buttonType="<in put type=button value=Proceed name=contOn
    > onclick=secondF unction();>"
    > document.write( buttonType);
    >}
    >
    > function secondFunction( ){
    > document.write( "Test string");
    >}
    > </script>
    >[/color]

    Document.write( ) after completion of the page gives
    an automatic document.open() and
    thereby destroys the content of the page
    INCLUDING the javascript content.

    So your second function doesn't exist anymore.

    Use CSS to hide or display the button,
    or innerHTML,
    or Child DOM functions.

    --
    Evertjan.
    The Netherlands.
    (Replace all crosses with dots in my emailaddress)

    Comment

    • RobG

      #3
      Re: Document.Write( )

      Eric Mitchell wrote:[color=blue]
      > Hello all,
      >
      > I am using the document.write( ) method to create new content on the same
      > page, however... I need to create a new button using this method (button in
      > HTML). Complicating the matter worse, I have a javascript function that
      > needs to be called in the onClick event of the button. Is there any way of
      > doing this?[/color]

      Yes.
      [color=blue]
      > Here is my code, which gives me an error because the function is
      > read as an object, and there is no object...[/color]

      You must write valid HTML to the page or use DOM methods to create the
      HTML. Alternatively, write the stuff the page and hide/show it using
      the CSS display attribute.
      [color=blue]
      >
      > <script language="javas cript">[/color]

      <script type="text/javascript">
      [color=blue]
      > function firstFunction() {
      > var buttonType="<in put type=button value=Proceed name=contOn
      > onclick=secondF unction();>"[/color]

      You must write valid HTML, so quote all attributes and in particular,
      quote the onclick value:

      var buttonType = '<input type="button" value="Proceed" name="contOn"'
      + ' onclick="second Function();">'
      [color=blue]
      > document.write( buttonType);
      > }[/color]

      This will completely replace the entire contents of the document. Is
      that what you want to do? If not, you need to use some other method of
      adding the button to your page, preferably using DOM createElement, and
      attaching the onclick event.

      If that's what you want to do, put the above input inside a form so you
      can just add the new control as another child of the form.
      [color=blue]
      > function secondFunction( ){
      > document.write( "Test string");
      > }[/color]
      [...][color=blue]
      > reads secondFunction( ) as an object, not a function. How can I fix this?[/color]

      Some working code below, but I'm not sure it will help very much.

      <html><head><ti tle>play</title>
      </head><body>
      <script type="text/javascript">
      function firstFunction() {
      var buttonType='<in put type="button" value="Proceed" name="contOn" '
      + ' onclick="second Function();">'
      document.write( buttonType);
      }

      function secondFunction( ) {
      alert('I am the second function');
      }
      </script>
      <p>Here is some text</p>
      <button onclick="firstF unction();">fir stFunction</button>
      <p>Here is some more text</p>
      </body</html

      --
      Rob

      Comment

      Working...