New Window Instead of Alert Box?

Discussion in 'Javascript' started by Tavish Muldoon, Mar 3, 2005.

  1. 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.
    Tavish Muldoon, Mar 3, 2005
    #1
    1. Advertising

  2. Tavish Muldoon

    kaeli Guest

    In article <>,
    enlightened us with...
    >
    > 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
    --
    --
    ~kaeli~
    He had a photographic memory that was never developed.
    http://www.ipwebdesign.net/wildAtHeart
    http://www.ipwebdesign.net/kaelisSpace
    kaeli, Mar 3, 2005
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?QW5nZWw=?=

    COMBO BOX INSTEAD OF DROP DOWN BOX

    =?Utf-8?B?QW5nZWw=?=, Jun 29, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    602
    Ken Cox [Microsoft MVP]
    Jun 29, 2004
  2. buran
    Replies:
    1
    Views:
    6,145
    Dimitri Glazkov
    Dec 29, 2004
  3. Ganesh
    Replies:
    0
    Views:
    541
    Ganesh
    Jun 29, 2007
  4. BWGames

    Alert box then close window

    BWGames, May 3, 2004, in forum: Javascript
    Replies:
    2
    Views:
    137
    Thomas 'PointedEars' Lahn
    May 24, 2004
  5. Replies:
    1
    Views:
    225
Loading...

Share This Page