I have a website where the user can download PDFs. The owner of the site wants to keep track of how many people have download a pdf via the site.
I know WixCode (or coding at all) very little, but I understand how to add prewritten WixCode to a site, and label buttons and site elements, etc.
In order to do this, I received a method and code from a WixCode expert and it is working well. I have code on the site that creates a timestamp for each button click and adds it to a data base, and then displaying the PDF in the browser. I am keeping the Wix button that is linked to the PDF, and then I am created a new button that hides the original button, and this new button as code to create the timestamp.
The problem is: With a standard Wix button with no code linked to a PDF, the PDF opens in a new browser window, making the ACTIVE browser window the page displaying the PDF.
With the code I am using to create a time stamp in my database, the PDF does open in a new browser window, but the browser then makes the ACTIVE browser window the website, not the newly launched browser tab that displays the PDF.
Does anyone know how I can change the code I have, so when a PDF is downloaded, the active browser window is the window displaying the PDF?
Note I tested this in Chrome, Safari and Firefox, all on a mac.
I have a live site that explains what I am doing and comparing how the two button methods work:
https://dacreativedesign.wixsite.com/test-download-pdf
Here is the code on the page I am using for the button to open the PDF and add the timestamp to a database that is named “Analytics”:
import wixData from ‘wix-data’;
import wixLocation from ‘wix-location’;
let Button1;
$w.onReady( function () {
Button1 = $w(“#Button1”).link;
$w(“#Button2”).target = “_blank”;
});
export function Button2_click (event) {
let toInsert = {
“title”: “PDF Downloaded”
};
wixData.insert(“Analytics”, toInsert)
.then( (results) => {
wixLocation.to(${Button1}
);
} )
. catch ( (err) => {
let errorMsg = err;
} );
}