POST Code Injection - Question

Hello!

Is it possible to use the Code Injection part of a post to add some custom data, IE a resource key.

Then in the post theme, reference the resource key thats in the post code injection part?

Resource key (in code injection) + theme template/ahref = the full link required for a button.

I think it’s possible, i just need to be pointed in the right direction on how to pull this off code wise.

Pretty sure i’ll need todo some custom varibles?

Im wanting todo a IF Member, show some code in the theme, and link in the resource key.

Thanks in advance
Sam

Is there a reason you can’t just use code injection to generate that link and apply it?

<a id="resource" href="#">Link</a>

<script>
document.getElementById('resource').href = 'https://my.site/key';
</script>
1 Like

Thanks for the reply! I think that is exactly what im after!

How would i get that URL (https://my.site/key) into the href=“#”> or will the # automatically do it?

The href="#" is just a placeholder, and translate to “when you click this link, scroll to the top of the page”.

The document.getElementById('resource').href = ... portion translates to "give me the element that has an id of resource, and set the href property to ..., and that’s the mechanism which updates the url

1 Like

That works perfectly! One more question.

Theme:

{{#if @member.paid}}
  <a id="resourcekey" class="c-btn c-btn--loading c-btn--full href="#">Download Now</a>
{{/if}}

Code Injection diffrent each per post:

   <script>
        document.getElementById('resourcekey').href = 'dece3584ef';
   </script>

What im trying todo is put the first part of the URL (clickable button) behind a paywall to a download link.

The first part of the URL is always the same,

https://domain.com/dev/474656070/

But if we add the resourcekey of “dece3584ef” to the url it’ll turn into a working link

https://domain.com/dev/474656070/dece3584ef

How would i make this work?

You can just make dece3584ef that full URL

e.g.

   <script>
        document.getElementById('resourcekey').href = 'https://domain.com/dev/474656070/dece3584ef';
   </script>

However, to add a bit more security, another option is to keep the main part of the URL hidden in the link:

<!-- handlebars -->
<noscript><style>#resourcekey{display:none}</style><noscript> <!-- hide the link for browsers that disable js-->
{{#if @member.paid}}
  <a id="resourcekey" class="c-btn c-btn--loading c-btn--full href="https://domain.com/dev/474656070/">Download Now</a>
{{/if}}

<!--- code injection -->
<script>
const el = document.getElementById('resourcekey');
if (el) {
  el.href = el.href + '{key}';
}
</script>