为什么这不起作用?
我正在尝试使用 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/#!/ 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...
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您真正需要的是 jQuery Address 插件。 (示例)
What you actually need is the jQuery Address plugin. (samples)