I’m trying to use Wix Code to create a link on our website where I can control the “onclick” function to call a script that’s already installed in our website just before the closing of the body tag. Any ideas or some direction on how I can accomplish this?
Hi,
Welcome to Wix Code
In order to control onclick event check out this tutorial .
To navigate you can use wixLocation method.
Feel free to paste your code here to consult.
Good luck!
Roi.
I have the button selected on the page and the code console open with the following code- see below. I’m just not sure… how does Wix know that I mean “#button9”? Where is that being connected to the code below?
// For full API documentation, including code examples, visit Velo API Reference - Wix.com
$w.onReady(function () {
//TODO: write your page related code here…
});
import wixLocation from ‘wix-location’;
wixLocation.to(“FareHarbor”);
And why, when I go to “preview” the page do I get the following errors:
There was an error in your script
Error: wix-location.to cannot be used before the page is ready.
Hi,
In order to make it work you should add an onClick event handler and paste the wixLocation.to function inside of it.
Check out the tutorial & video below
At the end the code should look similar to this:
import wixLocation from 'wix-location';
export function button1_click(event, $w) {
wixLocation.to('wixLocation.to("https://fareharbor.com/embeds/book/captainhookhiltonhead/items/43258?ref=SCHM');
}
I’m assuming I should also remove the link that I placed on the button using the standard tools?
I get an error when I try to use the code above. It is highlighting the (event, $w) part?
It says these parameters are never used. Not sure what that means.
Hi,
Yes, you should remove the normal link.
And you can either disregard the error you mentioned or simply remove the event and $w parameters, leaving the parenthesis
Hi there, I did all this and when I pushed it live and click on the button, nothing is happening.
Hi!
Can you please share a link to the page you’re having issues with?
One of the Wix Team will inspect it and provide you with a solution!
Doron.
Hi Doron- Thanks again for your help! I cannot leave the page live because the link does not work, but I copied the page for you and hid the copied page from the navigation: https://www.sheltercovehiltonhead.com/copy-of-party-fishing
It’s the green button on the page that says “book now”
Hi again!
I checked out the site and the buttons you’ve mentioned and I couldn’t find a problem.You’ve linked the buttons in the repeater to several Lightboxes from your site and it seems to work.Since you’re not using a database to draw data from to your repeater, leaving it connected by links is probably your best choice at the moment.
Anyway, your code wouldn’t have worked although it has no mistakes because you didn’t create an event in the button’s properties, the function you use should match the existing one in it.
Hope it helps.
Best of luck!
Doron.
Hi Doron, I realized I never got a response in my email inbox about this ongoing problem and decided to login to Wix and see where things were left off. Looks like you did respond and my spam filter caught it. I had to remove the event on the live page because it was not working.
I added it back and removed the link on the button:
And this does not work. When I click on the website button on the live site after I publish the above, nothing happens. I’m using the latest version of FF.
Hi,
Fix this line:
wixLocation.to('wixLocation.to("https://fareharbor.com/embeds/book/captainhookhiltonhead/items/43258?ref=SCHM');
to
wixLocation.to("https://fareharbor.com/embeds/book/captainhookhiltonhead/items/43258?ref=SCHM");
Roi.
Great! Well, the link works now, but
1: The script I’m using that I put on every page just before the close of the body tag: is not making the link open in a lightframe (additonal info: Advanced Lightframe API Usage | FareHarbor Help Center)
2: the link is opening in the same window of the website. Is there any easy fix for this?
Any additional help you can provide? Thanks so much for getting me this far!