Calling a function within itself (kindof recursion but ...)

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

    #1

    Calling a function within itself (kindof recursion but ...)

    <script>
    function testme(testvar) {
    if (testvar=='1') {
    alert('testvar= 1');
    document.open() ;
    document.write( 'This is just some text<br /><br />");
    document.write( '<a href="javascrip t:testme(\'2\') ;" >Next >>&nbsp;</a>');
    document.close( );
    }
    else if (testvar=='2') {
    alert('testvar= 2');
    }
    }
    </script>
    <a href="javascrip t:testme('1');" >Next >>&nbsp;</a>


    It seems that this should work, but alas, it doesn't. What I am emulating
    in this example is a case where you start off with a hyperlink that calls
    TESTME and produces another hyperlink. When you click on the hyperlink, it
    errors out and produces "object expected" in IE and "Error: callHelp is not
    defined" in NS. Why is it saying callHelp is not defined and how do I fix
    this?

    Thanks.


  • Jman

    #2
    Re: Calling a function within itself (kindof recursion but ...)


    "gf" <gfraley5@earth link.net> wrote in message
    news:dSpFc.2819 $oD3.2663@newsr ead1.news.pas.e arthlink.net...[color=blue]
    > <script>
    > function testme(testvar) {
    > if (testvar=='1') {
    > alert('testvar= 1');
    > document.open() ;
    > document.write( 'This is just some text<br /><br />");
    > document.write( '<a href="javascrip t:testme(\'2\') ;" >Next[color=green]
    >>&nbsp;</a>');[/color]
    > document.close( );
    > }
    > else if (testvar=='2') {
    > alert('testvar= 2');
    > }
    > }
    > </script>
    > <a href="javascrip t:testme('1');" >Next >>&nbsp;</a>
    >
    >
    > It seems that this should work, but alas, it doesn't. What I am emulating
    > in this example is a case where you start off with a hyperlink that calls
    > TESTME and produces another hyperlink. When you click on the hyperlink,[/color]
    it[color=blue]
    > errors out and produces "object expected" in IE and "Error: callHelp is[/color]
    not[color=blue]
    > defined" in NS. Why is it saying callHelp is not defined and how do I fix
    > this?
    >
    > Thanks.[/color]


    You just overwrote your entire document, with a new document that has no
    script in it... so then
    when you click on the link, it has no function to refer to any longer...

    If you included the function definition inside the text to be written to the
    document, then you might get it to work.

    Run this script and view the source; you see what now is in the document;
    not much, and certainly no function definition to be called a second time.

    It's not entirely clear to me why you would want to do anything quite like
    this.. but usually the best way to operate is to put everything inside of a
    string, contantinating every new statement into on big string or var, and
    then write out the var at the end out to the document with one
    document.write statement at the end, like:

    var a = "this is some text <br>";

    a += "this is some more text too <br>";

    a += "this is the final line of text to be written to the document. <br>";

    document.write (a);

    Also, you might want to write text into a DIV tag, or some other object that
    can act as a container so that the rest of your document is not affected
    (erased) when you write into it.

    Personally, I like looking at this kind of thing in a debugger; it's a
    little easier some times to see how things are working when you can watch
    the vars change as the script executes, and be able to see into DOM objects
    to see if they are null or assigned with ids and values as expected.

    http://www.mandala.com/javascript/debug_javascript.html
    Javascript Debugging with the MS Script Editor

    Ciao,
    Jeff










    Comment

    • gf

      #3
      Re: Calling a function within itself (kindof recursion but ...)

      Well, I chastised myself too soon :) Here is more of the real script, so I
      do not believe I am overwriting myself.

      <script>
      function callHelp(page) {
      var helpWin =
      window.open('', 'rwh_help','sta tus=1,menubar=0 ,toolbar=0,widt h=400,height=60 0
      ');
      with (helpWin.docume nt) {
      open();
      writeln('<html> <head><title>He lp</title></head>');
      writeln('<cente r><a href="http://somedomain.com" target="_blank" ><b
      style="color:bl ue;">Test</b></a></center>');
      writeln('<br /><table border=1>');
      if (page=='main') {
      writeln('<tr><t d>This is a test</td></tr><tr><td align="right">< a
      href="javascrip t:callHelp(\'pa ge2\');" >Next >>&nbsp;</a></td></tr>');
      }
      else if (page=='page2') {alert(page);
      writeln('page 2');
      }
      writeln('</table></html>');
      close();
      return;
      }
      }
      </script>

      "Jman" <likea@rock.com > wrote in message
      news:40e63c36$1 _4@news.athenan ews.com...[color=blue]
      >
      > "gf" <gfraley5@earth link.net> wrote in message
      > news:dSpFc.2819 $oD3.2663@newsr ead1.news.pas.e arthlink.net...[color=green]
      > > <script>
      > > function testme(testvar) {
      > > if (testvar=='1') {
      > > alert('testvar= 1');
      > > document.open() ;
      > > document.write( 'This is just some text<br /><br />");
      > > document.write( '<a href="javascrip t:testme(\'2\') ;" >Next[color=darkred]
      > >>&nbsp;</a>');[/color]
      > > document.close( );
      > > }
      > > else if (testvar=='2') {
      > > alert('testvar= 2');
      > > }
      > > }
      > > </script>
      > > <a href="javascrip t:testme('1');" >Next >>&nbsp;</a>
      > >
      > >
      > > It seems that this should work, but alas, it doesn't. What I am[/color][/color]
      emulating[color=blue][color=green]
      > > in this example is a case where you start off with a hyperlink that[/color][/color]
      calls[color=blue][color=green]
      > > TESTME and produces another hyperlink. When you click on the hyperlink,[/color]
      > it[color=green]
      > > errors out and produces "object expected" in IE and "Error: callHelp is[/color]
      > not[color=green]
      > > defined" in NS. Why is it saying callHelp is not defined and how do I[/color][/color]
      fix[color=blue][color=green]
      > > this?
      > >
      > > Thanks.[/color]
      >
      >
      > You just overwrote your entire document, with a new document that has no
      > script in it... so then
      > when you click on the link, it has no function to refer to any longer...
      >
      > If you included the function definition inside the text to be written to[/color]
      the[color=blue]
      > document, then you might get it to work.
      >
      > Run this script and view the source; you see what now is in the document;
      > not much, and certainly no function definition to be called a second time.
      >
      > It's not entirely clear to me why you would want to do anything quite like
      > this.. but usually the best way to operate is to put everything inside of[/color]
      a[color=blue]
      > string, contantinating every new statement into on big string or var, and
      > then write out the var at the end out to the document with one
      > document.write statement at the end, like:
      >
      > var a = "this is some text <br>";
      >
      > a += "this is some more text too <br>";
      >
      > a += "this is the final line of text to be written to the document. <br>";
      >
      > document.write (a);
      >
      > Also, you might want to write text into a DIV tag, or some other object[/color]
      that[color=blue]
      > can act as a container so that the rest of your document is not affected
      > (erased) when you write into it.
      >
      > Personally, I like looking at this kind of thing in a debugger; it's a
      > little easier some times to see how things are working when you can watch
      > the vars change as the script executes, and be able to see into DOM[/color]
      objects[color=blue]
      > to see if they are null or assigned with ids and values as expected.
      >
      > http://www.mandala.com/javascript/debug_javascript.html
      > Javascript Debugging with the MS Script Editor
      >
      > Ciao,
      > Jeff
      >
      >
      >
      >
      >
      >
      >
      >
      >
      >[/color]


      Comment

      • Mick White

        #4
        Re: Calling a function within itself (kindof recursion but ...)

        gf wrote:
        [color=blue]
        > Well, I chastised myself too soon :) Here is more of the real script, so I
        > do not believe I am overwriting myself.
        >
        > <script>
        > function callHelp(page) {
        > var helpWin =
        > window.open('', 'rwh_help','sta tus=1,menubar=0 ,toolbar=0,widt h=400,height=60 0
        > ');
        > with (helpWin.docume nt) {
        > open();
        > writeln('<html> <head><title>He lp</title></head>');
        > writeln('<cente r><a href="http://somedomain.com" target="_blank" ><b
        > style="color:bl ue;">Test</b></a></center>');
        > writeln('<br /><table border=1>');
        > if (page=='main') {
        > writeln('<tr><t d>This is a test</td></tr><tr><td align="right">< a
        > href="javascrip t:callHelp(\'pa ge2\');" >Next >>&nbsp;</a></td></tr>');[/color]

        writeln('<tr><t d>This is a test</td></tr><tr><td align="right">< a
        href="javascrip t:opener.callHe lp(\'page2\');" >Next>>&nbsp; </a></td></tr>');
        Mick

        [color=blue]
        > }
        > else if (page=='page2') {alert(page);
        > writeln('page 2');
        > }
        > writeln('</table></html>');
        > close();
        > return;
        > }
        > }
        > </script>
        >
        > "Jman" <likea@rock.com > wrote in message
        > news:40e63c36$1 _4@news.athenan ews.com...
        >[color=green]
        >>"gf" <gfraley5@earth link.net> wrote in message
        >>news:dSpFc.28 19$oD3.2663@new sread1.news.pas .earthlink.net. ..
        >>[color=darkred]
        >>><script>
        >>> function testme(testvar) {
        >>> if (testvar=='1') {
        >>> alert('testvar= 1');
        >>> document.open() ;
        >>> document.write( 'This is just some text<br /><br />");
        >>> document.write( '<a href="javascrip t:testme(\'2\') ;" >Next
        >>>
        >>>>&nbsp;</a>');
        >>>
        >>> document.close( );
        >>> }
        >>> else if (testvar=='2') {
        >>> alert('testvar= 2');
        >>> }
        >>> }
        >>></script>
        >>><a href="javascrip t:testme('1');" >Next >>&nbsp;</a>
        >>>
        >>>
        >>>It seems that this should work, but alas, it doesn't. What I am[/color][/color]
        >
        > emulating
        >[color=green][color=darkred]
        >>>in this example is a case where you start off with a hyperlink that[/color][/color]
        >
        > calls
        >[color=green][color=darkred]
        >>>TESTME and produces another hyperlink. When you click on the hyperlink,[/color]
        >>
        >>it
        >>[color=darkred]
        >>>errors out and produces "object expected" in IE and "Error: callHelp is[/color]
        >>
        >>not
        >>[color=darkred]
        >>>defined" in NS. Why is it saying callHelp is not defined and how do I[/color][/color]
        >
        > fix
        >[color=green][color=darkred]
        >>>this?
        >>>
        >>>Thanks.[/color]
        >>
        >>
        >>You just overwrote your entire document, with a new document that has no
        >>script in it... so then
        >>when you click on the link, it has no function to refer to any longer...
        >>
        >>If you included the function definition inside the text to be written to[/color]
        >
        > the
        >[color=green]
        >>document, then you might get it to work.
        >>
        >>Run this script and view the source; you see what now is in the document;
        >>not much, and certainly no function definition to be called a second time.
        >>
        >>It's not entirely clear to me why you would want to do anything quite like
        >>this.. but usually the best way to operate is to put everything inside of[/color]
        >
        > a
        >[color=green]
        >>string, contantinating every new statement into on big string or var, and
        >>then write out the var at the end out to the document with one
        >>document.writ e statement at the end, like:
        >>
        >>var a = "this is some text <br>";
        >>
        >>a += "this is some more text too <br>";
        >>
        >>a += "this is the final line of text to be written to the document. <br>";
        >>
        >>document.writ e (a);
        >>
        >>Also, you might want to write text into a DIV tag, or some other object[/color]
        >
        > that
        >[color=green]
        >>can act as a container so that the rest of your document is not affected
        >>(erased) when you write into it.
        >>
        >>Personally, I like looking at this kind of thing in a debugger; it's a
        >>little easier some times to see how things are working when you can watch
        >>the vars change as the script executes, and be able to see into DOM[/color]
        >
        > objects
        >[color=green]
        >>to see if they are null or assigned with ids and values as expected.
        >>
        >>http://www.mandala.com/javascript/debug_javascript.html
        >>Javascript Debugging with the MS Script Editor
        >>
        >>Ciao,
        >>Jeff
        >>
        >>
        >>
        >>
        >>
        >>
        >>
        >>
        >>
        >>[/color]
        >
        >
        >[/color]

        Comment

        • gf

          #5
          Re: Calling a function within itself (kindof recursion but ...)

          Of Course! Thank you so much for slapping me :lol:


          "Mick White" <mwhite13@BOGUS rochester.rr.co m> wrote in message
          news:AmEFc.365$ yd5.204@twister .nyroc.rr.com.. .[color=blue]
          > gf wrote:
          >[color=green]
          > > Well, I chastised myself too soon :) Here is more of the real script,[/color][/color]
          so I[color=blue][color=green]
          > > do not believe I am overwriting myself.
          > >
          > > <script>
          > > function callHelp(page) {
          > > var helpWin =
          > >[/color][/color]
          window.open('', 'rwh_help','sta tus=1,menubar=0 ,toolbar=0,widt h=400,height=60 0[color=blue][color=green]
          > > ');
          > > with (helpWin.docume nt) {
          > > open();
          > > writeln('<html> <head><title>He lp</title></head>');
          > > writeln('<cente r><a href="http://somedomain.com" target="_blank" ><b
          > > style="color:bl ue;">Test</b></a></center>');
          > > writeln('<br /><table border=1>');
          > > if (page=='main') {
          > > writeln('<tr><t d>This is a test</td></tr><tr><td align="right">< a
          > > href="javascrip t:callHelp(\'pa ge2\');" >Next >>&nbsp;</a></td></tr>');[/color]
          >
          > writeln('<tr><t d>This is a test</td></tr><tr><td align="right">< a
          >[/color]
          href="javascrip t:opener.callHe lp(\'page2\');" >Next>>&nbsp; </a></td></tr>');[color=blue]
          > Mick
          >
          >[color=green]
          > > }
          > > else if (page=='page2') {alert(page);
          > > writeln('page 2');
          > > }
          > > writeln('</table></html>');
          > > close();
          > > return;
          > > }
          > > }
          > > </script>
          > >
          > > "Jman" <likea@rock.com > wrote in message
          > > news:40e63c36$1 _4@news.athenan ews.com...
          > >[color=darkred]
          > >>"gf" <gfraley5@earth link.net> wrote in message
          > >>news:dSpFc.28 19$oD3.2663@new sread1.news.pas .earthlink.net. ..
          > >>
          > >>><script>
          > >>> function testme(testvar) {
          > >>> if (testvar=='1') {
          > >>> alert('testvar= 1');
          > >>> document.open() ;
          > >>> document.write( 'This is just some text<br /><br />");
          > >>> document.write( '<a href="javascrip t:testme(\'2\') ;" >Next
          > >>>
          > >>>>&nbsp;</a>');
          > >>>
          > >>> document.close( );
          > >>> }
          > >>> else if (testvar=='2') {
          > >>> alert('testvar= 2');
          > >>> }
          > >>> }
          > >>></script>
          > >>><a href="javascrip t:testme('1');" >Next >>&nbsp;</a>
          > >>>
          > >>>
          > >>>It seems that this should work, but alas, it doesn't. What I am[/color]
          > >
          > > emulating
          > >[color=darkred]
          > >>>in this example is a case where you start off with a hyperlink that[/color]
          > >
          > > calls
          > >[color=darkred]
          > >>>TESTME and produces another hyperlink. When you click on the[/color][/color][/color]
          hyperlink,[color=blue][color=green][color=darkred]
          > >>
          > >>it
          > >>
          > >>>errors out and produces "object expected" in IE and "Error: callHelp is
          > >>
          > >>not
          > >>
          > >>>defined" in NS. Why is it saying callHelp is not defined and how do I[/color]
          > >
          > > fix
          > >[color=darkred]
          > >>>this?
          > >>>
          > >>>Thanks.
          > >>
          > >>
          > >>You just overwrote your entire document, with a new document that has no
          > >>script in it... so then
          > >>when you click on the link, it has no function to refer to any longer...
          > >>
          > >>If you included the function definition inside the text to be written to[/color]
          > >
          > > the
          > >[color=darkred]
          > >>document, then you might get it to work.
          > >>
          > >>Run this script and view the source; you see what now is in the[/color][/color][/color]
          document;[color=blue][color=green][color=darkred]
          > >>not much, and certainly no function definition to be called a second[/color][/color][/color]
          time.[color=blue][color=green][color=darkred]
          > >>
          > >>It's not entirely clear to me why you would want to do anything quite[/color][/color][/color]
          like[color=blue][color=green][color=darkred]
          > >>this.. but usually the best way to operate is to put everything inside[/color][/color][/color]
          of[color=blue][color=green]
          > >
          > > a
          > >[color=darkred]
          > >>string, contantinating every new statement into on big string or var,[/color][/color][/color]
          and[color=blue][color=green][color=darkred]
          > >>then write out the var at the end out to the document with one
          > >>document.writ e statement at the end, like:
          > >>
          > >>var a = "this is some text <br>";
          > >>
          > >>a += "this is some more text too <br>";
          > >>
          > >>a += "this is the final line of text to be written to the document.[/color][/color][/color]
          <br>";[color=blue][color=green][color=darkred]
          > >>
          > >>document.writ e (a);
          > >>
          > >>Also, you might want to write text into a DIV tag, or some other object[/color]
          > >
          > > that
          > >[color=darkred]
          > >>can act as a container so that the rest of your document is not affected
          > >>(erased) when you write into it.
          > >>
          > >>Personally, I like looking at this kind of thing in a debugger; it's a
          > >>little easier some times to see how things are working when you can[/color][/color][/color]
          watch[color=blue][color=green][color=darkred]
          > >>the vars change as the script executes, and be able to see into DOM[/color]
          > >
          > > objects
          > >[color=darkred]
          > >>to see if they are null or assigned with ids and values as expected.
          > >>
          > >>http://www.mandala.com/javascript/debug_javascript.html
          > >>Javascript Debugging with the MS Script Editor
          > >>
          > >>Ciao,
          > >>Jeff
          > >>
          > >>
          > >>
          > >>
          > >>
          > >>
          > >>
          > >>
          > >>
          > >>[/color]
          > >
          > >
          > >[/color][/color]


          Comment

          Working...