How to open a URL in the same tab

Hi guys,
I have a URL field in my database (a URL within my Wix site) and I connected it to a Wix Pro gallery (“Links connect to”).
Now, when I click the item in the gallery, it opens a new tab.
Is there any way to make it open in the same tab?
I’ve found some solutions here for buttons with links, but can it done somehow with galleries too?

Thank you!
J.D.

1 Like

Same Problem here with the gallery it always open in a new tab…anyone please…I need to finish my project. I used this code in Repeaters button and it works

$w.onReady( function () {
$w(“#button1”).target = “_self”;

});

I try to used this also in the gallery but it not works…do you have code for the gallery?

Thank you!

have you found a solution?

Wix apps are proprietary, thus the only control you have over them is what they give you. You could create your own alternative and then you would have the ability to target your current window.

@juancarlosvalverdeso , as a workaround you can set the gallery to “when clicking an item nothing happens” on the editor, and then use wix-location to redirect the user to the link destination onItemClicked:

import wixLocation from 'wix-location';
$w.onReady(function () {
    $w("#gallery1").onItemClicked( (event) => {
        wixLocation.to(event.item.link);
      })
}) 

That might work.

You are the real mvp thanks for the help :wink:

@jonatandor35 This look like what im looking for but I cant seem to get it to work. When I click on the gallery item I get a TypeError: _wixlocation2.default.to.to is not a function. Any ideas?

@jka38214cill Maybe you will know the answer to this too?

@justinsa use only one “to”.
(it’s the forum code block that duplicated it).
it should be:
wixLocation.to(event.item.link);

@jonatandor35 Thank-you so much! However it still gives me an error when trying to click the gallery item. :'/

@justinsa Be sure to use " Links connect to " on the editor.

@justinsa Also, you should delete everything below what J.D. wrote for you. It’s more than just bad syntax, it may lead you on a path to the bad habit of having more than one onReady function.

@jonatandor35 I still am unable to get the link to open in the same window. Not sure what I am doing wrong? It seems sometimes it works but then reverts to opening in a new window. Is there something else I can do to get this working? Appreciate the help. (First part of the code is trying to address the same window opening, second part is a randomiser for the gallery)

import wixLocation from ‘wix-location’;

$w.onReady( function () {
$w(“#gallery1”).onItemClicked( (event) => {
wixLocation.to(event.item.link);
})
})

$w.onReady( function () {
$w(“#dataset1”).onReady(() => {
let items = $w(“#gallery1”).items;
items = shuffle(items);
$w(“#gallery1”).items = items;
});
});
export function shuffle(array) {
for ( let i = array.length - 1; i > 0; i–) {
const j = Math.floor(Math.random() * (i + 1));
[array[i], array[j]] = [array[j], array[i]];
}

return array;
}

@justinsa First make sure (on the editor) that the gallery is set to support links.
second, rearrange your code:

import wixLocation from 'wix-location';
$w.onReady(function () {
 $w("#dataset1").onReady(() => {
  let items = $w("#gallery1").items;
  items = shuffle(items);
  $w("#gallery1").items = items;
  $w("#gallery1").onItemClicked( (event) => {
   wixLocation.to(event.item.link);
  })
 });
});
export function shuffle(array) {
 for (let i = array.length - 1; i > 0; i--) {
 const j = Math.floor(Math.random() * (i + 1));
  [array[i], array[j]] = [array[j], array[i]];
 }
return array;
}

@jonatandor35 Thank-you so so very much for your help. After a couple more hours of trying to get it to work I think i found what was the issue. The links were full web addresses, when I changed those to be just “/pagename” it seems to be opening in the same window fine (before they were “http://mysite.wixsite.com/sitename/pagename”

I owe you a coffee at least!

4 Likes

Hi, It doesn’t works for me can you let me know what could be my problem?

No idea.

help please TMT

@justinsa Thank you <3 so much love you !!! /pagename why i never think of that <3

I am having the same issue. I have a database with dynamic pages and a button to link to a page within the website. I’ve tried all the code the form has suggested and it still opens in a new window! Any help is appreciated.
Thanks,
Simone