How to add a WhatsApp share button in a website using JavaScript?WhatsApp is one of the most popular mobile messaging apps nowadays. It was developed by Jan Koum and Brian Acton, but now it is owned by Facebook. Recently, there are almost 1.5 million active users on it. As WhatsApp is a too popular chatting app, most of the websites need a WhatsApp sharing option on them. The web developer requires to fulfil this requirement and add it's sharing option to their website for the efficiency of the users. This chapter will guide you on adding a WhatsApp sharing link or button to a website using the JavaScript programming language. This chapter will describe the following methods to create a WhatsApp sharing option for your website:
Why needed?As WhatsApp is the most popular chatting app, most websites need a WhatsApp sharing option on them. WhatsApp provides private sharing, which means that sharing is engaged between a few people rather than the public. The web developer requires to fulfil this requirement and add it's sharing option to their website for the efficiency of the users. So that the user can easily share any information directly from the website using that sharing option instead of copy and paste sharing. Steps to Add a WhatsApp sharing linkFollowing steps will create a WhatsApp sharing link on a webpage through which you can directly open WhatsApp and share the website data using it. Note: This will not work on large screens like laptops and computers. It will work perfectly on a Mobile phone.For the large-screen users, provide the WhatsApp web address or link inside the JavaScript code. Follow the steps below: Step 1: Design a simple webpage having a hyperlink on it using HTML anchor tag . We will use this link as a sharing option. HTML CODE Step 2: As we already told you that this method would not work on large screens (laptop/desktop/computer). So, we will add CSS to hide the sharing link on large screens. CSS code to hide the sharing link on large screens: Use the CSS @media query for this. CSS CODE This code will hide the link which you have created using the anchor tag as we set the min-width of the screen to 500px. For the testing purpose, you can increase the min-width to 1000 and check the output on a laptop; it will not hide the link. Step 3: Now, implement the above two steps with JavaScript to make the created hyperlink in working. So, when you click on this link, it will open WhatsApp directly from the current webpage. JAVASCRIPT CODE Add the below code to the anchor tab inside HTML code for mobile. Complete ExampleSee the complete code to create a WhatsApp sharing link below: Output on Mobile Check and run the above code on mobile instead of desktop or laptop. You will get the response as given below. Here, click on the Share to WhatsApp link, which will redirect you to the WhatsApp messenger on your mobile phone. ![]() When you click on the link, your WhatsApp application will open in mobile phone and it will ask you to select the contacts to share the text with them. This will share the text provided in the text parameter (This is WhatsApp sharing example) inside the anchor tag. ![]() Note: You will get this output of the above code only on the mobile phone.Output on desktop If you try to run the above code on large screens like desktop or laptop, you will get the response without having a WhatsApp sharing link, as shown in the below screenshot: ![]() Or, in case you get the WhatsApp sharing link on minimizing the browser screen size, you will get the blank response containing text in the search bar of the browser by clicking on that link. ![]() Create a WhatsApp Sharing buttonNow, we will create and add a WhatsApp Sharing button to a webpage. Basically, this code is designed to add a button to the website that will lead you to the WhatsApp mobile app. By clicking on this button, you will be redirected to the WhatsApp application to share some web content with your contacts. This method does not require a lengthy code. It just needs the window.open() method of JavaScript and link of WhatsApp and data you want to share. For mobile WhatsApp For WhatsApp Web on desktop Add one of the following codes to your final code with respect of which you want to open WhatsApp. ExampleSee the code for mobile to add a WhatsApp sharing button on a webpage/website: Copy Code Output on Mobile Run the above code on mobile, it will show you a small HTML button on a webpage, as shown in the below screenshot: ![]() Click on this Open WhatsApp button that will take you to your WhatsApp application to select and share data with your contact. See the screenshot of mobile: ![]() Create a WhatsApp Sharing icon using imageIn this method, we will create a WhatsApp icon by taking an image from the internet and add it to the webpage to create a sharing icon of WhatsApp. When you click on this image/icon, it will redirect you to the WhatsApp application for sharing some web content with your contacts. For mobile WhatsApp For WhatsApp Web Important points for this example:
E.g. 'whatsapp://send?text= https://www.youtube.com/watch?v=ohpCMpderow' ExampleThe below code will help you to add a WhatsApp sharing icon on your website. Remember that - below code will work only for the WhatsApp mobile application. On the web, it will show you nothing. Copy Code Output on Mobile Run the above code on mobile; it will show you a small icon of WhatsApp. Click on this WhatsApp image icon and see the output of how it will respond: ![]() Click on this WhatsApp image icon you will get the output as given below: ![]() WhatsApp sharing link with user inputThis is another example of creating a WhatsApp sharing option along with an input field to take input from the user. This means that we will take the input from the user in an input field that he/she wants to share and send this message using JavaScript by providing a sharing button to share with their contacts on WhatsApp messenger. In this example, we will take an input field to take input from the user and a button to submit that user input and a WhatsApp sharing link. This will be done using JavaScript programming. Look at the code for WhatsApp sharing with user input: ExampleSee the complete code to add a WhatsApp sharing icon on a webpage: Copy Code Output When you execute the above code on mobile, it will show you a text field to take input from the user and a button to share that input to WhatsApp. See the mobile screenshot for the above program: ![]() Provide the message in the text field and click on the Share to WhatsApp button. It will redirect you to your WhatsApp to share the message with your WhatsApp contact. ![]() Next Topic#
|
Python tutorial provides basic and advanced concepts of Python.
Vue.js is an open-source progressive JavaScript framework
HTML refers to Hypertext Markup Language. HTML is the gateway ...
Java is an object-oriented, class-based computer-programming language.
PHP is an open-source,interpreted scripting language.
Spring is a lightweight framework.Spring framework makes ...
JavaScript is an scripting language which is lightweight and cross-platform.
CSS refers to Cascading Style Sheets...
jQuery is a small and lightweight JavaScript library. jQuery ...
SQL is used to perform operations on the records stored in the database.
C programming is considered as the base for other programming languages.
JavaScript is an scripting language which is lightweight and cross-platform.
Vue.js is an open-source progressive JavaScript framework
ReactJS is a declarative, efficient, and flexible JavaScript library.
jQuery is a small and lightweight JavaScript library. jQuery ...
Node.js is a cross-platform environment and library for running JavaScript app...
TypeScript is a strongly typed superset of JavaScript which compiles to plain JavaScript.
Angular JS is an open source JavaScript framework by Google to build web app...
JSON is lightweight data-interchange format.
AJAX is an acronym for Asynchronous JavaScript and XML.
ES6 or ECMAScript 6 is a scripting language specification ...
Angular 7 is completely based on components.
jQuery UI is a set of user interface interactions built on jQuery...
Python tutorial provides basic and advanced concepts of Python.
Java is an object-oriented, class-based computer-programming language.
Node.js is a cross-platform environment and library for running JavaScript app...
PHP is an open-source,interpreted scripting language.
Go is a programming language which is developed by Google...
C programming is considered as the base for other programming languages.
C++ is an object-oriented programming language. It is an extension to C programming.
C# is a programming language of .Net Framework.
Ruby is an open-source and fully object-oriented programming language.
JSP technology is used to create web application just like Servlet technology.
The JSTL represents a set of tags to simplify the JSP development.
ASP.NET is a web framework designed and developed by Microsoft.
Perl is a cross-platform environment and library for running JavaScript...
Scala is an object-oriented and functional programming language.
VBA stands for Visual Basic for Applications.
Spring is a lightweight framework.Spring framework makes ...
Spring Boot is a Spring module that provides the RAD feature...
Django is a Web Application Framework which is used to develop web applications.
Servlet technology is robust and scalable because of java language.
The Struts 2 framework is used to develop MVC based web applications.
Hibernate is an open source, lightweight, ORM tool.
Solr is a scalable, ready-to-deploy enterprise search engine.
SQL is used to perform operations on the records stored in the database.
MySQL is a relational database management system based...
Oracle is a relational database management system.
SQL Server is software developed by Microsoft.
PostgreSQL is an ORDBMS.
DB2 is a database server developed by IBM.
Redis is a No SQL database which works on the concept of key-value pair.
SQLite is embedded relational database management system.
MongoDB is a No SQL database. It is an document-oriented database...
Memcached is a free, distributed memory object caching system.
Hibernate is an open source, lightweight, ORM tool.
PL/SQL is a block structured language that can have multiple blocks in it.
DBMS Tutorial is software that is used to manage the database.
Spark is a unified analytics engine for large-scale data processing...
IntelliJ IDEA is an IDE for Java Developers which is developed by...
Git is a modern and widely used distributed version control system in the world.
GitHub is an immense platform for code hosting.
SVN is an open-source centralized version control system.
Maven is a powerful project management tool that is based on POM.
Jsoup is a java html parser.
UML is a general-purpose, graphical modeling language.
RESTful Web Services are REST Architecture based Web Services.
Postman is one testing tools which is used for API testing.
JMeter is to analyze the performance of web application.
Jenkins builds and tests our software projects.
SEO stands for Search Engine Optimization.
MATLAB is a software package for mathematical computation, visualization...
Unity is an engine for creating games on multiple platforms.
Hadoop is an open source framework.
Pig is a high-level data flow platform for executing Map Reduce programs of Hadoop.
Spark is a unified analytics engine for large-scale data processing...
Spring Cloud is a framework for building robust cloud applications.
Spring Boot is a Spring module that provides the RAD feature...
AI is one of the fascinating and universal fields of Computer.
Cloud computing is a virtualization-based technology.
AWS stands for Amazon Web Services which uses distributed IT...
Microsoft Azure is a cloud computing platform...
IoT stands for Internet of Things...
Spring Cloud is a framework for building robust cloud applications.
Email:jjw.quan@gmail.com