Hi all
I am creating a dynamic LP using Score Token and JavaScript.
I want to change contents based on the lead Score and some attributes.
But I can not convert Score c format to JavaScript type Number.
My Sample
<script type="text/javascript">
<!--
function score2number( score ){
var number = score;
for( i = 0 ; i < 10 ; i++ ){
number=number.replace( "/&#" + 48 + i + ";/g", i );
}
return number;
}
var x = "{{lead.Test_Score}}";
var n = 0; // Type Number
document.write("<p>Score=" + x + "</p>");
document.write("<p>len=" + x.length + "</p>");
if( x.length > 0 ){
n = parseInt(score2number( x ) );
}else{
// null
document.write("<p>null</p>");
}
document.write("<p>Number=" + n + "</p>");
-->
</script>
Result
Score=65 <==== this lead score is 65 and html is 65
len=10
Number=NaN <=== Still String type
isNaN=true
Please, teach me your solution.
Takehiro
Solved! Go to Solution.
That's pretty fragile code to unescape entities, IMO. How about:
function unescapeEntities( str ) {
var tmpEl = document.createElement('SPAN');
tmpEl.innerHTML = str;
return tmpEl.innerHTML;
}
I fixed now.
Before
for( i = 0 ; i < 10 ; i++ ){
number=number.replace( "/&#" + 48 + i + ";/g", i );
}
After
for( i = 0 ; i < 10 ; i++ ){
var pattern = new RegExp( "&#" + String(48 + i) + ";", "g" );
number=number.replace( pattern, i );
}
Thank you
Sanford
You're right.
Your sample is better than mine.
Thank you.
It's funny because I was working on exactly that task when you posted!