Open URL in new window

Hi there,

i added a Button to my site which links to a function. That function builds an URL depending on the data a user entered and then opens this url preferrably in a new window/tab.
I used the wixlocation.To() subroutine to call the url but it always gets opened in the same window. I want a new window. How can this be done?

Hi,

Welcome to the Wix Code forums.

You can change the functionality of links using the functions described in $w.LinkableMixin . Keep in mind that the behavior of new tabs/windows is often a browser setting.

Good luck,

Yisrael

Great - thanks a lot Yisrael!

Hello Kalihari

Can you please share the code you used to solve the issue mentioned above?

Thanking in anticipation.

Anupam Dubey

Yisrael:

How would set a target to “_blank” for a table when using wixLocation.to()?

Is this possible, I get an error.

Hey Brett,

You need to provide more information. What code are you using? How are you setting _blank?

@yisrael-wix

On a database populated table, I open up a lightbox if the condition in my dataset is set to “popup”. If the condition is “redirect”, I want to open a URL in a new tab or browser. Right now, it opens in the same browser. It doesn’t look like I can use target on a table.

export function table1_rowSelect() {
setTimeout(() => {
let itemData = $w(“#dataset1”).getCurrentItem();
if (itemData.type.action===“Popup”){
wixWindow.openLightbox(“Media”, itemData);
}
else if (itemData.type.action===“Redirect”){
$w(“#table1”).target = “_blank”; ← I get an error in the IDE
wixLocation.to(itemData.location);
}
}, 2000);
}

@brett82 No, you can’t set target for a table. However, you can use link/target on some components such as Button and Image . For more information, see the LinkableMixin API .

@yisrael-wix

That’s what I thought. Would this make sense as a feature request?

Also, on a separate issue, last week, I think I stumbled upon an issue with hooks. https://www.wix.com/code/home/forum/community-discussion/sporadic-issues-with-data-hooks

Thanks for your quick response.

@brett82 Feature requests don’t make sense. :upside_down_face: Feel free to make the request. Let the smart people at Wix figure it out.

@yisrael-wix Thanks! Feature request has been submitted. BTW: Is there anyone that could help me on the data hooks issue? I think it might be a bug.

Hello, I have a similar question about how to use code to have a button open a PDF in a NEW browser window, but making the browser tab with the PDF the CURRENT browser window. I know very little about WixCode (or code in general) so any advice is greatly appreciated. My question is posted here:

what about slides in gallery?

export function gallery1_itemClicked(event) {
let i=event.itemIndex
$w(“#dataset2”).getItems(i, 1)
.then( (result) => {
let items = result.items;
event.item.target=“_blank”

    wixLocation.to("https://affichecy.com/events/"+items[0].title); 

} )
}
does not work

let i=event.itemIndex
$w(“#dataset2”).getItems(i, 1) .then( (result) => { l
et items = result.items;
event.item.target=“_blank”
event.item.link=“/events/”+items[0].title
} )
does not work also

https://www.wix.com/corvid/forum/main/comment/5ccf63bce10055006e0a29d6

sorry, this link is for opening in the same tab.
In galleries the default is to open in a separate tab.

@jonatandor35 I need to open in new tab. But I cannot do this - only in same.

Hello coders! I have question related to this subject: I am using the Onclick event to trigger facebook tracking and then wixlocation.to to go an external link. I would like the link to open in a new window and I can’t get it to work. When I use the button’s set “link and target” inside onReady it works but then, when I add the Onclick it stops working. Is there a way to have the “link and target” inside the Onclick? something like this?

export function buttonYoutube_click(event) {

ClickToService();
ClickToYoutube();
$w(“#buttonYoutube”).link = “Fernan Dust - De Vuelta En Casa (Canción de Venezuela) - YouTube”;
$w(“#buttonYoutube”).target = “_blank”;

}

My goal is simply to track the clicks and the open a link in a new window… does anybody has an idea on how to do this?
Thanks a lot!

I have the same issue. You can have a button open a link in a new window by resetting the target to _blank but as soon as you add an OnClick event it no longer works. It also doesn’t work if you attach the button to a dataset on the page. The only way I have been able to make the target _blank work is on a button with no code attached to it.

You can’t do it in the OnClick event because wixLocation.to() doesn’t accept the target attribute.

I am trying to have the button open an external site in a new tab but need code on the button to write to my data collection

Any help would be appreciated.

I have the same question
Has anybody responded to itmaster’s question. We need some guidance!