SitePoint Sponsor

User Tag List

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

    external window.close()?

    Can someone please give me an example of using window.close() from an external .js file. In other words, zero javascript in the the HTML source. Thanks.

  2. #2
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,182
    Mentioned
    191 Post(s)
    Tagged
    2 Thread(s)
    The code I gave in your other thread - http://www.sitepoint.com/forums/showthread.php?t=559942 - can be put in an external javascript file
    external.js
    Code:
    function say_hi(){
    child = window.open("child.html");
    }
    function say_bye(){
    child.close();
    }
    close.html
    HTML Code:
    <html><head><title>close window test</title>
    <script type="text/javascript" src="external.js"></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>

  3. #3
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,825
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Your close.html file still contains JavaScript.

    To have zero JavaScript in the HTML you need to move those onclick calls into the external JavaScript as well.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  4. #4
    SitePoint Guru tictike's Avatar
    Join Date
    Apr 2008
    Location
    Canada
    Posts
    863
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I should have been more specific. I have a pop up window that I want to close from within that newly popped up window. I'm trying to avoid putting <p><a href="javascript:window.close()">close Window</a></p> in that newly popped up window. Is there a way to close a pop up using a link that's inside the new popped up window, yet maintain 100&#37; unobtrusive javascript?

  5. #5
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,182
    Mentioned
    191 Post(s)
    Tagged
    2 Thread(s)
    As felgall stated, to be 100% free of javascript within the mark-up the "onclick" should be in the external file too. This could be done with an "Event Listener", but last I knew IE didn't support it.

  6. #6
    SitePoint Guru tictike's Avatar
    Join Date
    Apr 2008
    Location
    Canada
    Posts
    863
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you remember check back tomorrow. I'll post code that I have (that doesn't work) and hopefully you'll be able to point out why.

  7. #7
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,825
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by Mittineague View Post
    As felgall stated, to be 100% free of javascript within the mark-up the "onclick" should be in the external file too. This could be done with an "Event Listener", but last I knew IE didn't support it.
    Event Listeners are JavaScript only. JScript (as used by Internet Explorer) uses attachEvent instead. attachEvent is not quite as powerful as event listeners since one of the rarely used options is not available there but a simple piece of code can easily get both linked in so that the appropriate one runs.

    http://javascript.about.com/library/bldom21.htm has an example of how to combine the two.

    Alternatively you can give the tag an id (eg. <div id="open">) and then add the onclick using

    document.getElementById('open').onclick = say_hi;

    just make sure that the div is created before the javaScript statement runs.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  8. #8
    SitePoint Guru tictike's Avatar
    Join Date
    Apr 2008
    Location
    Canada
    Posts
    863
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I was making an example to post here and I got it to work. I gave a link in the popped up window an id and assigned a function to that id to run self.close().


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
  •