Javascript Not Working

Javascript Not Working

  

Hey Guys,

I have the following details in my web block:

SyntaxEditor Code Snippet

const configuration = {
  partnerID: '',
  userID: '',
};
var revieveSDK;
console.log("SDK Initialized");
function ReviceSetConf(input_userid, input_RevivepartnerID){
    console.log("Function");
    configuration.userID=input_userid;
    configuration.partnerID=input_RevivepartnerID;
    revieveSDK = new RevieveSDK(configuration.partnerID, true);
    revieveSDK.setUserId(configuration.userID);
    console.log(revieveSDK.getConfiguration());
}


I am calling the function using the below code in an expression in the same web block:

SyntaxEditor Code Snippet

"<script src='https://sdk.revieve.com/revieve-sdk.min.js'>
ReviceSetConf("+ReviveuserID+","+ RevivepartnerID+");
console.log(revieveSDK.getConfiguration());

</script>"

Where ReviveuserID and RevivepartnerID are the input parameters to the web block. The problem is that the above script does not seem to be getting executed (meaning the console prints only 'SDK Initialized'). Can anyone help?

Hi Tanveer,

Having the src attribute and content in a script tag is a bad idea. Different browsers do different things and the recommendation is actually to not execute the content when the src exists (https://www.w3.org/TR/1999/REC-html401-19991224/interact/scripts.html#edef-SCRIPT).

So, I would define two script tags: one for the src file and the other for the content.

Tell me if that worked.

Cheers,

José

José Costa wrote:

Hi Tanveer,

Having the src attribute and content in a script tag is a bad idea. Different browsers do different things and the recommendation is actually to not execute the content when the src exists (https://www.w3.org/TR/1999/REC-html401-19991224/interact/scripts.html#edef-SCRIPT).

So, I would define two script tags: one for the src file and the other for the content.

Tell me if that worked.

Cheers,

José

Thanks for the best practice, but that did not fix my problem of function not getting fired.

Within the Web Block I have two expressions now and it has the following code:

SyntaxEditor Code Snippet - Expression 1

"<script src='https://sdk.revieve.com/revieve-sdk.min.js'/>"

SyntaxEditor Code Snippet - Expression 2

"<script>
ReviceSetConf("+ReviveuserID+","+ RevivepartnerID+");
console.log(revieveSDK.getConfiguration());
console.log("+"Started Function Exec"+");
</script>"


Any other hints on how to get the second script to fire?

Solution

This worked on my end

Expression 1

"<script src='https://sdk.revieve.com/revieve-sdk.min.js'></script>"

Expression 2

"<script>
const configuration = {
  partnerID: '',
  userID: '',
};
var revieveSDK;
console.log('SDK Initialized');
function ReviceSetConf(input_userid, input_RevivepartnerID){
    console.log('Function');
    configuration.userID=input_userid;
    configuration.partnerID=input_RevivepartnerID;
    revieveSDK = new RevieveSDK(configuration.partnerID, true);
    revieveSDK.setUserId(configuration.userID);
    console.log(revieveSDK.getConfiguration());
}

ReviceSetConf('test','test');
console.log(revieveSDK.getConfiguration());
console.log('Started Function Exec');
</script>"

Note that changed I made on the parameters of ReviceSetConf

Solution
ReviceSetConf("+ReviveuserID+","+ RevivepartnerID+");

Are you passing numbers or strings?

If you are passing strings, it's better to use this:

ReviceSetConf('"+ReviveuserID+"','"+ RevivepartnerID+"');

Hi Tanveer,

Are you getting any javascript errors in the DevTools of the browser?

Cheers,

José

Thank you everyone.