JavaScript - How to Return Current URL for Share Button? Last Updated : 26 Nov, 2024 Comments Improve Suggest changes Like Article Like Report In this article, we will learn how to create share buttons on websites, allowing users to share a post or the URL of the site on various social media platforms. We will break this down into simple steps for better understanding.Step 1: Create the HTML StructureFirst, we need to create an HTML file to display the share buttons and a simple layout on the browser. html <!DOCTYPE html> <html> <head> <style> /* This CSS is optional */ #share-button { padding: 10px; font-size: 24px; } </style> </head> <body> <button id="share-button">Share</button> <!-- The anchor tag for the sharing link --> <a href="#"></a> <script> // JavaScript code here </script> </body> </html> OutputResult for HTML code.Above is the HTML code and we will be writing only the JavaScript part from this point.Step 2: Display the Current Web Page's URLIn this step, we will add the JavaScript code which will display the current web-page's URL as a link.Here the 'window' is a global object which consists of various properties out of which location is one such property and the href property provides us the complete URL with the path and the protocol used. But we don't need the 'https://'(protocol) so we remove it using the slice method of JavaScript. HTML <!DOCTYPE html> <html> <head> <style> /* This CSS is optional */ #share-button { padding: 10px; font-size: 24px; } </style> </head> <body> <button id="share-button">Share</button> <!-- The anchor tag for the sharing link --> <a href="#"></a> <script> // Storing the URL of the current webpage const URL = window.location.href.slice(7); const link = document.querySelector('a'); link.textContent = URL; link.href = URL; // Displaying in the console console.log(URL); </script> </body> </html> OutputResult for Step: 2Step 3: Remove Protocol from URLWe don't want the URL to be displayed as soon as the browser window loads instead we want it to be displayed when we click the button. So we will add an event listener to the button and then display the URL when the button is clicked HTML <!DOCTYPE html> <html> <head> <style> /* This CSS is optional */ #share-button { padding: 10px; font-size: 24px; } </style> </head> <body> <button id="share-button">Share</button> <a href="#"></a> <script> const URL = window.location.href.slice(7); const link = document.querySelector('a'); const button = document.querySelector('#share-button'); // Adding a mouse click event listener to the button button.addEventListener('click', () => { link.textContent = URL; link.href = URL; // Displaying in the console console.log(URL); }); </script> </body> </html> OutputStep 4: Add Social Media Sharing LinksNow the link is displayed when the button is clicked, but we don't need a link to the current page but a link that shares the current webpage on a social media platform. So we update the code as follows. HTML <!DOCTYPE html> <html> <head> <style> #share-button { padding: 10px; font-size: 24px; } </style> </head> <body> <button id="share-button">Share</button> <a href="#"></a> <script> const fbShare = "https://www...com/sharer/sharer.php?u="; // Storing the URL of the current webpage const URL = window.location.href.slice(7); // Removes 'http://' // Select elements const link = document.querySelector('a'); const button = document.querySelector('#share-button'); // Adding a click event listener to the button button.addEventListener('click', () => { // Display the current webpage link link.textContent = fbShare + URL; link.href = fbShare + URL; // Log in the console console.log(fbShare + URL); }); </script> </body> </html> OutputThis is how we can get a sharing URL, instead of displaying the sharing URL we can actually use it within the button to directly redirect the user to the particular social media platform. Also, I have used the Facebook share URL and similar URLs are available for almost all the social media platforms and feel free to experiment with them and come up with your own share button. This post has covered all the essentials about sharing the current web-page's URL using JavaScript. Comment More infoAdvertise with us Next Article JavaScript - How to Return Current URL for Share Button? H haridarshanc Follow Improve Article Tags : JavaScript Web Technologies HTML CSS CSS-Misc HTML-Misc JavaScript-Misc +3 More Similar Reads How to Get the Current URL using JavaScript? Here are two different methods to get the current URL in JavaScript.1. Using Document.URL PropertyThe DOM URL property in HTML is used to return a string that contains the complete URL of the current document. The string also includes the HTTP protocol such as ( http://).Syntaxdocument.URLReturn Val 1 min read How To Get URL And URL Parts In JavaScript? In web development, working with URLs is a common task. Whether we need to extract parts of a URL or manipulate the URL for navigation, JavaScript provides multiple approaches to access and modify URL parts. we will explore different approaches to retrieve the full URL and its various components.The 3 min read How to get URL Parameters using JavaScript ? To get URL parameters using JavaScript means extracting the query string values from a URL. URL parameters, found after the ? in a URL, pass data like search terms or user information. JavaScript can parse these parameters, allowing you to programmatically access or manipulate their values.For getti 3 min read How to Extract the Host Name from URL using JavaScript? Extracting the hostname from a URL using JavaScript means retrieving the domain part from a complete web address. This can be done using JavaScript's URL object or methods like window.location, which allow easy access to the hostname of a URL.What is URL?A URL (Uniform Resource Locator) is the web a 2 min read How to Get Browser to Navigate URL in JavaScript? As a web developer, you may need to navigate to a specific URL from your JavaScript code. This can be done by accessing the browser's window object and using one of the available methods for changing the current URL.In JavaScript, there are several approaches for navigating to a URL. The most common 4 min read How to get the current absolute URL in Ruby on Rails? In Ruby on Rails, fetching the current absolute URL can be essential for various tasks like redirecting users, generating links, or handling dynamic content. Fortunately, Rails provides straightforward methods to achieve this.Table of Content Using `request.original_url`:Benefits of `request.origina 2 min read How to redirect to a relative URL in JavaScript? To redirect to a relative URL in JavaScript, you could use window.location.href. It is a property in JavaScript that represents the complete URL of the current page. It can be used to get the current URL or to navigate to a new URL by assigning a new URL to it. Approach HTML Structure:The HTML file 2 min read How to get protocol, domain and port from URL using JavaScript ? The protocol, domain, and port of the current page can be found by two methods: Method 1: Using location.protocol, location.hostname, location.port methods: The location interface has various methods that can be used to return the required properties. The location.protocol property is used to return 2 min read How to add Share button in React Native ? React Native is an open-source UI software framework created by Meta Platforms, Inc. It is used to develop applications for Android, Android TV, iOS, macOS, tvOS, Web, Windows, and UWP by enabling developers to use the React framework along with native platform capabilities.In this article, we will 2 min read How to get the file name from page URL using JavaScript ? JavaScript provides multiple techniques for string manipulation and pattern matching. By demonstrating various methods, the article equips developers with versatile solutions to dynamically retrieve and utilize file names from different URL formats within their applications. There are several approa 3 min read Like