Automatically redirecting to

Question:
Hi everyone,
I’m working on a website using Wix Studio, and I need a bit of help with a redirect. I want the homepage to stay loaded for 3 seconds before automatically redirecting to the “Contact” page. I’ve already tried a code that works in theory, but it doesn’t seem to function as expected.

Could anyone guide me on the correct code or any adjustments I need to make to achieve this effect?
Thanks in advance!

import wixLocation from 'wix-location';

$w.onReady(function () {
   setTimeout(function() {
      wixLocation.to("/contact"); // Redirects to the Contact page after 3 seconds
   }, 3000); // 3000 milliseconds = 3 seconds
});

Product:
[Wix Stduio]

I’m not seeing anything immediately wrong with this.

Is “/contact” the actual slug of the contact page?

Also worth noting that wixLocation became wixLocationFrontend around a year or so ago.

So would look like this:

import wixLocationFrontend from "wix-location-frontend";

$w.onReady(function () {
   setTimeout(function() {
      wixLocationFrontend.to("/contact"); // Redirects to the Contact page after 3 seconds
   }, 3000); // 3000 milliseconds = 3 seconds
});

Or, you can use the SDK, as we now encourage :slight_smile:

import { location } from "@wix/site-location";

$w.onReady(function () {
   setTimeout(function() {
      location.to("/contact"); // Redirects to the Contact page after 3 seconds
   }, 3000); // 3000 milliseconds = 3 seconds
});

When you say “not working” what does that mean in practice? Anything being logged in the browser?

On top of my head, I can think of one potential reason. The new wix pages have slug like /blank, /blank-1, and so on. May sure your page slug is correct. You can find it in SEO settings.

Also use this debug code to see if the code is working correctly, use this code and preview inside the editor(not a test site)

import wixLocation from 'wix-location';

$w.onReady(function () {
    console.log("Page loaded, starting 3-second timer...");
    setTimeout(() => {
        console.log("Redirecting to Contact page now!");
        wixLocation.to("/contact");
    }, 3000);
});

You should see following message in Dev console

Running the code for the Home page. To debug this code in your browser’s dev tools, open c1dmp.js.

Page loaded, starting 3-second timer…

Home

Line 4

Widget updated with props:

{…}

Redirecting to Contact page now!