0

I can successfully pre-populate an email from a button click with the first part of the page URL, and the first parameter in the URL, but it's cutting off after that. I have multiple parameters in the URL. How do I capture them all and put the full URL in the link in the email?

So for the URL e.g. https://website.com/folder?param1=one&param2=two

Current code:

<div class="button">
  <a
    href="mailto:?subject=Email title&body=Here’s a link: &nbsp; [sub]"
    data-message=""
    onclick="this.href = this.href.replace('[sub]',window.location)"
  >
    Email me a link to this page
  </a>
</div>

This is populating the email link part with https://website.com/folder?param1=one only. &param2=two is missing.

I've also tried with

<div class="button">
  <a
    href="mailto:?subject=Email title&body=Here’s a link: &nbsp;"
    data-message=""
    onclick="this.href += window.location.href;"
  >
    Email me a link to this page
  </a>
</div>

Which has the same result.

I have a feeling it's to do with needing to use URLSearchParams and specify the parameter names, but I don't know how to write this into the existing code. I read MDN.

2
  • 1
    You're putting all of this into what's already a query string, ?subject=...&body=..., have you tried escaping the URL? Commented Jan 6 at 11:25
  • &param2= will be interpreted as a parameter of the mailto: href, same as &body= You need to escape the & from the location.href Commented Jan 6 at 11:38

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.