为什么这不起作用?

发布于 2024-10-13 12:34:22 字数 2990 浏览 1 评论 0原文

我正在尝试使用 AJAX 实现类似 Facebook 的页面导航。我写了以下代码。

if ("onhashchange" in window) {
    window.onhashchange = function () {
        locationChanged(window.location.href);
    }
}
else {
    var storedURL = window.location.href;
    window.setInterval(function () {
        if (window.location.href != storedURL) {
            storedURL = window.location.href;
            locationChanged(storedURL);
        }
    }, 250);
}

function locationChanged(e) {
    if (window.location.href.include('#!')) {
        var paths = window.location.href.split("#!");
        if (paths.length >= 2) {
            var pos = paths.length - 1;

            if (paths[pos].startsWith("/"))
                paths[pos] = paths[pos].substr(1);

            $('#holder').load(paths[pos]);
        }
    }
    else {
        if (window.location.href.endsWith('Index.html')
        || !window.location.href.endsWith('.html')) {
            //this is first page
            redirect("Login.html");
        }
    }
}

$(document).ready(function() {
    if (window.location.href.endsWith('Index.html')
    || !window.location.href.endsWith('.html')) {
        //this is first page
        redirect("Login.html");
    }

    captureLinks();
});

function captureLinks() {
    $('a').click(function(e) {
        e.preventDefault();
        redirect($(this).attr("href"));
    });
}

function redirect(page) {
    var path = page;

    if (window.location.href.include('#!')) {
        var paths = window.location.href.split("#!");

        var pos = paths.length - 2;

        if (!paths[pos].endsWith("/"))
            paths[pos] += "/";

        if (!page.startsWith("/"))
            page = "/" + page;

        path = paths[pos] + "#!" + page;
    }
    else {
        if (path.endsWith(".html"))
            path = window.location.href.trimEndTill("/");
        else
            path = window.location.href;

        if (!path.endsWith("/"))
            path += "/";

        if (!page.startsWith("/"))
            page = "/" + page;

        path += "#!" + page;
    }

    window.location.href = path;    
}

优点是代码可以工作,但有一个唯一的问题。有一个 Index.html 页面,它是应用程序的主入口页面,当我写下...

http://localhost: 8081/

它将其转换为...

http://localhost:8081/#!/ Login.html

这是完美的。但是当我指出它说...

http://localhost:8081/Index.html

它是使其...

http://localhost:8081/Index.html/#!/ Login.html

这会产生 404 错误,因为没有名为“Index.html/”的页面。我修改了代码,以便它可以检测 Index.html 并在将其指向 Login.html 之前先将其删除。尽管代码现在给出了正确的结果,即使 Index.html 为...

http://localhost:8081 /#!/Login.html

但问题是,它永远不会使用 $.load 函数在正文中加载该页面 (Login.html)。有什么问题吗?我还想知道我的代码是否足够高效?

I am trying to achieve Facebook like page navigation using AJAX. I have written following code.

if ("onhashchange" in window) {
    window.onhashchange = function () {
        locationChanged(window.location.href);
    }
}
else {
    var storedURL = window.location.href;
    window.setInterval(function () {
        if (window.location.href != storedURL) {
            storedURL = window.location.href;
            locationChanged(storedURL);
        }
    }, 250);
}

function locationChanged(e) {
    if (window.location.href.include('#!')) {
        var paths = window.location.href.split("#!");
        if (paths.length >= 2) {
            var pos = paths.length - 1;

            if (paths[pos].startsWith("/"))
                paths[pos] = paths[pos].substr(1);

            $('#holder').load(paths[pos]);
        }
    }
    else {
        if (window.location.href.endsWith('Index.html')
        || !window.location.href.endsWith('.html')) {
            //this is first page
            redirect("Login.html");
        }
    }
}

$(document).ready(function() {
    if (window.location.href.endsWith('Index.html')
    || !window.location.href.endsWith('.html')) {
        //this is first page
        redirect("Login.html");
    }

    captureLinks();
});

function captureLinks() {
    $('a').click(function(e) {
        e.preventDefault();
        redirect($(this).attr("href"));
    });
}

function redirect(page) {
    var path = page;

    if (window.location.href.include('#!')) {
        var paths = window.location.href.split("#!");

        var pos = paths.length - 2;

        if (!paths[pos].endsWith("/"))
            paths[pos] += "/";

        if (!page.startsWith("/"))
            page = "/" + page;

        path = paths[pos] + "#!" + page;
    }
    else {
        if (path.endsWith(".html"))
            path = window.location.href.trimEndTill("/");
        else
            path = window.location.href;

        if (!path.endsWith("/"))
            path += "/";

        if (!page.startsWith("/"))
            page = "/" + page;

        path += "#!" + page;
    }

    window.location.href = path;    
}

The good point is that the code is working but it has an only issue. There is an Index.html page which is the main entry page of the app and when I write say...

http://localhost:8081/

It converts it to...

http://localhost:8081/#!/Login.html

Which is perfect. But when I point it to say...

http://localhost:8081/Index.html

It was making it...

http://localhost:8081/Index.html/#!/Login.html

That was creating 404 error as there is no page named "Index.html/". I modified the code so it could detect Index.html and remove it first before pointing it to Login.html. Although the code gives the correct result now even with Index.html as...

http://localhost:8081/#!/Login.html

But the problem is, it never load that page (Login.html) in the body using $.load function. Is there anything wrong? I would also like to know if my code is efficient enough?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

说好的呢 2024-10-20 12:34:22

您真正需要的是 jQuery Address 插件。 (示例

What you actually need is the jQuery Address plugin. (samples)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文