最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Sveltekit goto a new tab - Stack Overflow

programmeradmin1浏览0评论

I'm using sveltekit goto function to navigate to a page using on:click on a div.

clickHandler: (id: string) => goto(`/app/profile/${id}/settings/general-info`)

How can I specify to open page in a new tab just like using CTRL + Click?

I'm using sveltekit goto function to navigate to a page using on:click on a div.

clickHandler: (id: string) => goto(`/app/profile/${id}/settings/general-info`)

How can I specify to open page in a new tab just like using CTRL + Click?

Share Improve this question asked Oct 25, 2022 at 8:57 Vincent KaraniVincent Karani 711 silver badge3 bronze badges 3
  • 1 "on:click on a div" is bad. For accessibility, click events should only be attached on interactive elements that are supposed to be clicked, e.g. button. – brunnerh Commented Oct 25, 2022 at 9:49
  • 1 Does this answer your question? Open a URL in a new tab (and not a new window) – brunnerh Commented Oct 25, 2022 at 9:52
  • 2 If an element is supposed to be a link, it should use an a tag, that way the user simply can use Ctrl-Click or the right click menu to open the link elsewhere. If you want the link to open in a new tab by default, just specify target="_blank". – brunnerh Commented Oct 25, 2022 at 10:01
Add a ment  | 

2 Answers 2

Reset to default 3

If you want to manage this from a ts/js file and the code is running client side then you can use window.open instead of goto -

window.open(`/app/profile/${id}/settings/general-info`, "_blank");

you can use a regular <a> element to create a link and specify target="_blank" to open it in a new tab.

here is an example of the the code.

<a href="https://svelte.dev" target="_blank">Svelte</a>

a live web environment showing that how it works.

发布评论

评论列表(0)

  1. 暂无评论