Opening external url on button click programmatically

Hi,

I’m trying to open external url using wix-location to function.
I followed the documentation, but it does nothing, no action triggers. Local url’s works though.

Here’s the code I’m using:

import wixLocation from ‘wix-location’;

export function registrationClick(event, $w) {
wixLocation.to(“https://wix.com”);
}

EDIT: Ok, so I’ve managed to open url in New tab, but I need to do this in current window. Any thoughts?

You might be able to assign a link to the button and set a target . Understand that because how a page opens (same window, new tab, new window, etc) is often dependent on the browser settings.

Hi @Klaudas,

Did you get it work ?

Did you get to open external page from wix programmatically using javascript ?

Thanks

Thanks for helping me dig into this for my site.
I made the “on click” work using this :
It also detects whether it is on a mobile screen or not to give different links.

import wixLocation from 'wix-location';
import wixWindow from 'wix-window';

export function vectorImage4_click_1(event) {
    //Add your code for this event here:
    if(wixWindow.formFactor === "Mobile"){
        wixLocation.to("https://api.whatsapp.com/send?phone=+316123456");
    } else {
        wixLocation.to("https://web.whatsapp.com/send?phone=+316123456&text=");
        //wixWindow.open('www.nu.nl', '_blank');
    }
}

But I ran in the problem that I could not get it to open on a new tab, it only opens in current tab. So I changed it all this this, and now it works fine :

import wixLocation from 'wix-location';
import wixWindow from 'wix-window';

export function vectorImage4_viewportEnter(event) {
    //Add your code for this event here:
    if(wixWindow.formFactor === "Mobile"){
        $w("#vectorImage4").link = "https://api.whatsapp.com/send?phone=+31652189062";
    } else {
        $w("#vectorImage4").link = "https://web.whatsapp.com/send?phone=+31652189062&text=", '_blank';
        $w("#vectorImage4").target = "_blank";
    }
}

It now opens a whats-app web tab while on desktop, but opens the whats-app on mobile phones when on mobile device

This is an old post from 2018 and is being closed.
Please add a new forum post and refer to this old post with a link if needed.

This has also been mentioned in previous forum posts as well, plus the change of use for calling WhatsApp too from ‘https://api.whatsapp.com’ to ’ https://wa.me

https://www.wix.com/corvid/forum/community-discussion/whatsapp-banner
Also, if you simply look through WhatsApp own docs then you will find the solution for yourself.
https://faq.whatsapp.com/en/general/26000030

Using Click to Chat
WhatsApp's Click to Chat feature allows you to begin a chat with someone without having their phone number saved in your phone's address book. As long as you know this person’s phone number, you can create a link that will allow you to start a chat with them. By clicking the link, a chat with the person automatically opens. Click to Chat works on both your phone and WhatsApp Web.

To create your own link, use https://wa.me/<number> where the <number> is a full phone number in international format. Omit any zeroes, brackets or dashes when adding the phone number in international format. For a detailed explanation on international numbers, read this article. Please keep in mind that this phone number must have an active account on WhatsApp.

Use: https://wa.me/15551234567

Don't use: https://wa.me/+001-(555)1234567

To create your own link with a pre-filled message that will automatically appear in the text field of a chat, use https://wa.me/whatsappphonenumber/?text=urlencodedtext where whatsappphonenumber is a full phone number in international format and URL-encodedtext is the URL-encoded pre-filled message.

Example: https://wa.me/15551234567?text=I'm%20interested%20in%20your%20car%20for%20sale

To create a link with just a pre-filled message, use https://wa.me/?text=urlencodedtext

Example: https://wa.me/?text=I'm%20inquiring%20about%20the%20apartment%20listing

Also, Vorbly had a simple example here.
https://www.vorbly.com/Vorbly-Code/WIX-CODE-WHATSAPP-CLICK-TO-CHAT