egu*_*iar 3 html javascript wordpress redirect web-applications
我的链接看起来像这样:
http://localhost:8081/wordpress/?p=535
Run Code Online (Sandbox Code Playgroud)
如何使用JavaScript重定向到此页面?我不熟悉?语法,但我觉得我应该能够发送一些带有帖子ID的请求?
就像是:
redirect("p="+postid);
Run Code Online (Sandbox Code Playgroud)
只是打电话window.Location对我不起作用,因为如果帖子不存在,那么我希望能够做其他事情.另外,我不希望它直接发送给本地主机.它需要指向网站的根目录......
更像这样:
if (request("p="+postid) exists) {
redirect("p="+postid);
}
Run Code Online (Sandbox Code Playgroud)
document.location.href = "http://localhost:8081/wordpress/?p=535";
Run Code Online (Sandbox Code Playgroud)
如果要先检查页面是否存在,可以执行以下操作:
var addr = "/wordpress/?p=535"; // here with a relative url
$.ajax({
type: 'HEAD',
url: addr,
success: function() {
document.location.href = addr;
}
});
Run Code Online (Sandbox Code Playgroud)
这将产生一个HEAD请求(因此没有内容下载)来检查页面是否存在,只有当它正常时才更改当前位置.
请注意,如果需要,可以在没有jQuery的情况下执行此操作,如果需要,我可以让您确定确切的语法.
但这只能在两个页面来自同一个域时才有效.
| 归档时间: |
|
| 查看次数: |
1031 次 |
| 最近记录: |