Re: Can't link to anchors from emails

SanfordWhiteman
Level 10 - Community Moderator

Re: Can't link to anchors from emails


Yes, I am sure.

Alas. You’re right, bug still exists.

 

You can put the JS in the <head> (note I wouldn’t use this exact code anymore, but it still works):

<script>
document.addEventListener("DOMContentLoaded", function(e) {
  let scrollToSelector = getURLParameter("scrollTo");
  if(scrollToSelector) {
    let scrollToElement = document.querySelector(scrollToSelector);
    scrollToElement && scrollToElement.scrollIntoView();
  }

  /**
   * @see [IOWA Util]
   */
  function getURLParameter(param) {
    if (!window.location.search) return;

    var m = new RegExp(param + '=([^&]*)').exec(window.location.search.substring(1));
    if (!m) return;    

    return decodeURIComponent(m[1]);
  }
});
</script>

 

Then encode the hash in the query string:

https://www.example.com/?scrollTo=%23MyElement

 

That’ll have the same effect as:

https://www.example.com/#MyElement

(or close enough that the diff is negligible)

DeniseGreenberg
Level 4

Re: Can't link to anchors from emails

Hi @SanfordWhiteman  - Just found your latest post. I've asked them to put the (latest) script you sent in the <head>. Once that's done, is there something they need to do to label the section that they might not be doing? And is the "%23" in your link example required? To be very specific, this is the page:

 

https://www.earlens.com/providers/

 

And the content they are trying to get the link to scroll to is the first row under "Featured Research."

 

Would I use this as the URL?

 

https://www.earlens.com/providers/?scrollTo=hearing-review


Or this?


https://www.earlens.com/providers/?scrollTo=%23hearing-review

 

This is the only section of source code on the page that I can find that says "hearing-review".  Is that the correct way to name the section? Come to think of it, that appears to lead to the "Featured Research" headline rather than the first row of content underneath it. Or am I misinterpreting the code?

DeniseGreenberg_0-1646079709262.png

 

Anonymous
Not applicable

Re: Can't link to anchors from emails

Hi Nicholas

You mentioned that in your experience not all email clients will respond to anchor links. Do you know if this is the case for Outlook? I have followed the steps but cannot seem to get the anchors to work.

When I view the html version of my email the links work fine, but not when I view the email in both Outlook and Gmail.

Any comment would be much appreciated!

Anonymous
Not applicable

Re: Can't link to anchors from emails

I've just seen another article talking about exactly this which provides some answers: https://nation.marketo.com/message/122193#comment-122193

Nicholas_Manojl
Level 9

Re: Can't link to anchors from emails

Hi Dominic - I actually misinterpreted the initial question.

The link itself to an external webpage anchor will be fine.

For whatever reason I thought the scenario was an anchor within the email.