Switch On Off with URL

Hello, I want to know how I can turn switch input on or off if the URL contains “testingpage”. Here is my code:

import wixLocation from 'wix-location';

$w.onReady(function () {
 if (wixLocation.url.includes("testingpage"))
    {
    $w('#switch1').checked === true }
 
    }
)


Can someone tell me what is incorrect? Thanks in advance!

1 Like

What are you trying to do with the above code? I don’t see you setting the switch - you’re just checking if it’s checked or not. Plus, the URL is much different when running in Preview than it is in Live.

Hi dear Yisrael, This switch input is showing on all pages. I need my switch input to be true if the URL contains “testingpage”, and to be false if the URL doesn’t contain “testingpage”. This is all I need. Thanks in advance!

if (wixLocation.url.includes("testingpage")) {
    $w('#switch1').checked = true }
}
else {
    $w('#switch1').checked = false }
}

You also need to use an assignment = and not a compare ===
To learn more about Javascript, see An Introduction to JavaScript.

Good luck,

Yisrael

Thank You Dear Yisrael, it works prefect now! Here is the code`

import wixLocation from 'wix-location';
$w.onReady(function () {
if (wixLocation.url.includes("testingpage")) {
    $w('#switch1').checked = true }

else {
    $w('#switch1').checked = false }})
1 Like

Hi All / @yisrael-wix ,

I’m trying to use " User input " ’ Switch ’ to toggle between two websites… a very simple implementation.
There is going to be 2 different language websites (identical).
When user switch it on to ‘right’ it will redirect to English website and when it is turned off “left” it should go to the language website.

I’m not a coder but have done some basic implementations:
Can someone tell me what exactly I need to do… with code that I can use.

Thanks in advance,
Pawan

Hi,
First of all you can use the tutorial for a multilingual site here . If you want to use switch element simply use wixLocation.to in the switch’s onChange event.

Good luck :slight_smile:

Multilingual won’t help us here.
I tried below code but nothing works

I’m very new to code and learning ABC of coding, would be great help if you can put the code here… please :pray:t3:

import wixLocation from 'wix-location';
$w.onReady(function () {
if (wixLocation.to('https://www.mezonic.com')) {
    $w('#switch1').checked = true }

else {
    $w('#switch1').checked = false }});

Thanks,
Pawan

Hi, Your code should look like this:

export function switch1_change(event, $w) {
	if($w('#switch1').checked === true){
		wixLocation.to("/blog");
	}
	else{
		wixLocation.to("/home");
	}
}

Good luck.