Jul 26, 2021

1 min read

How to Set a Canonical URL in JavaScript

The canonical URL of a web page is the key element that informs search engines which is the primary version that should be presented to end users in search results. Sounds pretty important, right? In this quick tutorial, we will demonstrate how you can use an API in JavaScript to automatically set the rel canonical URL of an HTML document.

First, we will run this command to install jQuery:

bower install jquery

Next, we will simply input the file or file URL, canonical URL to set, and API key into the following example code:

var settings = {
"url": "",
"method": "POST",
"timeout": 0,
"headers": {
"inputFileUrl": "<string>",
"canonicalUrl": "<string>",
"Content-Type": "application/x-www-form-urlencoded",
"data": {
"inputFile": "<binary>"
$.ajax(settings).done(function (response) {

And just like that, your link will be set and ready to tell the search engines what’s up. If you need to obtain an API key, you can do so by registering for a free account on the Cloudmersive website; this provides 800 monthly calls across our library of APIs.