Bug Tracker

Modify

Ticket #9684 (closed bug: invalid)

Opened 2 years ago

Last modified 2 years ago

.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:
Blocking: Blocked by:

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

comment:1 Changed 2 years ago by rwaldron

  • Owner set to lukas@…
  • Status changed from new to pending
  • Component changed from unfiled to attributes

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 2 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 2 years ago by timmywil

  • Priority changed from undecided to low
  • Component changed from attributes to selector

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 2 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 2 years ago by timmywil

  • Status changed from pending to closed
  • Resolution set to invalid

@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.

Please follow the  bug reporting guidlines and use  jsFiddle when providing test cases and demonstrations instead of pasting the code in the ticket.

View

Add a comment

Modify Ticket

Action
as closed
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.