1

I'm currently building a dynamic website based on jQuery en hashChanged. Currently I use this code on document.ready:

$(window).hashchange( function(){
    switch(location.hash)
    {
    case "#/calendar":
        content.load("calendar.php");
        break;
    case "#/media":
        content.load("media.php");
        break;
    case "#/social":
        content.load("social.php");
        break;
    case "#/settings":
        content.load("settings.php");
        break;
    default:
        content.load("home.php");
        title.text("Debafla");
    }
});

Now I'm building a video galery, so i wanted to use URLs like these:

example.com/#/video/12485

So video.php need to be loaded in the content div en i need to get the video with ID 12485 from the DB. But how can I cover these hash pages in javascript?

Thank you!

4

2 回答 2

2

使用以下模式添加额外的 if-else 条件:

$(window).hashchange( function(){
    if (/^#\/video\/\d+$/.test(location.hash)) { //hash equals #/video/numbers ?
        var num = location.hash.match(/\d+/)[0]; //12485, for example
         //do something, for example:
        content.load("video.php?video_id=" + num);

    } else {//Else switch:
        switch(location.hash)
       {
     ...
于 2011-11-01T20:56:58.660 回答
2

我会使用拆分方法:

$(window).hashchange( function(){
    switch(location.hash.split("/")[1])
    {
    case "calendar":
        content.load("calendar.php");
        break;
    case "media":
        content.load("media.php");
        break;
    case "social":
        content.load("social.php");
        break;
    case "settings":
        content.load("settings.php");
        break;
    case "video":
        content.load("video.php?v=" + location.hash.split("/")[2]);
        break;
    default:
        content.load("home.php");
        title.text("Debafla");
    }
});
于 2011-11-01T21:00:44.397 回答