Rewriting itself

L

laszlo

The function below (as fc.js) works with IE6.0, NS4.2 NS7.1. It
rewrites itself increasing the number.

However if I comment out the othervise unnecessary window.alert, it
still works with NS but falls into and endless loop in IE. The alert
helps only after the scr=fc.js line, otherwise the first recursion
causes the endless loop in IE

Any cue for the reason or more preferably workaround is appreciated. I
include the text for fc.js and the calling test.html

laszlo

***** fc.js *****

function testrut(cnt) {
var teststr = "Hello";
var nwf = window.open("", "testecske", "width=200,height=200");

cnt++;

nwf.document.writeln("<HTML><HEAD>");
nwf.document.writeln("<TITLE>recursive javascript</TITLE></HEAD>");

nwf.document.writeln("<BODY BGCOLOR=silver ><FORM><CENTER>");
nwf.document.writeln(teststr + cnt + "<BR><BR>");
nwf.document.writeln("<INPUT TYPE=BUTTON VALUE=repeat
onclick='testrut(" + cnt + ");'><BR><BR>");
nwf.document.writeln("<INPUT TYPE=BUTTON VALUE=close
onclick='window.close();'><BR><BR>");
nwf.document.writeln("</CENTER></FORM>");
nwf.document.writeln("<SCRIPT LANGUAGE='JavaScript1.2'
SRC='fc.js'></SCRIPT>");
nwf.document.writeln("</BODY></HTML>");
window.alert('hohoho');
nwf.document.close();
}

***** test.html *****


<HTML><HEAD>
<SCRIPT LANGUAGE='JavaScript1.2' SRC='fc.js'></SCRIPT>
<TITLE>Test</TITLE></HEAD>
<BODY><FORM><CENTER>
<INPUT TYPE=BUTTON VALUE=start onclick=testrut(0);>
</CENTER></BODY></HTML>
 
L

laszlo

I've get no answer, but fortunatelly found the resolution. The trick
is to put a hidden button into the calling html, set the id as
parameter, and for rewriting click this button from javascript. This
seems to be w/o hazard.

http://www.lzkiss.netfirms.com/test/recurse.html

*** fc.js ****
function testrut(name1, name2) {
var widget = document.getElementById(name1);
var cnt = widget.value;
cnt = cnt.replace(/rrr/i, '');
var teststr = "Hello=" + cnt;
cnt = parseInt(cnt) +1;
var nwf = window.open("", "testecske", "width=200,height=200");
nwf.document.writeln("<HTML><HEAD>");
nwf.document.writeln("<SCRIPT TYPE='text/JavaScript'
LANGUAGE=JavaScript1.2>");
nwf.document.writeln("var id1='" + name1 + "'");
nwf.document.writeln("var id2='" + name2 + "'");
nwf.document.writeln("function finish(flag) {");
nwf.document.writeln("var widget =
window.opener.document.getElementById(id1);");
nwf.document.writeln("widget.value = '' + " + cnt + ";");
nwf.document.writeln("if (flag)
window.opener.document.getElementById(id2).click();");
nwf.document.writeln("else window.close();");
nwf.document.writeln("}");
nwf.document.writeln("</SCRIPT>");
nwf.document.writeln("<TITLE>recursive javascript</TITLE></HEAD>");

nwf.document.writeln("<BODY BGCOLOR=silver ><FORM><CENTER>");
nwf.document.writeln(teststr + "<BR><BR>");
nwf.document.writeln("<INPUT TYPE=BUTTON VALUE=repeat
onclick='finish(1)'><BR><BR>");
nwf.document.writeln("<INPUT TYPE=BUTTON VALUE=close
onclick='finish(0);'><BR><BR>");
nwf.document.writeln("</CENTER></FORM>");
nwf.document.writeln("</BODY></HTML>");
nwf.document.close();
}

*** recurse.html ***

<HTML><HEAD>
<SCRIPT LANGUAGE='JavaScript1.2' SRC='fc.js'></SCRIPT>
<TITLE>Test</TITLE></HEAD>
<BODY><FORM><CENTER>
<INPUT TYPE=TEXT SIZE=8 NAME='I0' ID='I0' VALUE=1>
<SPAN STYLE={visibility:hidden}>
<INPUT TYPE=BUTTON ID='I1' NAME='I1' VALUE='' onclick='testrut("I0",
"I1");'> </SPAN>
<INPUT TYPE=BUTTON VALUE='start' onclick='testrut("I0", "I1");'>
</CENTER></FORM></BODY></HTML>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top