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)
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!
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.
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
.
The ^ symbol indicates that the event will execute any link that begins with /
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With