简体   繁体   中英

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>

Hi. 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. Thank u

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

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?

将href属性更改为href="#"

解决它的另一种方法是

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM