href with target Blank on jQuery

Asked

Viewed 104 times

1

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

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

Turns out I’d like it to open in another window.

I tried several options, among them:

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

But it didn’t work.

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

1 answer

0

Your question face brought up something you didn’t know, the Html tag target was discontinued by w3c learn more here. So now it is indicated to use the tag rel with the value External thus:

$(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>
  • Failed. Redirect occurs. But NOT with target Blank

  • I edited the answer.

  • does <img rel="External" src='' /> work? Because if it works, just set default to normal href

Browser other questions tagged

You are not signed in. Login or sign up in order to post.