Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does 'a[href^="/"]' mean?

In the code below, is a[href^="/"] a regular expression?

Why does it have the ^ symbol and what does it do?

Which click events will it/won't it respond to?

$(document).on('click', 'a[href^="/"]', function(e) { 
     e.preventDefault(); 
     var href = $(e.currentTarget).attr('href'); 
     console.log('click ' + href); 
     Backbone.history.navigate(href, { trigger: true }); 
}); 

(code source)

like image 679
Leahcim Avatar asked Oct 02 '15 02:10

Leahcim


People also ask

What does a href =# mean?

Definition and Usage The href attribute specifies the URL of the page the link goes to. If the href attribute is not present, the <a> tag will not be a hyperlink. Tip: You can use href="#top" or href="#" to link to the top of the current page!

What does a href mean in CSS?

href stands for hypertext reference. It's the source of the file used by the tag. You can use both not only when connecting an external css file, also for using <a> tags,for a regular hyperlink.


2 Answers

Q. In the code below, is a[href^="/"] a regular expression?

R. No, it's not a regular expression. It's a (CSS based) jQuery selector.

Q. Why does it have the ^ symbol and what does it do?

It has this symbol because it means startsWith.

Q. Which click events will it/won't it respond to?

R. It will respond to any a tag that have an href attribute that starts with /.


Note: that Backbone code is probably using that selector to get all the internal links of the website and change their behavior to do client navigation, while the external links will start with http/https.

like image 111
Buzinas Avatar answered Oct 18 '22 09:10

Buzinas


The ^ symbol indicates that the event will execute any link that begins with /

like image 35
Eborio Linárez Avatar answered Oct 18 '22 09:10

Eborio Linárez