Results 1 to 8 of 8

Thread: Close HTML Page

  1. #1
    Silver Lounger
    Join Date
    Dec 2000
    Location
    California, USA
    Posts
    1,758
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Close HTML Page

    I'm a novice at Java code and am having an issue with closing a window. The code I'm using prompts the user to select yes or no to close the window. I want to be able to close the window without a prompt. Is there a better way to write the code?

    <HTML>
    <HEAD>
    <SCRIPT LANGUAGE="JavaScript"></SCRIPT>
    </HEAD>

    <BODY>

    </BODY>
    </HTML>


    Thanks,
    John

  2. #2
    Plutonium Lounger Leif's Avatar
    Join Date
    Dec 2000
    Location
    U.K.
    Posts
    14,010
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Close HTML Page

    Do you want it to close automatically after a period of time?

    If you simply want a link to click on - e.g. the word "Close" - try this in the body:<pre>Close</pre>


  3. #3
    Uranium Lounger
    Join Date
    Jan 2001
    Location
    Cincinnati, Ohio, USA
    Posts
    7,089
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Close HTML Page

    Before someone else points this out, this is JavaScript - not Java. Similar names but completely different animals. If Leif's suggestion isn't what you're aiming for, try this. It will put a form button on the page for the user to click.
    <pre><form>
    <input type="button" value="Close Window" onClick="parent.close()">
    </form></pre>

    This will work only if the window was spawned by another existing browser window. If you're trying to close the parent window itself, you'll get a prompted - which I suspect is what's happening. There is an additional example at the Javascript Source that may be better for your purposes - this will close the window without a prompt.
    -Mark

  4. #4
    Silver Lounger
    Join Date
    Dec 2000
    Location
    California, USA
    Posts
    1,758
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Close HTML Page

    Leif,

    Yes, I would like it to close automatically without any user interaction.

    John

  5. #5
    Uranium Lounger
    Join Date
    Jan 2001
    Location
    Cincinnati, Ohio, USA
    Posts
    7,089
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Close HTML Page

    It's worth noting that unless the window was created with a Javascript, you will get the prompt - I should have added that in my other post.
    -Mark

  6. #6
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Close HTML Page

    Maybe something like this?

    <HTML>
    <HEAD>
    <TITLE>Now you see me, now you don't</TITLE>
    <script>
    function shutMe()
    {
    // Set a two second time-out on this window
    window.setTimeout("window.close()", 2000);
    }
    </script>
    </HEAD>
    <BODY onLoad="shutMe();">
    ...blah blah blah...
    </BODY>
    </HTML>

    This will trigger the security message under the circumstances described above. The setTimeout method also is one way that pop-up ads open after a delay, so it's conceivable that some ad blockers might interfere.

  7. #7
    Silver Lounger
    Join Date
    Dec 2000
    Location
    California, USA
    Posts
    1,758
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Close HTML Page

    I'm still prompted for a YES or NO to close the window.

    Thanks,
    John

  8. #8
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Close HTML Page

    If you mean you're prompted by the browser, it's because of the way the window was opened. You will get this warning if the window was NOT opened by JavaScript. Can you control how this window opens?
    Attached Images Attached Images

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •