Repeater: Open link in same window...

Hi everyone,
I just started using Wix Code and have an issue with the repeater.
The repeater shows my latest blog posts as you can see in the picture below.
When I hit the “read more” button, the corresponding blog page opens in a new window.
However I want the link to open on the same page


Can someone help me? I guess I need to write a code, but I´m not sure which function I have to use…

Thanks,
Alex

1 Like

Hi Alex,

Welcome to WixCode. It’s nice to see you’re getting right into it. Repeaters - nice.

You can get the link to open in the same page as long as the URL points to a page on your site. You will need some code - but it is simple and easy:

import wixLocation from 'wix-location'; 

export function button1_click(event, $w) {
	let link = $w1("#button1").link;
	console.log(link);
	wixLocation.to(link);
}

The above example assumes that the page change occurs by clicking on a button in the repeater, but you can base it on whatever click event you want. The idea here is to let wixLocation.to() redirect the page. When the page is on your site, it will open in the same tab/window (depending on your browser settings). If it is an external URL, then it will open in a new tab/window. Make sure you import the wix-location API to your code.

Note: The $w scope selector accesses the click events button instead of a global element.

Another note: The code includes the line console.log(link); since console.log is my friend and yours too. It helps “see” what’s going on and provides valuable feedback of your code execution.

I hope this helps. Good luck,

Yisrael

4 Likes

Hi Yisrael,

thanks for the quick response!
I get the idea behind your code, nevertheless the (internal) page still opens on another tab.

Let me clarify again:
I created a database with my blog titles, subtitles, images, etc and I added the column “URL” that contains the complete URL to the corresponding blog post. The “read more” button is connected to this URL.

Is there anything I may need to change in my database or in the button / repeater settings?

I also noticed that the parameter ‘event’ in your code is never used, but I guess that’s no problem.

Best
Alex

1 Like

Hmm, if it thinks the blog page is “outside” of your site, then that might be the reason that it’s opening in a new tab.

Please post a request in the Feature Request forum for allowing tab/window options when redirecting to a page. I believe that this already on the “to do list”, but another post will maybe up the priority.

1 Like

I finally found a solution!

$w.onReady(function () {
$w(“#element1”).target = “_self”;
$w(“#element2”).target = “_self”;
$w(“#element3”).target = “_self”;
});

Nevertheless I will post the request, a simple “open on current tab” option would be easier.

6 Likes

Hey Alex,

Good catch! That’s the correct solution. I just missed it - happens sometimes.

Yisrael

1 Like

Hey guys! I’m so excited to have found this post! I have repeaters on multiple pages throughout my site. Could you tell me if I need to add this code on every page, or if this is something I should add using the ‘Site’ tab. Also, is the code I need a combination of both Alex’s and Ysrael’s suggestions or just Alex’s?

2 Likes

Hey Juanita,

The code is page oriented since it’s for the specific repeater page. Alex had a great catch on this. For more information, take a look at $w.Button.target .

Yisrael

1 Like

Wow!!! It’s like Christmas morning for me!!! Thanks SO much for this! It works! It works! It works!

1 Like

Yisrael, not sure why Wix is defaulting for repeater to open in same tab. I have a repeater with search set up, so opening in same tab makes for tedious work for user…how do I make it open in new tab? This way after reading the info in new tab, they close out and are right back to the original search results in repeater. That seems like the more logical default for a repeater anyway.

Hey Jeff,

Opening in the same tab is determined by the browser. This means that it’s very difficult (or impossible) to create a full consistent user experience since different browsers behave differently, and users tend to configure according to personal preferences (where it’s possible to set).

Bummer!

Hi there,

$w.onReady(function () {

$w(" #element1 ").target = “_self”;

   $w(" [#element2](https://www.wix.com/code/home/forum/search/posts%3Fquery=%23element2) ").target = "_self"; 
   $w(" [#element3](https://www.wix.com/code/home/forum/search/posts%3Fquery=%23element3) ").target = "_self";		 

});
This is a very good example. But what about the gallery?

In my example, I use the gallery to go to the next dynamic page. API Reference doesn’t provide an indication of the parameter “target”. Link https://wixmanns.wixsite.com/pdesign/project/academy

I could use this tutorial to create previous and next buttons, but in this case I need a manual reference to the next project. In my database I use special fields, everything works, but the link opens in a new window, and I need to open it in the current one.


It’s real?

None of these codes work for me, keeps on opening in a new tab, is there a simple follow through method with a code

@juanita-taylor was the code you used successfully a combo or just Alex’s? I tried variations. The closest I got was combining the two but the result is my site is opening the link in the same window AND opening a a new tab. Any help would be greatly appreciated. Thank you!

@melissa-1 Hello, Melissa! The code below is used for a button inside a repeater to direct clients to a dynamic category page containing their time reports. It does not require an onclick event which I see you’ve included in your code so you may be attempting to do something more than what I’m doing here. In any event, I hope this helps:

$w.onReady(function () {
  $w("#button13").target = "_self";
  $w("#button14").target = "_self";
  });

@juanita-taylor Thank you SO much! Yes, that fixed it!

Hi!

If you are still looking for a solution to the gallery → open new link problem, here is how i did it:

from properties select the onItemClicked for the gallery you have

and inside put this. replace your dataset name with the dataset name in that page.

   $w("#dataset1").onReady(()=>{
        $w("#dataset1").getItems(event.itemIndex, 1)
        .then((result)=> {
            let data = result.items[0].links;
            wixLocation.to(data)
            console.log(data);
        })
    })

this is what my dataset looks like:

hope this helps. let me know if you need more clarification

check my reply right above your comment above

Hi guys, I’ve experienced some issues after the transition to #WixTurbo. Just noticed, that links on my website keep opening in a separate window despite the code outlined above… is there anyone with a similar problem? Any suggestions how to solve the issue?

Yes Alex. You are right. I have same issue as you. I tried every way and every code i could find. There is nothing we can do to open the link in same windows so far. It’s a huge bug. Hope it’s gonna solved soon.

1 Like