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
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.
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.
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.
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?
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, 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.
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).
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.
@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:
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.