I have social buttons for sharing the current page on all my web application pages, the URL attribute is as follows data-url="https://www.myPage.com/whateverComes". However, I am looking for a way to have this URL dynamically from the location.href. Is there a way to achieve this without having to use javascript? maybe just plain HTML or inline javascript.
I am using pug as templating engine, -window.location.href or #{window.location.href} didn't work. Any ideas please ? thanks.
window.location.href is a client side JavarScript variable, pug has no access to it.
Assuming you're using Node.js with express you can do something like this
app.get("/posts/:post", (req, res) => {
res.render("page", {
currentURL: `${req.protocol}://${req.get("host")}${req.originalUrl}`
});
});
then in your pug template interpolate the variable:
a(data-url=currentURL)= Link