Need to jump to a specific anchor somewhere in your website from an external URL/other site? The code below may help:
import wixLocation from 'wix-location';
$w.onReady(function () {
setTimeout(function () { // Uncomment/comment this line and line 24 if you want to delay page redirection
let query = wixLocation.query['anchor']; // look for "?anchor=" in the URL string
let anchorto = '#' + decodeURIComponent(query); // Add # character to front of whatever appears after ?anchor= to create our ElementID
let url = $w(anchorto).link; // set the variable url to the link associated with the ElementID stored in anchorto
if (url === undefined) { // If we are unable to find a valid anchor or URL
url = '/error404'; // set the url to our default 404 page
}
wixLocation.to(url); // and off we go to our new redirected URL
}, 3000); // Uncomment/comment this line and line 8 if you want to delay page redirection 1,000 is 1 second
});
That above is the Page Code from a page on our site called "redirect-to". The page name is not important. Example URL:
eastnetworks co uk / redirect-to?anchor = WhatWeDo
N.B. Unable to post links here, so if you want to see the above in action, remove all spaces and replace the ones before and between "co uk" with periods/full stops"." .
Alternatively, find our blog pages. Most of the pages contain a link "Home Test Service" that uses this approach to land in the correct part of our website.
The "redirect-to" page has some buttons on it - I have 20. Could have more, or less. One of these buttons has a link to the anchor "WhatWeDo". To aid external readability, I changed the ElementID for this button to # WhatWeDo. You don't need to, but I think it looks better than using # button77.
Basically the code uses the buttons ElementID (# WhatWeDo) to push the link into the wixLocation.to(url) instruction.
Cheers
Peter
202103160824GMT - Edited for clarity. 202103160948GMT - Fixed typo.
Does this still work in Wix? I have tried to implement it on my site, but it can't seem to find any of links the Element IDs that I try to reference in the URL.