javascript popup issue In Internet Explorer !

24,773

When popup windows opened using window.open are blocked by a popup blocker, a feature of pretty much any modern browser these days, the return value of window.open() is not a window object, but null.

In order to circumvent these issues you would need to test the value returned by window.open() before attempting to invoke any methods on it.

Below is a piece of code to demonstrate how to go around this problem:

function open_window(Location,w,h) //opens new window
{
  var options = "width=" + w + ",height=" + h;
  options += ",menubar=no,location=no,resizable,scrollbars,top=500,left=500";

  var newwin = window.open(Location,'newWin',options);

  if (newwin == null)
  {
    // The popup got blocked, notify the user
    return false;
  }

  newwin.focus();
}

In general, popup windows should be used only as a last resort or in controlled environments (internal company website, etc). Popup blockers tend to behave in very inconsistent ways and there may be more than a single popup blocker installed in a given browser so instructing the user on how to allow popups for a given website is not necessarily a solution. Example: IE7 + Google toolbar = two popup blockers.

If I may suggest, perhaps you should consider using something like this: http://jqueryui.com/demos/dialog/

The advantages are numerous:

  1. Skinnable, so you can create a more consistent look to match your website.
  2. No popup blockers.
  3. Good API and documentation that is consistent across most, if not all, major browsers.

If you still require that the newly opened "window" contain an external URL, you could use an IFRAME inside the opened dialog window.

Hope this helps,

Lior.

Share:
24,773
mehdi
Author by

mehdi

Updated on July 09, 2022

Comments

  • mehdi
    mehdi almost 2 years

    i have Problem with opening popups in javascript i have this function to open my popups in IE6 and IE7:

    function open_window(Location,w,h) //opens new window
    {
      var win = "width="+w+",height="+h+",menubar=no,location=no,resizable,scrollbars,top=500,left=500";
      alert(win) ;
      window.open(Location,'newWin',win).focus();
    
    }
    

    it's working . i mean my new window opens but an error occurs. The Error Message is :

    'window.open(...)' is null is not an object.
    do you want to countinue running script on this page ?

    then i have button in onclick event it's will call a function to close current window an refresh the opener function is

    function refreshParent(location) 
    {
      window.opener.location.href = location ; 
      window.close();
    }
    

    it's also gives me error : window.opener.location is null or not an object but i'm sure i'm passing correct parameters

    i call it like this :

    for second part :

    <input type="button" name="pay" value="test" onclick="refreshParent('index.php?module=payment&task=default')" >
    

    for first part :

    <a onclick="javascript:open_window('?module=cart&task=add&id=<?=$res[xproductid]?>&popup=on','500' , '500')"  style="cursor:pointer" id="addtocard"> <img src="../images/new_theme/buy_book.gif" width="123" border="0"/> </a>
    

    it's really confuse me . Please Help ;)

    • Cerebrus
      Cerebrus almost 15 years
      Sounds like a Popup blocker issue.
    • Rene Saarsoo
      Rene Saarsoo almost 15 years
      Well I didn't understand a single word, but I think I managed to find the page in question. I also maneged to open the popup and close it from the button inside that - after that the page refreshed itself. I didn't notice any errors. Everything seemed to work just fine. Maybe it's something in your computer? Have you tried it in someone elses?
  • mehdi
    mehdi almost 15 years
    however it still gaves me error Please Check this site www.pouran.net unfortunately this web site in Persian language and i don't think you are comfort with that . so if you can go ahead and try to add something to your basket then you see the error message . tanks .