It used to be possible to have a button with a link and an onClick event (which executes a function), but now it no longer works.
For example, if you have an onClick event and change the link property using code, when you click on the button it only fires the onClick event and doesn’t even add a link to the button.
Please fix this!
You can’t have a button linked through the Wix Editor and have the same button do something in code.
If you want the button to do something before moving to another page, then you need to write it in code so that the action happens first before the user is moved onto another page.
It literally was working like 2 days ago , then all of the sudden it stopped working, I did not touch the Site. I just received a report by a user that told me the button didn’t redirect him to the page.
"you need to write it in code so that the action happens first before the user is moved onto another page. " - how do I do that? I want to user Link to be opened in a new tab.
First off I would check that it doesn’t work on whatever browser you have access to, as it might just be a case of it still does work and that the user could not access it due to them having a third party browser app on their browser which is blocking spyware, adblocker, popus, tracking etc, which in turn could effect your website functionality and make some areas of your website not work.
I have had it myself where the third party apps that I am using on Chrome and Firefox were blocking certain parts of a website and therefore making some elements not appear or work on the site. However, once I had whitelisted the website in those third party apps, then the website worked fine again as they did not check the website again.
As for the action before, it all depends on what you are trying to do as you don’t say what you want the button to do before moving the user onto a new page etc.
Simple thing would be to have the button do something after the onClick event handler function is called and then use set timeout to leave a delay before the user is moved onto the new page through the Wix Location - to function. https://www.wix.com/corvid/reference/wix-location.html#to
Set Timeout links:
https://www.w3schools.com/js/js_timing.asp
https://www.w3schools.com/jsref/met_win_settimeout.asp
export function input1_keyPress(event, $w) {
setTimeout(() => {
//This code will run in 5 seconds
}, 5000);
Previous posts about page delaying.
https://www.wix.com/corvid/forum/community-discussion/how-to-delay-going-to-a-link-after-clicking
https://www.wix.com/corvid/forum/community-discussion/redirect-page-after-a-time-delay
Similar here for delayed lightbox opening.
https://www.wix.com/corvid/forum/community-discussion/set-timeout-for-lightbox
To have a element link to another page and either open in the same tab or a new tab, then look at using _blank or _self as shown here. https://www.wix.com/corvid/reference/$w.LinkableMixin.html
I’m using the same browser and addons as before. “and then use set timeout to leave a delay before the user is moved onto the new page through the Wix Location - to function.” - I can’t open a new tab using the Wix Location to function. “To have a element link to another page and either open in the same tab or a new tab, then look at using _blank or _self” - I literally used that on the button.
Here’s my code.
The downloadBtn_click function gets executed, but the Link added to the button doesn’t work, it doesn’t even show up in the live site HTML code for the button.
@pptgames
Much easier to paste your code here.
When you say it was working before, what were you actually getting as the link should be a page url name with the / in front of it.$w(“#myElement”).link = “/about-me”;
Or something else as listed in the API Reference. https://www.wix.com/corvid/reference/$w.LinkableMixin.html#link
link
Sets or gets the element’s link.
Description
When the element is clicked, the browser navigates to the link location.The different types of links you can use are:
-
/localPageURL: another page on your site
-
/: your site’s home page
-
http(s)://: an external web address
-
wix:document://: a document stored in the Media Manager
-
mailto:?subject=: an email
-
tel:: a phone number
Although as Liran (Wix Mod) added in another forum post reply…
Having both link and onClick is not a good practice, as you define two different behaviours for a button. As a solution, at the end of your onClick function, use wixLocation.to.
You just need to have the onClick event handler function, then your code for what you want to happen and then the wix location link at the end of your code.
import wixLocation from "wix-location";
$w.onReady(function () {
export function downloadBtn_click(event) {
//your code here for something to happen...//
wixLocation.to("/your-page-url-here");
}
Another option that you can try is to add your target to the onReady page function itself, similar to what you have done already with both the target and the link.
On your page add the following line to the onReady function at the top of the page.
$w("#downloadBtn").target = "_blank";
So in the end your code should look like this.
$w.onReady(function () {
$w("#downloadBtn").target = "_blank";
});
//rest of code//
And that’s it. You can then still use the Connect panel to make your button link to the URL.
All this code does is tell the button that it should open any links in a new tab.
@givemeawhisky
“Much easier to paste your code here.” - I tried to, but when I clicked Publish an error occurred and it said I should contact the Site owner.
The Link is correct, I’m 100% sure.
"at the end of your onClick function, use wixLocation.to. " - But I want the Link to be openned in a new tab, Wix Location doesn’t allow that.
“Another option that you can try is to add your target to the onReady page function itself …” - I can’t connect the button link to the URL using the Connect panel because the URL is encrypted and the code in the onReady function decrypts the URL and sets the button’s link to the decrypted URL.
This is still a problem for me. Buttons connected to dynamic data say in a repeater don’t have an option to select _target outside of clicking one of the links panels. I think you moved that setting into the other pages and forgot about this.
When I change the _target in code on my dynamic button, it only served to reopen the same page in a new tab.