Reputation: 2466
So basically i want run the code only if url looks like this: www.example.com/#page
otherwise do not run the jquery like example: www.example.com/whatever/#page, www.example.com/whatever/whatever#page
etc..,
my code:
$(window).load(function(){
if(window.location.hash == '#page'){alert('success');}
});
currently it alerts every url that contains #page. Any suggestion thanks!
Upvotes: 2
Views: 923
Reputation: 97672
How about
$(window).load(function(){
var test = location.protocol + '//' + location.host + '/#page';
if(test == location){alert('success');}
});
Upvotes: 0
Reputation: 544
check window.location.pathname
if (window.location.pathname == "/sample/sample" && window.location.hash == "#page")
or whatever permutation of that works for you
if you're looking to do it based on the depth of the path, lets say, alert only on pages that are at least two deep on the pathname, then you need to split the pathname
window.location.pathname.split("/").length //this is the depth of the url
So to combine the two ideas and fit your example,
var depth = window.location.pathname.split("/").length;
// pathname starts with a /, which adds one to the length of the array, so subtract
depth = depth -1;
// now run our checks
if (depth == 2 && window.location.hash == "#page") alert("success!");
else alert ("failure!");
Upvotes: 0
Reputation: 2377
try it
$(window).load(function(){
if(document.location.toString().indexOf('/#page')!=-1){alert('success');}
});
Upvotes: 1