Im wanting to do an ajax page change that is search engine friendly.
Basically in theory, if a user clicks the link to another page, only the relevant content is changed. (ie. next page of product list) rather than the whole page.
However if search engines or people without javascript wil still be able to navigate the same old way.
So, I’ve got:
<a href="page2.html" onclick="MyAjaxFunction();">
In theory the MyAjaxFunction returns false to prevent the href. HOWEVER this doesn’t work. the broswer still follows the href as I presume the return false is being called too late (once the ajax page loads).
I tried putting the return false; at the end of the “onclick” in the href line, but still the same result.
Make sure you add the return keyword in the onclick; otherwise, the function is just being executed the returned boolean value isn’t being stored/sent anywhere.
I can’t as I dont have it in front of me…but it is just a very simpole one that sends data to a page and gets content back (html product list) to update the product container.
Make sure that it’s the invoked function through the onClick that is returning the boolean value and not the callback function for the Ajax request.
Wrong way:
function onclickAjaxFunc()
{
Ajax.request('yoursite.com', {
onSuccess : function(response)
{
// ... code to process response
return false; // DON'T DO THIS
}
});
}
Right way:
function onclickAjaxFunc()
{
Ajax.request('yoursite.com', {
onSuccess : function(response)
{
// ... code to process response
}
});
return false;
}
When you get a chance, post your Ajax function because I need visibility into that to see what the problem might be. Also–and knowing the hell of cross-browser issues–what browsers have you tested this in?
I’m thinking this is a race condition problem, such that you’re replacing the DOM via a Ajax call yet still relying on the calling code to return false for an onclick on the very DOM you’ll be replacing. This setup is just asking for trouble.
I’m going to sandbox an alternative and post back in a few minutes with my results.
Okay, get ride of that http.send(null); line. Try again.
Edit: That http.send(null) is causing an error, thus the “return false;” line is never reached. The Ajax call, however, is successfully being sent so the DOM is changing but then getPage() never returns false (because of the error) so the link then redirects you.