New Window Instead of Alert Box?

T

Tavish Muldoon

Hello,

I have same Javascript that performs a little quiz after choose
selections from radio buttons. It displays the number of answer that
one got right. But it puts them in an alert box - I want it to be in
a new window.

The alert box works fine and show the correct data.

The button:

<input type=button onClick="Showtotal()" value="TOTAL">

Showtotal():

function Showtotal(){
var answerText = "What is the
total?\n------------------------------------\n";
for(i=1;i<=5;i++){
answerText=answerText+"\nQuestion :"+i+"\n";
if(ans!=yourAns){
answerText=answerText+"\nThe correct answer was
"+ans+"\n"+explainAnswer+"\n";
}
else{
answerText=answerText+" \nCorrect! \n";
score++;
}
}
[some code snipped]
alert(answerText); <---I want this data to show up in a new
window....


I tried adding :

var w = window.open('','mywin','width=500,height=500'); to my declared
variables

then instead of the alert box line, it was replaced with:

w.document.write('<html><head><title>Choice</title></head><body><pre>'+answerText+'</pre></body></html>');
w.document.close();
w.focus();

</script>


What happens - when I open the original window - it opens up a blank
new windows with the orignal behind it. Pressing the TOTAL button
does nothing.

Help?

Thanks,

Tmuld.
 
K

kaeli

What happens - when I open the original window - it opens up a blank
new windows with the orignal behind it. Pressing the TOTAL button
does nothing.

Do this instead.
(watch for word-wrapping!)

Add this declaration to your code outside ANY function:
var h_window = null;

(allows window re-use)

Add this function to your code:

function showTotalWindow(answerText)
{
if (!h_window || h_window == null || typeof h_window == "undefined" ||
h_window.closed || !h_window.document)
h_window = window.open("","Choice","height=300,width=
300,scrollbars=yes,resizable=yes");
var doc = h_window.document;
doc.open();
doc.writeln("<html><head><title>Choice</title>");
doc.writeln("</head><body>");
doc.writeln("<pre>" + answerText + "</pre>");
doc.writeln("</body></html>");
doc.close();
h_window.focus();
return;
}

Change the line of code in ShowTotal() from
alert(answerText);
to
showTotalWindow(answerText);

Note: the above code modified from my help window popups, so it should work
in MSIE, Opera, and Firefox, at least.

HTH
--
 

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,001
Messages
2,570,254
Members
46,851
Latest member
CliftonCor

Latest Threads

Top