views:

2761

answers:

3

hi , 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 ;)

A: 

Works perfectly fine for me. Tested in IE6/7/8.

Of course I couldn't test it with your URLs so I replaced these with simple filenames. I'd suggest you try it also with simple filenames and see if it also fails then.

Beside that...

You don't need to add "javascript:" at the beginning of onclick attribute value.

It would also be good if you added a href="..." attribute to the link with the same URL that you give to open_window. Then it would become a real link and you wouldn't have to add cursor:pointer to it. For example:

<a href="?module=cart&task=add&id=<?=$res[xproductid]?>&popup=on"
   onclick="open_window(this.href, '500' , '500'); return false;"> ...
Rene Saarsoo
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 .
mehdi
+3  A: 

Hi.

When popup windows opened using window.new 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.

Lior Cohen
+1 on using a modal jQuery dialog.
Robert Claypool
A: 

Here is a way to have your cake and eat it too I have not tested it on all browsers but it should really work

function open_window(url,target,w,h) { //opens new window 
  var parms = "width="+w+",height="+h+",menubar=no,location=no,resizable,scrollbars,top=500,left=500";
  var win = window.open(url,target,parms);
  if (win) {
    win.focus();
    return false; // cancel the onClick
  }
  return true; // make the link perform as normal
}

Using the link

<a href="?module=cart&task=add&id=<?=$res[xproductid]?>&popup=on"
target="newWin" 
onclick="return open_window(this.href,this.target,500,500)"
id="addtocard"><img src="../images/new_theme/buy_book.gif" width="123" border="0"/></a>

which even saves you the silly cursor thing since it is an actual link which works even when JS is turned off