Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery: if url contains #work then do something

Tags:

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.

like image 536
Eddie Avatar asked Apr 22 '11 16:04

Eddie


2 Answers

$(function() {
    if ( document.location.href.indexOf('#Work') > -1 ) {
        $('#elementID').animate({"left": "250"}, "slow");
    }
});
like image 172
Ketan Modi Avatar answered Sep 28 '22 08:09

Ketan Modi


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
} 
like image 45
Paul Avatar answered Sep 28 '22 06:09

Paul