SitePoint Sponsor

User Tag List

Results 1 to 6 of 6

Hybrid View

  1. #1
    SitePoint Addict
    Join Date
    Dec 2001
    Location
    Market Harborough, UK
    Posts
    206
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    window.onblur for a popup

    Odd one this (aren't they always).

    I have a popup window which absolutely must not be allowed to be placed in the background. Easy, I thought. Set the onblur property to window.close(). If I do this on a normal page, it works (either with window.onblur="window.close()" or by setting onblur-"window.close()" in the <body> tag).

    The problem is, the popup is dynamically created. I've tried using

    popupWin.document.write('<body onBlur=\"window.close();\">\n');

    and

    popupWin.onblur="window.close()";

    and neither works (although if I right-click and "view source" the first option correctly places the code in the tag).

    I suspect I need the window to re-process it's contents once written, but how do I get it to do that? Or is there something else I could so?

    The problem arises because if the user clicks the link that brings up the popup, backgrounds it, then clicks the link again, it all goes horribly wrong. An alternative would be if I could, using the code the ling fires off, detect if the window already exists and if so, bring it to the front, clear it out and then re-populate it.

    Anyone got any ideas?

    This is the last problem I need to solve before rolling out a major upgrade to our internal management system, so it's fairly urgent!

    Thank you one and all!


    Paul Simpson, BSc, MCNI, MCNE

  2. #2
    I'll take mine raw silver trophy MikeFoster's Avatar
    Join Date
    Dec 2002
    Location
    Alabama, USA
    Posts
    2,560
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Perhaps this. It uses the same window (and brings it to the front). If the window has been closed it opens another one.

    Code:
    function CWindow(name, w, h, x, y, loc, men, res, scr, sta, too)
    {
      var f = '';
      if (w && h) {
        if (document.layers) f = 'screenX=' + x + ',screenY=' + y;
        else f = 'left=' + x + ',top=' + y;
        f += ',width=' + w + ',height=' + h + ',';
      }
      f += ('location='+loc+',menubar='+men+',resizable='+res
        +',scrollbars='+scr+',status='+sta+',toolbar='+too);
      this.features = f;
      this.name = name;
      this.load = function(sUrl) {
        if (this.wnd && !this.wnd.closed) this.wnd.location.href = sUrl;
        else this.wnd = window.open(sUrl, this.name, this.features);
        this.wnd.focus();
        return false;
      }
    }

  3. #3
    SitePoint Addict
    Join Date
    Dec 2001
    Location
    Market Harborough, UK
    Posts
    206
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for that, Mike.

    I have to be honest, I haven't quite gotten my head around Object-Oriented stuff yet (although I know enough to know that I'm looking at OO code here!).

    the references to "this" refer, I assume, to the current window? If so, does that mean that your function should go in the popup window's code?

    For my purposes, I doan't actually need to worry about cross-browser compliance (it's an internal site and we have control over the desktop software) although I appreciate that for some people who may well read this thread, that is important. I also don't need a multi-purpose function with variable sizes, features etc etc.

    Could you do me a huge favour and highlight which part of the code actually does the "is this window already there? If so bring it forwards and empty it, if not create it" bit? Also, could you explain where this code executes, is it part of the parent's or the popup's script?

    Many thanks,

    Paul
    Paul Simpson, BSc, MCNI, MCNE

  4. #4
    I'll take mine raw silver trophy MikeFoster's Avatar
    Join Date
    Dec 2002
    Location
    Alabama, USA
    Posts
    2,560
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Paul, I've got to leave out right now, I'll be back mon. sorry about that. There's several people here who can explain it tho. cya

  5. #5
    I'll take mine raw silver trophy MikeFoster's Avatar
    Join Date
    Dec 2002
    Location
    Alabama, USA
    Posts
    2,560
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I've beefed up the CWindow documentation a little bit.

    the references to "this" refer, I assume, to the current window? If so, does that mean that your function should go in the popup window's code?
    No, CWindow goes either in a script element in the head of the html file or in an external js file which is included by the html file. The CWindow code is executed in the context of the parent window.

    For my purposes, I doan't actually need to worry about cross-browser compliance (it's an internal site and we have control over the desktop software) although I appreciate that for some people who may well read this thread, that is important.
    There is only one line that deals with cross-browser support. You can delete that if statement and its corresponding 'else' (but not the statment following the 'else').

    I also don't need a multi-purpose function with variable sizes, features etc etc.
    CWindow may be overkill for you then. Perhaps the following is all you need.
    Code:
    var childWindow = null;
    function winOpen(sUrl) {
      if (childWindow && !childWindow.closed) {childWindow.location.href  = sUrl;}
      else {childWindow = window.open(sUrl, "myWinName");}
      childWindow.focus();
      return false;
    }
    In your html it is used in the same way as CWindow:
    Code:
    <a target='myWinName' href='pg.html' onclick="return winOpen(this.href)">w1 link</a>

  6. #6
    SitePoint Addict
    Join Date
    Dec 2001
    Location
    Market Harborough, UK
    Posts
    206
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Many thanks!

    I think you've given me enough to get my system working. I'll post again if I get stuck.

    Paul
    Paul Simpson, BSc, MCNI, MCNE


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
  •