-
I want to manipulate the "href" attribute in a link with a hook. <a href="/about" data-preview>About</a> When the above link is clicked, if that link has the " Description: target_url: ".../about"
current_url: ".../example"
new_visit_url: ".../example?preview=about" I used " |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 7 replies
-
What have you tried inside those hooks? Setting |
Beta Was this translation helpful? Give feedback.
-
Manipulating the URL in the clicked link1- For SEO, we add the permalink directly to the link. <a href="localhost/about">About</a> 2- To differentiate we add a <a href="localhost/about" data-preview>About</a> 3- By taking advantage of Swup's hooks, we actually create the connection we want. swup.hooks.on('link:click', (visit) => {
const element = visit.trigger.el;
if (document.contains(element)) {
if (element.hasAttribute("data-preview") || element.closest("[data-preview]")) {
var target_url = new URL(element.href),
current_url = new URL(window.location.href);
current_url.searchParams.set("preview", target_url.pathname);
visit.to.url = current_url;
}
}
}); In this way, when the link is clicked, instead of visiting the " 4- (Optional) Keeping scroll position, "if you are going to open the target in a modal like me" // if (document.contains(element)) {
// ....
visit.scroll.reset = false;
// } |
Beta Was this translation helpful? Give feedback.
Manipulating the URL in the clicked link
1- For SEO, we add the permalink directly to the link.
2- To differentiate we add a
data-preview
attribute.3- By taking advantage of Swup's hooks, we actually create the connection we want.