kamal
kamal

Reputation: 1193

How to write JavaScript which works normally in Chrome and IE?

function popupaddpackage(urlToOpen){
  var window_width = screen.availWidth/2;
  var window_height = screen.availHeight;
  var window_left = (screen.availWidth/2)-(window_width/2);
  var window_top = (screen.availHeight/2)-(window_height/2);
  var winParms = "Status=yes" + ",resizable=yes" + ",height="+window_height+",width="+window_width + ",left="+window_left+",top="+window_top;
  var newwindow = window.open(urlToOpen,'_blank',winParms);
  newwindow.focus();
}

<a href="" onclick="popupaddpackage('popupchange.jsp?extkey=<%=extKey%>&classId=<%=classid%>')">OPEN POPUP</a>

I use this js function to open pop up screen in my jsp, and this works normally in Chrome but in IE doesn't work normally. In Chrome - pop up screen opens and the page behind remains stable, but in IE - pop up screen opens and the page behind redirects to index.jsp (the page behind is property.jsp). How to solve it?

Upvotes: 0

Views: 206

Answers (4)

Alba Mendez
Alba Mendez

Reputation: 4635

Please, launch JS pop-ups the right way and the problem should be gone.

In your case, it would be something like:

function popupaddpackage(link) {
  var window_width = screen.availWidth/2;
  var window_height = screen.availHeight;
  var window_left = (screen.availWidth/2)-(window_width/2);
  var window_top = (screen.availHeight/2)-(window_height/2);
  var winParms = "Status=yes" + ",resizable=yes" + ",height="+window_height+",width="+window_width + ",left="+window_left+",top="+window_top;
  var newwindow = window.open(link.href,link.target,winParms);
  newwindow.focus();
}

And for the HTML:

<a href="popupchange.jsp?extkey=<%=extKey%>&classId=<%=classid%>" target="_blank" onclick="popupaddpackage(this);return false;">OPEN POPUP</a>

That way, you:

  1. cleanly separate logic from markup
  2. help search engines understand (index) your page correctly
  3. users without javascript can also open the page without problems

Upvotes: 4

Alex
Alex

Reputation: 23310

Another way of fixing it would be

<a href="javascript:void(0);" onclick="...your function ... ">OPEN POPUP</a>

Upvotes: -1

Mike Sav
Mike Sav

Reputation: 15321

Have you tried adding return false to the last line of popupaddpackage(urlToOpen)? For SEO reasons it may be advantageous to put a URL in the href and what is JavaScript is disabled?

Upvotes: 1

Exelian
Exelian

Reputation: 5888

Change the href attribute to href="#"

Upvotes: -1

Related Questions