SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Evangelist
    Join Date
    Jan 2005
    Posts
    425
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Popup window focus

    How would one do the following in Javascript:
    Somebody clicks on a link, and Javascript checks if the window called "popup" has already been opened, if it hasn't it opens it. If the user is clicking the link a second time it brings the popup to focus rather than re-opening it.

    I've tried something like:
    PHP Code:
    <script language="javascript" type="text/javascript"><!--
     var 
    winref null;
    function 
    addWindow(add_window_url) {

    if (
    winref==null || winref.closed) {
             
    winref window.open(add_window_url,'popup');
    } else {
    winref.focus();
    }
    return 
    false;
    }
    //--></script> 
    But that didn't work.

  2. #2
    Caveat surfer Buddy Bradley's Avatar
    Join Date
    May 2003
    Location
    Cambridge, UK
    Posts
    2,366
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just using the same target ('popup' in your example) will do what you want regarding links appearing in the same window.

    To focus the page, call window.focus() in the page you're loading.

  3. #3
    SitePoint Evangelist
    Join Date
    Jan 2005
    Posts
    425
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks, I want to do it from the parent window, as the popup window is a form window, so if the person re-clicks the link to launch the popup - I want it to gain focus, rather than reload - as this would cause any entered form data to be lost.

    This is why I want the smarts to decide if to load popup OR to set focus to already open popup to be in parent window.

  4. #4
    SitePoint Evangelist
    Join Date
    Jan 2005
    Posts
    425
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Any other ideas?

  5. #5
    CSS & JS/DOM Adept bronze trophy
    Join Date
    Mar 2005
    Location
    USA
    Posts
    5,482
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try this.
    Code:
    function addWindow(add_window_url) {
      if(!winref || winref.closed) {
        winref = window.open(add_window_url,'popup');
      } else {
        setTimeout("winref.focus();",1);
      }
      return false;
    }
    We miss you, Dan Schulz.
    Learn CSS. | X/HTML Validator | CSS validator
    Dynamic Site Solutions
    Code for Firefox, Chrome, Safari, & Opera, then add fixes for IE, not vice versa.

  6. #6
    SitePoint Evangelist
    Join Date
    Jan 2005
    Posts
    425
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks...That seems to work in IE7 (it also works in IE7 without the setTimeout) - BUT neither version I can get to work in firefox. In firefox(2) the focus still doesn't change.

  7. #7
    SitePoint Evangelist
    Join Date
    Jan 2005
    Posts
    425
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Any suggestions as to how to get this to work in FireFox?

  8. #8
    SitePoint Evangelist
    Join Date
    Jan 2005
    Posts
    425
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    guess not

    Is this a Bug in firefox?


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
  •