href with target blank in jQuery

1

I have an image inside a div.facebook that receives clike mouse and redirects to an external page:

$("div.facebook img").click(function() {
     $(location).attr('href','http://www.facebook.com');
 });

I happen to want it to open in another window.

I tried several options, including:

$("div.facebook img").click(function() {
     $(location).attr('href','http://www.facebook.com');              
                .attr('target','_blank')
 });

But it did not work.

What can be done in this case to continue with jQuery ?

    
asked by anonymous 12.05.2018 / 21:54

1 answer

0

Because your question brought up something you did not know, the Html target tag was discontinued by w3c Learn more here . So now you are prompted to use the rel tag with the external value like this:

$(document).ready(function(){
    $("a").attr("href", "http://www.facebook.com");
    $("a[rel=external]").click(function() {
        if (!this.oldHref) {
            this.oldHref = this.href;
            this.href = "";
        }
        window.open(this.oldHref);
    });
})

HTML:

<a rel="external">Aperte</a>
    
12.05.2018 / 22:29