Javascript popup in Internet Explorer!

I have a problem 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 works. I mean, my new window opens, but an error occurs. Error message:

'window.open (...)' is null, this is not an object.
Do you want to expect a script to run on this page?

then I have a button in the onclick event, it will call a function to close the current window, update the open function

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

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

I call it this way:

for the second part:

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

for the 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 really bothers me. Please, help;)

+4
source share
3 answers

When pop-ups opened with window.open are blocked by a pop-up blocker, the function of almost any modern browser these days, the return value of window.open () is not a window object, but null.

To get around these problems, you will need to check the value returned by window.open () before trying to call any methods on it.

The following code snippet demonstrates how to work 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, pop-ups should only be used as a last resort or in controlled environments (internal company website, etc.). Pop-up blockers tend to behave very inconsistently, and there can be more than one pop-up blocker in a given browser, so telling the user how to enable pop-ups for a given website is not necessarily a solution. Example: IE7 + Google Toolbar = two pop-up blockers.

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

The benefits are numerous:

  • Skinnable, so you can create a more consistent look to suit your site.
  • No popup blockers.
  • Good APIs and documentation that are consistent across most, if not all major browsers.

If you still need the newly opened β€œwindow” to contain an external URL, you could use the IFRAME inside the open dialog box.

Hope this helps,

Lior.

+6
source

Works great for me. Tested in IE6 / 7/8.

Of course, I could not verify it with your URLs, so I replaced them with simple file names. I suggest you try it also with simple file names and see if it succeeds then.

Besides...

You do not need to add "javascript:" at the beginning of the onclick attribute value.

It would be nice if you added the href = "..." attribute to the link with the same URL that you give open_window. Then it will become a real link, and you will not need to add a cursor: a pointer to it. For instance:

 <a href="?module=cart&task=add&id=<?=$res[xproductid]?>&popup=on" onclick="open_window(this.href, '500' , '500'); return false;"> ... 
0
source

Here is a way to get the cake and eat it. I have not tested it in all browsers, but it really should 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 a silly cursor thing, as it is a real link that works even when JS is turned off.

0
source

Source: https://habr.com/ru/post/1286168/


All Articles