Chris Burton
Chris Burton

Reputation: 1205

jQuery switch statement(?)

I am trying to figure out how to write a "switch statement" so if one particular (let's call it 'yourdomain.com') external URL matches it does not open in a new window, the rest will. Can someone lend a hand in teaching me?

jQuery(function ($) {
    $('a[href^="http://"]')
        .not('[href*="http://mydomain.com"]')
        .attr('target', '_blank');

e.g.

mydomain.com = this will not open a new window because it's my URL

yourdomain.com = this particular URL will not open in a new window even though it's an external URL

anyotherdomain.com = every other external URL besides the one above will open in a new window

Upvotes: 0

Views: 164

Answers (2)

Chris Burton
Chris Burton

Reputation: 1205

Solved it by adding another .not()

jQuery(function ($) {
    $('a[href^="http://"]')
        .not('[href*="http://mydomain.com"]').not('[href*="http://yourdomain.com"]')
        .attr('target', '_blank');

Upvotes: 1

anche
anche

Reputation: 2884

$(function() {
     var elms = $('a[href^="http://"]');
     $.each(elms, function() {
          var current_link = $(this).attr("href");
          if (current_link == "yoururl") {
               $(this).attr('target', '_blank');
          }
     });
});

could this be helpful?

Upvotes: 1

Related Questions