Connecting Tech Pros Worldwide Help | Site Map

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

gf
Guest
 
Posts: n/a
#1: Jul 23 '05
<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="javascript:testme(\'2\');" >Next >>&nbsp;</a>');
document.close();
}
else if (testvar=='2') {
alert('testvar=2');
}
}
</script>
<a href="javascript: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
Guest
 
Posts: n/a
#2: Jul 23 '05

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



"gf" <gfraley5@earthlink.net> wrote in message
news:dSpFc.2819$oD3.2663@newsread1.news.pas.earthl ink.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="javascript:testme(\'2\');" >Next[color=green]
>>&nbsp;</a>');[/color]
> document.close();
> }
> else if (testvar=='2') {
> alert('testvar=2');
> }
> }
> </script>
> <a href="javascript: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










gf
Guest
 
Posts: n/a
#3: Jul 23 '05

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','status=1,menubar=0,tool bar=0,width=400,height=600
');
with (helpWin.document) {
open();
writeln('<html><head><title>Help</title></head>');
writeln('<center><a href="http://somedomain.com" target="_blank"><b
style="color:blue;">Test</b></a></center>');
writeln('<br /><table border=1>');
if (page=='main') {
writeln('<tr><td>This is a test</td></tr><tr><td align="right"><a
href="javascript:callHelp(\'page2\');" >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.athenanews.com...[color=blue]
>
> "gf" <gfraley5@earthlink.net> wrote in message
> news:dSpFc.2819$oD3.2663@newsread1.news.pas.earthl ink.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="javascript:testme(\'2\');" >Next[color=darkred]
> >>&nbsp;</a>');[/color]
> > document.close();
> > }
> > else if (testvar=='2') {
> > alert('testvar=2');
> > }
> > }
> > </script>
> > <a href="javascript: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]


Mick White
Guest
 
Posts: n/a
#4: Jul 23 '05

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','status=1,menubar=0,tool bar=0,width=400,height=600
> ');
> with (helpWin.document) {
> open();
> writeln('<html><head><title>Help</title></head>');
> writeln('<center><a href="http://somedomain.com" target="_blank"><b
> style="color:blue;">Test</b></a></center>');
> writeln('<br /><table border=1>');
> if (page=='main') {
> writeln('<tr><td>This is a test</td></tr><tr><td align="right"><a
> href="javascript:callHelp(\'page2\');" >Next >>&nbsp;</a></td></tr>');[/color]

writeln('<tr><td>This is a test</td></tr><tr><td align="right"><a
href="javascript:opener.callHelp(\'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.athenanews.com...
>[color=green]
>>"gf" <gfraley5@earthlink.net> wrote in message
>>news:dSpFc.2819$oD3.2663@newsread1.news.pas.eart hlink.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="javascript:testme(\'2\');" >Next
>>>
>>>>&nbsp;</a>');
>>>
>>> document.close();
>>> }
>>> else if (testvar=='2') {
>>> alert('testvar=2');
>>> }
>>> }
>>></script>
>>><a href="javascript: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.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=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]
gf
Guest
 
Posts: n/a
#5: Jul 23 '05

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


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


"Mick White" <mwhite13@BOGUSrochester.rr.com> 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','status=1,menubar=0,tool bar=0,width=400,height=600[color=blue][color=green]
> > ');
> > with (helpWin.document) {
> > open();
> > writeln('<html><head><title>Help</title></head>');
> > writeln('<center><a href="http://somedomain.com" target="_blank"><b
> > style="color:blue;">Test</b></a></center>');
> > writeln('<br /><table border=1>');
> > if (page=='main') {
> > writeln('<tr><td>This is a test</td></tr><tr><td align="right"><a
> > href="javascript:callHelp(\'page2\');" >Next >>&nbsp;</a></td></tr>');[/color]
>
> writeln('<tr><td>This is a test</td></tr><tr><td align="right"><a
>[/color]
href="javascript:opener.callHelp(\'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.athenanews.com...
> >[color=darkred]
> >>"gf" <gfraley5@earthlink.net> wrote in message
> >>news:dSpFc.2819$oD3.2663@newsread1.news.pas.eart hlink.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="javascript:testme(\'2\');" >Next
> >>>
> >>>>&nbsp;</a>');
> >>>
> >>> document.close();
> >>> }
> >>> else if (testvar=='2') {
> >>> alert('testvar=2');
> >>> }
> >>> }
> >>></script>
> >>><a href="javascript: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.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.[/color][/color][/color]
<br>";[color=blue][color=green][color=darkred]
> >>
> >>document.write (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]


Closed Thread