JavaScript Code to Add Link to Copied Text from Your Website
Add Backlinks to Copied Text with JavaScript and Boost Your Website’s SEO. Do you want to boost your website’s search engine optimization (SEO) and drive more traffic? Adding backlinks to copied text is a great way to do it. Our JavaScript code makes it easy to add backlinks to any text that is copied from your website.
Just copy this Javascript code and paste it to the footer of your website.
<script type=”text/javascript”>
function addLink() {
var body_element = document.getElementsByTagName(‘body’)[0];
var selection;
selection = window.getSelection();
var pagelink = “<br /><br /> Read more at: <a href='”+document.location.href+”‘>”+document.location.href+”</a><br />”; // change this if you want
//The Script Created by https://facttuts.com
var copytext = selection + pagelink;
var newdiv = document.createElement(‘div’);
newdiv.style.position=’absolute’;
newdiv.style.left=’-99999px’;
body_element.appendChild(newdiv);
newdiv.innerHTML = copytext;
selection.selectAllChildren(newdiv);
window.setTimeout(function() {
body_element.removeChild(newdiv);
},0);
}
document.oncopy = addLink;
</script>
Why place the JavaScript code in the footer?
Placing the JavaScript code in the footer of your webpage helps improve the overall performance and loading time of your webpage. This is because the script will load after the entire page content has loaded. As a result, the user will not have to wait for the script to load before they can view the page content.
How it works
Our JavaScript code works by dynamically appending a backlink to the copied text. The backlink will point back to the original source of the text, which will help to improve your website’s SEO.
Objective of this code
The objective of this code is to add a custom link to the text whenever a user copies content from a webpage. This link will lead back to the original webpage, allowing readers to visit the source and providing proper attribution.
Step-by-step explanation
The code works in the following steps:
- The code defines a function called
addLink()
. This function will be triggered whenever the user copies content from the webpage. - The
addLink()
function captures the text that the user has selected (highlighted) on the webpage. - The code creates a new variable called
pagelink
, which contains a string that says “Read more at:” followed by the URL of the current webpage. The URL is fetched usingdocument.location.href
. - The selected text and the
pagelink
are combined to create a new string calledcopytext
. This new string contains the user-selected text along with the custom link back to the webpage. - A new
<div>
element is created usingdocument.createElement('div')
. This<div>
will be used temporarily to hold thecopytext
content. - The newly created
<div>
is styled to be positioned off-screen (hidden from the user’s view). It is given a CSS style ofposition: absolute
andleft: '-99999px'
. This ensures it will not be visible on the webpage. - The
<div>
element is appended to the<body>
element of the webpage usingdocument.body.appendChild(newdiv);
. - The content of the
<div>
(copytext
) is set to the combined text and link created earlier. - The code selects all the text inside the
<div>
usingselection.selectAllChildren(newdiv);
. This step is necessary so that the browser copies the modified content (original selection + page link) to the clipboard when the user performs the copy action. - A 100-millisecond delay is added using
window.setTimeout()
before removing the<div>
from the document. This small delay ensures that the browser has enough time to copy the modified content to the clipboard before the temporary<div>
is removed. - The code sets up an event listener using
document.addEventListener('copy', addLink);
. This means that whenever the user performs a copy action on the webpage, theaddLink()
function will be called, and the custom link will be added to the copied content.
In summary, this JavaScript code allows a website to append a link to the current page whenever a user copies any content. The link acts as a reference back to the original page, providing proper source attribution and encouraging readers to visit the original source.
The power of backlinks for SEO
Backlinks are one of the most important factors in SEO. Search engines like Google use backlinks to determine the authority and relevance of a website. When other websites link to yours, it signals to search engines that your content is valuable and worth sharing. This can lead to higher rankings in search results, which will drive more traffic to your website.
Benefits of adding backlinks to copied text
There are many benefits to adding backlinks to copied text, including:
- Enhanced search visibility: By incorporating backlinks into copied content, your website gains additional exposure and relevance in search engine algorithms. This can lead to higher rankings and increased organic traffic.
- Increased referral traffic: As users click on the backlinks when sharing the copied content, it generates referral traffic back to your website. This direct traffic source is valuable and can result in higher engagement and conversions.
- Building authority: Backlinks not only boost your website’s authority in the eyes of search engines but also establish your website as a trusted source of information, encouraging visitors to return for more.
- Encouraging content sharing: Users who find valuable content on your website may be more likely to share it on various platforms, leading to even more backlinks and increased exposure.
- User experience improvement: Providing backlinks with copied content enhances the user experience as it gives proper credit and acknowledgment to your website. This positively influences the reader’s perception of your brand.
Responsible usage
While our JavaScript code offers significant benefits, it is essential to use this functionality responsibly and ethically. Ensure that you comply with copyright laws and respect the terms of service of websites you link to. Additionally, avoid excessive backlinking, as search engines may penalize websites engaged in manipulative practices.