I have been mulling about SEO, ajax and links. I get confused when looking at code from different web-pages and how they seem to handle this issue.
I have always made sure that a static context exists for the function that makes the ajax-call. I have not been placing javascript inline of my markup but I have rather been using ids to invoke the functions with external js-files. A typical example of my own is the following:
<a href="/resource?take=10" id="next-ten">Link</a>
And then hookup the id with a click function.
But what I see on some major pages is that they use things like:
<a href="#" onclick="ajaxCall();">Link</a>
<a href="javascript:void(0)" onclick="ajaxCall()">Link</a>
Is there some benefits of using javascript inline like above? I don't get it, major sites seems to be using it?
the second way is bad because a crawler that does not use javascript would not be able to use the second method.
the first method would still work if it didn't use javascript.
As long as your links are properly named and contextually appropriate, AND behave correctly without javascript enabled, you should be 100% fine.
Not that some crawlers do use javascript though, so even though the second variation is a poor one, it might still work sometimes.
tl;dr: If it works without javascript you're good.
On HTML part write this way:
<a href="/resource?take=10" onclick="return ajaxCall()">Link</a>
On JavaScript part, write this way:
function ajaxCall() {
// AJAX functionalities will go here
return false;
}
Search engines will index the url, as JavaScript code will not be executed during crawlers fetch the page. But when an user browses this page using browsers, the JavaScript code will be executed (assuming the user did not disable JavaScript), and the ajaxCall
function will be called.
Note: As the function returns
false
, user will not navigate to the URL defined inhref
section. But if it returnstrue
orvoid
, then user will be navigated to the defined location.