Dynamic link creation opening new window not working

I have a page in the members area which takes the logged in user’s email, uses it to look up a link in the CMS. This link I then want to open in a new tab. Here is my code (I also assign a couple of text fields with the CMS data just to prove connection works). The issue lies in $w(‘#button3’).link = entry.link - I was hoping this would open the link:

import wixUsers from ‘wix-users’;
import wixData from ‘wix-data’;

$w.onReady(function () {

async function button3_click(event) {
    let user = wixUsers.currentUser;
    let userEmail;

    if (user.loggedIn) {
        try {
            // Get the user's email using async/await
            userEmail = await user.getEmail();
            console.log('user email', userEmail);
            $w('#text51').text = userEmail;

            // Query your CMS collection based on the user's email
            wixData.query('ExceldocumentLinks')
                .eq('email', userEmail)
                .find()
                .then(results => {
                    // Check if any entries were found
                    if (results.items.length > 0) {
                        // Access the first entry
                        const entry = results.items[0];

                        // Now you can use the entry data as needed
                        console.log('CMS Entry:', entry);
                        $w('#text52').text = entry.link; 
                        $w('#button3').link = entry.link;
	
                    } else {
                        // Handle the case where no entry was found for the constant email value
                        console.error('No CMS entry found for the constant email value');
                    }
                })
                .catch(error => {
                    // Handle errors during the query
                    console.error('Error querying CMS:', error);
                });
        } catch (error) {
            console.error('Error getting user email:', error);
        }
    } else {
        // Handle the case where the user is not logged in
        console.error('User not logged in');
    }
}

// Assign the click event handler to the button
$w('#button3').onClick(button3_click);

});

I tried wixlocation but I believe this isn’t approprate in this case. I also tried custom element but couldn’t find any specific guidance or resources to this type of request (I am relativey new to Velo so learning as I go).

Any help or links would be much appreciated.

$w(‘#button’).link is a great way to set a button to open a external link on click. You can also set .target to control the behavior of where the link opens.

Does the #text52 element display the intended external url?

If so, does the string use the format below?

  • http(s)://<url>: an external web address

Note: The http(s):// is required to be recognized as a external url

Hi Thomas, thanks for your reply. Yes, #text52 element does display the intended external url and the string used in is in that format. The window doesn’t even attamept to open if that helps too?

Thank you

@thomasj It has now started working. The link I was using as a test had a redirect attachted to it so I changed it to a link that did not and it worked. Thank you for your help, I was looking in the wrong place!

1 Like