Skip to main content

Bug Tracker

Side navigation

#9684 closed bug (invalid)

Opened June 28, 2011 03:09PM UTC

Closed June 28, 2011 05:45PM UTC

.attr() or .prop() to change target on a link not working

Reported by: Owned by:
Priority: low Milestone:
Component: selector Version: 1.6.1
Keywords: Cc:
Blocked by: Blocking:

I'm trying to change the target property of all my external links so that everything that isn't on the same domain has an automatic target="_blank" added to it, but I keep getting a javascript error message. After searching, it seems that "target" isn't included in neither .attr() nor .prop().

This used to work in previous versions of JQuery.

Here's the code I'm using (I tried both with .attr() and .prop() and neither worked) :

$("a[href^='http:'],a[href^='https:']").not("[href*=" + location.hostname + "]").prop('target','_blank').not(":has(img)").not("[id]").after('<img src="/Images/Icones/icone_externe.gif" width="12" height="12" alt="external link" style="margin-left:3px;"/>');

Attachments (0)
Change History (5)

Changed June 28, 2011 03:30PM UTC by rwaldron comment:1

component: unfiledattributes
owner: →
status: newpending

Thanks for taking the time to contribute to the jQuery project! Please provide a reduced jsFiddle test case to help us assess your ticket!

Additionally, be sure to test against the jQuery Edge version to ensure the issue still exists. To get you started, I've created this boilerplate: Open the link and click to "Fork" in the top menu.

Changed June 28, 2011 03:51PM UTC by anonymous comment:2

Here's the code I tried :

I wasn't able to make it work... it doesn't seem to add neither the target nor the icon (but in my tests on my pages, it does add the icon when I remove the prop('target', '_blank') code). Am I doing anything wrong? It really is a simple script and it's been working for a while with all previous versions.

Changed June 28, 2011 04:45PM UTC by timmywil comment:3

component: attributesselector
priority: undecidedlow

I don't think this is an issue with attr or prop:

I think what may be happening is the selection is not collecting your element. The culprit may be that in

.not("[href*=" + location.hostname + "]")

the href should be quoted...

.not("[href*='" + location.hostname + "']")

log out your selection and let us know

Changed June 28, 2011 05:13PM UTC by germLukas comment:4

It worked...! Thanks!! Since which version do we have to put it in quotes? It used to work great untill now...

Thanks for the help!

Changed June 28, 2011 05:45PM UTC by timmywil comment:5

resolution: → invalid
status: pendingclosed

@germLukas It has always been in the documentation that quotes are mandatory( This improves the performance of the selector engine and has been more strictly enforced since 1.5 I believe. Thanks for your quick response.