开发者

Make the code conditional jquery

OK I'm working on the new version of my website: http://themes.visualise.ca/visualise/

When you select a project by clicking on an image it dynamically开发者_如何学C loads the requested wordpress project page within the existing page and changes the url to include the hash so I get: http://themes.visualise.ca/visualise/#project

And if I give this direct url to people Wordpress automatically load the project page as if the user had clicked on the image within the page.

Everything is as I want it except for one thing my code make the project page ("#board") loads all the time even if there is no #project hash in the url so I would like to make the code conditional.

$(window).load(function(e){
    $.ajaxSetup({cache:false});
    var post_slug = window.location.hash.substring(1);
    $("#board").load("http://themes.visualise.ca/visualise/ajax/",{slug:post_slug});
    $("#board").delay(1500).slideDown("slow");
});

http://url , http://url/ , http://url/# , http://url/#/ should not load the project board only http://url/#project or http://url/#whatever should

But I don't know how since I still new to jQuery and programming in general.


It should be like this:

$(window).load(function(e){
    $.ajaxSetup({cache:false});
    var post_slug = window.location.hash.substring(1);
    if(/^[a-zA-z0-9\-_]+$/.test(post_slug)){ //This conditions loading
       $("#board").load("http://themes.visualise.ca/visualise/ajax/",{slug:post_slug});
       $("#board").delay(1500).slideDown("slow");
    }
});

We are asking if there's a string hash.

EDIT: I've updated the condition to use a regular expression to suite the needs you recently commented

Hope this helps

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜