Javascript: open & close new window on image's onMouseOver & onMouseOut, but only if new window onMouseOver = true

yerty picture yerty · Jul 21, 2011 · Viewed 8.7k times · Source

thank you all for helping me previously with my Javascripting problems. My current problem is that I need to open & close a new window on an image's onMouseOver & onMouseOut, respectively, but if the new window onMouseOver == true then I don't want the new window to close.

I am sure there is a simple solution, but I can't seem to figure out a way to cancel the image's onMouseOut="closeDetails();" if the user hovers over the New Window. Below is most of the code I am dealing with. Thanks in advance for your help.

<body>
   <img  name="img1" id="img1" onMouseOver="windowDelay(this);"
           onMouseOut="closeDetails();" src="images/127.jpg" height="240" width="166"/>
</body>

<script language="JavaScript" type="text/javascript">

// This opens the movie details pop-up after an
// half second interval.
function windowDelay(thatImg)
{
    winOpenTimer = window.setTimeout(function() {openDetails(thatImg);}, 2000);
}


// This is the function that will open the
// new window when the mouse is moved over the image
function openDetails(thatImg) 
{
    // This creates a new window and uses the hovered image name as the window 
    // name so that it can be used in the that window's javascript 
    newWindow = open("", thatImg.name,"width=400,height=500,left=410,top=210");

    // open new document 
    newWindow.document.open();


    // Text of the new document
    // Replace your " with ' or \" or your document.write statements will fail
    newWindow.document.write("<html><head><title>Movies</title>");
    newWindow.document.write("<script src='myDetails.js' type='text/javascript'>");
    newWindow.document.write("</script></head>");
    newWindow.document.write("<body bgcolor='white' onload='popUpDetails();'>");
    newWindow.document.write("... SOME OTHER HTML....");
    newWindow.document.write("</body></html>");


    // close the document
    newWindow.document.close(); 
}



// This is the function that will call the
// closeWindow() after 2 seconds
// when the mouse is moved off the image.
function closeDetails() 
{
    winCloseTimer = window.setTimeout("closeWindow();", 2000);
}

// This function closes the pop-up window
// and turns off the Window Timers
function closeWindow()
{
    // If popUpHover == true then I do not want
    // the window to close
    if(popUpHover == false)
    {
        clearInterval(winOpenTimer); 
        clearInterval(winCloseTimer);
        newWindow.close();
    }
}

function popUpDetails()
{
    // This will be used to prevent the Details Window from closing
    popUpHover = true;

    // Below is some other javascript code...
}
</script> 

Answer

benekastah picture benekastah · Jul 21, 2011

I would recommend against using a new browser window for this task. Try something like this:

var popup = {
  open = function () {
    if (this.element == null) {
      // create new div element to be our popup and store it in the popup object 
      this.element = document.createElement('div');
      this.element.id = "myPopup";
      // you don't need a full html document here. Just the stuff you were putting in the <body> tag before
      this.element.innerHTML = "<your>html</here>";
      // Some bare minimum styles to make this work as a popup. Would be better in a stylesheet
      this.element.style = "position: absolute; top: 50px; right: 50px; width: 300px; height: 300px; background-color: #fff;";
    }
    // Add it to your <body> tag
    document.body.appendChild(this.element);
    // call whatever setup functions you were calling before
    popUpDetails();
  },
  close = function () {
    // get rid of the popup
    document.body.removeChild(this.element);
    // any other code you want
  }
};

// The element you want to trigger the popup
var hoverOverMe = document.getElementById("hoverOverMe");
// set our popup open and close methods to the proper events
hoverOverMe.onmouseover = popup.open;
hoverOverMe.onmouseout = popup.close;

That should do it. It's much easier to control than a new browser window. You will want to tweak the CSS yourself.

EDIT:

Here are instructions to do this with an actual window. To reiterate, using an actual window is not the best way to accomplish this task. A stylized div tag to look like a window is better because it offers more control, as well as standardized functionality across browsers. However, if you must use a window, here it is:

// You can use many principles from the example above, but I'll give the quick version
var popup;
var hoverOverMe = document.getElementById("hoverOverMe");

hoverOverMe.onmouseover = function () {
  popup = window.open("path_to_content", "popup");
};
hoverOverMe.onmouseout = function () {
  popup.close();
};

It works, but not very well (IMHO). If the user has their settings such that new windows open in new tabs (as I do), then a tab will open up. Javascript has no control over that. In Firefox, the new tab will open and gain focus, at which point it immediately closes because hoverOverMe had its onmouseout event fired (which obviously closes the window). I imagine you'd have this same problem with an actual window, too.