TruMan1
TruMan1

Reputation: 36158

How do I exclude elements that have a "target" defined (for opening external links in new windows)?

I'm not sure if this script is working properly. I do not want to overwrite existing targets. How can I adjust this to exclude links that already have a target defined?

$("a[@href^='http://']").not("[@href*='" + window.location.host + "']").attr('target','_blank');

Upvotes: 0

Views: 113

Answers (1)

cletus
cletus

Reputation: 625347

Use .not("[target]") to exclude links with a target attribute defined:

$("a[href^='http://']").not("[target]")
  .not("[href*='" + window.location.host + "']")
  .attr('target','_blank');

Also the syntax of putting @ in front of attribute selectors was deprecated in 1.2 and removed in 1.3.

Upvotes: 1

Related Questions