jquery: if url contains #work then do something
50,541
Solution 1
$(function() {
if ( document.location.href.indexOf('#Work') > -1 ) {
$('#elementID').animate({"left": "250"}, "slow");
}
});
Solution 2
window.location.href is pulling the URL into a variable, so you can't search for #Work using that method. Try:
var url = window.location.href;
if (url.search("#Work") >= 0) {
//found it, now do something
}
Author by
Eddie
Updated on August 13, 2020Comments
-
Eddie over 3 years
I tried to write a script which allow me to load certain events when I enter specific url.
My code looks like this:
$(function(){ var url = window.location.pathname; $("url:contains('#Work')").animate({"left": "250"}, "slow"); });
But it doesnt work. Any suggestions? Any help is appreciated.
-
Wiseguy about 13 yearsNote for passers-by: searching a string will return
0
if found at the beginning and-1
if not found. It works in this case because the fragment will never be at the beginning of the URL, but generally you should search strings with> -1
or>= 0
. -
Wiseguy about 13 yearsNote for passers-by: searching a string will return
0
if found at the beginning and-1
if not found. It works in this case because the fragment will never be at the beginning of the URL, but generally you should search strings with> -1
or>= 0
.