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);
}
If you want to check if the page exists first, you could do this :
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.
UPDATE:- after the comment
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
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:
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-complicated for this problem.