admin管理员组文章数量:1303335
My link looks like this:
http://localhost:8081/wordpress/?p=535
How can I redirect to this page with JavaScript? I'm unfamiliar with the ?
syntax, but I have a feeling I should be able to send some sort of request with the ID of the post?
Something like:
redirect("p="+postid);
Just calling window.Location
won't work for me because if the post doesn't exist, then I want to be able to do something else. Also, I don't want it to direct to local host. It needs to direct to the root of the website...
More like this then:
if (request("p="+postid) exists) {
redirect("p="+postid);
}
My link looks like this:
http://localhost:8081/wordpress/?p=535
How can I redirect to this page with JavaScript? I'm unfamiliar with the ?
syntax, but I have a feeling I should be able to send some sort of request with the ID of the post?
Something like:
redirect("p="+postid);
Just calling window.Location
won't work for me because if the post doesn't exist, then I want to be able to do something else. Also, I don't want it to direct to local host. It needs to direct to the root of the website...
More like this then:
if (request("p="+postid) exists) {
redirect("p="+postid);
}
Share
Improve this question
edited Dec 16, 2012 at 12:47
Peter Mortensen
31.6k22 gold badges110 silver badges133 bronze badges
asked Aug 31, 2012 at 14:50
egucciaregucciar
2,1096 gold badges24 silver badges25 bronze badges
0
5 Answers
Reset to default 9document.location.href = "http://localhost:8081/wordpress/?p=535";
If you want to check if the page exists first, you could do this :
var addr = "/wordpress/?p=535"; // here with a relative url
$.ajax({
type: 'HEAD',
url: addr,
success: function() {
document.location.href = addr;
}
});
This will make a HEAD request (so no content download) to check if the page exists and only change the current location if it's OK.
Note that you may do that without jQuery if you want, I let you determine the exact syntax if needed.
But this can only work if both page are from the same domain.
window.location.href = "http://localhost:8081/wordpress/?p=535";
window.location = "http://localhost:8081/wordpress/?p=535";
UPDATE:- after the ment
althought this aint the trick that should be used but can be
try sending an ajax request to the url and if it successfull returns some content redirect else if there is an 404
error just do something else.
To send the user to another URL, use
window.location.href = 'http://localhost:8081/wordpress/?p=535'
The stuff after "?" is part of the URL and is meant to be processed by a server-sided program.
You can add a variable post ID into the location like this:
var postID = 535;
window.location = "http://localhost:8081/wordpress/?p="+postID;
Now that you've updated your question I think it's obvious that this isn't a job for JavaScript. If you want to check if a page exists you should be doing that check server-side since only the server-side code has access to your database where it can check if specific post IDs actually exist or not.
You could of course code up some AJAX interface where the JavaScript goes off and makes a query to the server-side code to see if the post exists, and then you'll get your answer back and act on it accordingly—but that seems really over-plicated for this problem.
本文标签: htmlTest if a WordPress page exists using JavaScript before redirecting to itStack Overflow
版权声明:本文标题:html - Test if a WordPress page exists using JavaScript before redirecting to it - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741701408a2393309.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论