Bug Tracker

Opened 8 years ago

Closed 8 years ago

#9684 closed bug (invalid)

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

Reported by: lukas@… Owned by: lukas@…
Priority: low Milestone: 1.next
Component: selector Version: 1.6.1
Keywords: Cc:
Blocked by: Blocking:

Description

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;"/>');

Change History (5)

comment:1 Changed 8 years ago by Rick Waldron

Component: unfiledattributes
Owner: set to lukas@…
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: http://jsfiddle.net/rwaldron/da3nM/ Open the link and click to "Fork" in the top menu.

comment:2 Changed 8 years ago by anonymous

Here's the code I tried :

http://jsfiddle.net/germLukas/nPXcT/

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.

comment:3 Changed 8 years ago by timmywil

Component: attributesselector
Priority: undecidedlow

I don't think this is an issue with attr or prop: http://jsfiddle.net/timmywil/nPXcT/3/

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

comment:4 Changed 8 years ago by germLukas

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!

comment:5 Changed 8 years ago by timmywil

Resolution: invalid
Status: pendingclosed

@germLukas It has always been in the documentation that quotes are mandatory(http://api.jquery.com/attribute-equals-selector/). This improves the performance of the selector engine and has been more strictly enforced since 1.5 I believe. Thanks for your quick response.

Note: See TracTickets for help on using tickets.