I have a Cloud page where I am invoking a code resource (using below script tag) which has all my AMPScript written - <script type="text/javascript"src="http://cloud.homes.xyz.email/RetrieveMemberdata.js"></script>

I want to pass the SubscriberKey from this so that I can access that in my code resource. How can I do that?


You can set any JS variable in your page context, which becomes available in your included JS file. Here is an example:

<script type="text/javascript">
var SubscriberKey = "%%=v(@SubscriberKey)=%%";
<script type="text/javascript" src="http://cloud.homes.xyz.email/RetrieveMemberdata.js">

You can now use SubscriberKey within your JS file, and it will give you the value it gets passed from AMPScript.

I don't know how you retrieve your Subscriber Key, but you can do it like Jonas suggests, using RequestParameter(), e.g.:

<script type="text/javascript">
var SubscriberKey = "%%=RequestParameter(SubscriberKey)=%%";

The key principle is to set the SubscriberKey JS value before you include your external JS file.

Also, I recommend using CloudPagesURL is your are sending people to this CloudPage from your emails. This will prevent SubscriberKey being disclosed in the URL. Not that important, as you in this case either way will reveal it in HTML, but useful when you only process it in SSJS/AMPScript.


I understand your problem as the code resource missing subscriber context. Or is there a necessity to handle this inside the script tag (not sure how that would work)?

Why don't you, inside the AMPScript in your caller page, send a HTTPPOST2() call from the caller page toward the code resource with a parameter including the subscriberKey?


Then, on your Code Resource, retrieve said parameter using AMPScript function: RequestParameter().


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.