SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Guru tictike's Avatar
    Join Date
    Apr 2008
    Location
    Canada
    Posts
    863
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    window.close() in an extenal file?

    Is it possible to put window.close() in an external file? I read that window.close() can only be used if the window is opened using window.open(), so, I'm not sure if that means I have to use that method from within the new opened window like this, for example:

    <p><a href="javascript:window.close();">close window</a></p>

    Of course, I'd like to put it in an external file.

  2. #2
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,290
    Mentioned
    198 Post(s)
    Tagged
    3 Thread(s)
    Yes, for security reasons, window.close() only works for a window that was either opened by window.open() or directly by the browser. That is, if you use your browser to open a page that has window.close() it will work (though maybe with a "trying to close window" alert).

  3. #3
    SitePoint Guru tictike's Avatar
    Join Date
    Apr 2008
    Location
    Canada
    Posts
    863
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Can you give an example script?

  4. #4
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,290
    Mentioned
    198 Post(s)
    Tagged
    3 Thread(s)
    You mean like
    HTML Code:
    <html><head><title>close test</title>
    <script type="text/javascript">
    function say_bye(){
    window.close();
    }
    </script>
    </head>
    <body>
    <span onclick="say_bye();">click here to close this window</span>
    </body></html>

  5. #5
    SitePoint Guru tictike's Avatar
    Join Date
    Apr 2008
    Location
    Canada
    Posts
    863
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes, but how would that be coded for an external javascript file?

  6. #6
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,290
    Mentioned
    198 Post(s)
    Tagged
    3 Thread(s)
    Something like
    child.html
    HTML Code:
    <html><head><title>child page</title>
    </head>
    <body>
    child page
    </body>
    </html>
    close.html
    HTML Code:
    <html><head><title>close window test</title>
    <script type="text/javascript">
    function say_hi(){
    child = window.open("child.html");
    }
    function say_bye(){
    child.close();
    }
    </script>
    </head>
    <body>
    <div onclick="say_hi();">click here to open child window</div>
    <div onclick="say_bye();">click here to close child window</div>
    </body></html>


Bookmarks

Posting Permissions

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