0

I created a product-based service website. I need my customer to get quotes from the website to my WhatsApp.

Example:

<button> 
 <a href=" https://api.whatsapp.com/send? phone=whatsappnumber&text=The current URL of the website... I need a quote for this product" >
 Send a quote on WhatsApp
</button>

How to get the current URL of the website? Please assist me.

asked Jan 13, 2023 at 5:09
1

3 Answers 3

3

The window.location object can be used to get the current page address (URL) and to redirect the browser to a new page.

  1. window.location.href returns the href (URL) of the current page

console.log(window.location.href)

  1. window.location.hostname returns the domain name of the web host

console.log(window.location.hostname)

  1. window.location.pathname returns the path and filename of the current page

console.log(window.location.pathname)

  1. window.location.protocol returns the web protocol used (http: or https:)

console.log(window.location.protocol)

  1. window.location.assign() loads a new document

function newDoc() {
 window.location.assign("https://stackoverflow.com")
}
<input type="button" value="Learn More" onclick="newDoc()">

answered Jan 13, 2023 at 5:15
0

To get the current URL of the website, you can use the window.location.href property in JavaScript. You can then add this value to the text parameter in the WhatsApp link as follows:

<button>
 <a href="https://api.whatsapp.com/send?phone=whatsappnumber&text='I need a quote for this product' + window.location.href">
 Send a quote on WhatsApp
 </a>
</button>

This will send a message to your WhatsApp number with the text "I need a quote for this product" followed by the current URL of the website.

answered Jan 13, 2023 at 5:17
0

This would work without an anchor tag.

<button onclick="window.open('https://api.whatsapp.com/send?phone=whatsappnumber&text=' + window.location.href, '_blank');">Send a quote on whatsapp</button>
answered Jan 13, 2023 at 5:18

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.