简体   繁体   中英

Open new tab on div element click with javascript

I have a piece of code that supposed to open a link in a new tab once per session when user clicks on a specific class or id of html element. but for some reason it's doesn't work.

var clickCounter = 0;
function bodyimgclick(e){
  if( clickCounter == 0 ) {

var homepage = document.querySelector('.post-body img');
    var popUp = function(e) {
      window.open ("http://google.com");
      homepage.removeEventListener('click', popUp, false);
    }

    homepage.addEventListener('click', popUp, false);
 }
  clickCounter++;
}

change window.open (" http://google.com "); window.open(' http://google.pl ', '_blank');

The additional ('_blank') argument opens a new tab.

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