How do I noindex ajax-loaded pages without a head tag?

by dejuan.watsica , in category: SEO , 2 years ago

How do I noindex ajax-loaded pages without a head tag?

Facebook Twitter LinkedIn Telegram Whatsapp Pocket

3 answers

by jose_gulgowski , 2 years ago

@dejuan.watsica 

To prevent search engines from indexing AJAX-loaded pages without a head tag, you can add a noindex directive in the X-Robots-Tag HTTP header. This header is sent with the AJAX response, and it allows you to control the indexing of the page.


Here's an example in JavaScript using jQuery:

1
2
3
4
5
6
7
8
9
$.ajax({
  url: "your-ajax-loaded-page.html",
  beforeSend: function(xhr){
    xhr.setRequestHeader("X-Robots-Tag", "noindex");
  },
  success: function(data) {
    // Your success handling code here
  }
});


Note that this will only work if the server is configured to pass on the X-Robots-Tag header in its responses. If the server is not configured to pass this header, you will need to modify its configuration or contact the server administrator for assistance.

Member

by delpha , a year ago

@dejuan.watsica 

If you don't have access to the HTTP headers or server configuration, you can use the robots meta tag to prevent indexing on the AJAX-loaded pages. You can add this tag dynamically using JavaScript.


Here's an example:


1 2 3 4


var meta = document.createElement('meta'); meta.name = 'robots'; meta.content = 'noindex'; document.getElementsByTagName('head')[0].appendChild(meta);


This JavaScript code creates a meta element and appends it to the head tag of the document. The meta element has a name attribute set to "robots" and a content attribute set to "noindex", which instructs search engines not to index the page.


You should add this code in the success callback of your AJAX request or wherever appropriate to ensure it is executed after the content is loaded.


Please note that although this method can discourage indexing by most search engines, it is not foolproof. Determined search engine crawlers may still index the page, but most search engines will honor the noindex directive if properly implemented.

by laverna_hirthe , a year ago

@dejuan.watsica 

Another option to prevent search engines from indexing AJAX-loaded pages without a head tag is to use the JavaScript history API to change the URL of the page. By changing the URL, you can ensure that search engines do not associate the AJAX-loaded content with a specific URL, thus preventing indexing.


Here's an example using the history API:


1 2 3 4 5 6 7 8 9 10


$.ajax({ url: "your-ajax-loaded-page.html", success: function(data) { // Your success handling code here

1
2
// Change the URL without reloading the page
history.replaceState({}, "", "");


} });


By using history.replaceState() and providing an empty URL, you replace the current URL with a blank one. This ensures that search engines do not assign any indexing value to the page.


Keep in mind that this method relies on the history API, which may not be supported by older browsers. Additionally, it only prevents indexing by search engines and does not guarantee that the page won't be crawled.