SOLVED

Using Score Token in JavaScript issue

Go to solution
Anonymous
Not applicable

Using Score Token in JavaScript issue

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 &#99 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 &#54;&#53;

len=10

Number=NaN     <=== Still String type

isNaN=true

Please, teach me your solution.

Takehiro

1 ACCEPTED SOLUTION

Accepted Solutions
SanfordWhiteman
Level 10 - Community Moderator

Re: Using Score Token in JavaScript issue

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;

}

View solution in original post

4 REPLIES 4
Anonymous
Not applicable

Re: Using Score Token in JavaScript issue

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

SanfordWhiteman
Level 10 - Community Moderator

Re: Using Score Token in JavaScript issue

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;

}

View solution in original post

Anonymous
Not applicable

Re: Using Score Token in JavaScript issue

Sanford

You're right.

Your sample is better than mine.

Thank you.

SanfordWhiteman
Level 10 - Community Moderator

Re: Using Score Token in JavaScript issue

It's funny because I was working on exactly that task when you posted!