Reputation: 1
I'm trying to add a class only when the page loads with a specific URL (example.com/#test) but can't seem to get it.
$(document).ready(function() {
if(window.location.href === "https://example.com/#test"){
$('.test').addClass('display');
} else {
$('.test').removeClass('display');
}
});
Upvotes: 0
Views: 101
Reputation: 371
The window.location
object has a hash
member, which contains all charatcers from the #
mark on.
Try: window.location.hash.indexOf("test") > -1
as your condition.
Ex:
$(document).ready(function() {
if(window.location.hash.indexOf("test") > -1){
$('.test').addClass('display');
} else {
$('.test').removeClass('display');
}
});
Upvotes: 1