-1

I have a html page where there is a link for example facebook.com

when someone clicks on the link, It should open on a new tab using javascript.

Basically I want that link to open in new tab using java script when someone clicks over it.

<script>
     $("facebook.com").on("click",function(){
         window.open('www.facebook.com','_blank');
     });
</script>
Himanshu Bhandari
  • 1,769
  • 2
  • 23
  • 37
Zmax Gera
  • 55
  • 4
  • 3
    Possible duplicate of [Open a URL in a new tab (and not a new window) using JavaScript](http://stackoverflow.com/questions/4907843/open-a-url-in-a-new-tab-and-not-a-new-window-using-javascript) – Divyesh Savaliya Feb 29 '16 at 12:43
  • Firstly, `facebook.com` is not a valid selector, you should provide the id or class of the `a` element. Secondly the URL you give to `window.open` should start with `http://` otherwise it will be interpreted as a relative path. Lastly, why not just put the `target="_blank"` attribute on the link in HTML and save having to use JS at all? – Rory McCrossan Feb 29 '16 at 12:44
  • well actualy it is complicated, i am working on a mvc php platform. I just want to replace tha link with _blank using javascript – Zmax Gera Feb 29 '16 at 12:49

1 Answers1

0

This code maybe help you:

$("#link").click(function(){
  var win = window.open('http://facebook.com/', '_blank');
  if(win){
      //Browser has allowed it to be opened
      win.focus();
  }else{
      //Broswer has blocked it
      alert('Please allow popups for this site');
  }
});

demo

  • thank you for the response, there are many link with the same button ID but i just want 1 particular link to open in a new tab. – Zmax Gera Feb 29 '16 at 12:59
  • @ZmaxGera You can change `#link` to special ID –  Feb 29 '16 at 13:01
  • i cant change the #link to special ID as I am using a dynamic slider where there are many link on the same ID – Zmax Gera Feb 29 '16 at 13:03