How to Get a Canonical URL in JavaScript

Cloudmersive
1 min readJul 26, 2021

Retrieving the canonical URL of an HTML document can be helpful if you’re looking to contribute the link to a sitemap; supplying accurate canonical URLs will aid search engines in displaying the best search results for a user. To obtain the rel=canonical URL of a specific HTML document, we have a quick and easy API solution that can be run in JavaScript to simplify the entire operation.

We will start by installing the jQuery library:

bower install jquery

Post-installation, we are ready to call the function with the following code:

var settings = {
"url": "https://api.cloudmersive.com/convert/edit/html/head/get/rel-canonical-url",
"method": "POST",
"timeout": 0,
"headers": {
"inputFileUrl": "<string>",
"Content-Type": "application/x-www-form-urlencoded",
"Apikey": "YOUR-API-KEY-HERE"
},
"data": {
"inputFile": "<binary>"
}
};
$.ajax(settings).done(function (response) {
console.log(response);

And that’s it! If you need to retrieve an API key, you can do so by registering for a free account on the Cloudmersive website; this provides 800 calls/month across our multitude of APIs.

--

--

Cloudmersive

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.