-2

Hi i have an html widget with javascript code and a variable named snapAuthor but when i call the variable the console tells me it didn't find it and this is the tool code :

<b:widget id='LinkList7' locked='false' title='Joker Template' type='LinkList' visible='true'>
<b:includable id='AUTH'><b:if cond='data:widget.instanceId == &quot;LinkList500&quot;'><b:tag name='script' type='text/javascript'><b:loop values='data:links' var='link'>AuthorsInfo[&#39;<data:link.name/>&#39;]=&#39;<data:link.target.jsEscaped/>&#39;;</b:loop></b:tag><b:else/>
<b:tag name='script' type='text/javascript'>
(function(){var snapAuthor=AuthorsInfo.filter(function(a){return a.name===&#39;<data:title/>&#39;})[0];


if(snapAuthor!==undefined){snapAuthor.provided=true;

<b:loop values='data:links' var='link'>
<b:if cond='data:link.name contains &quot;-ad&quot;'>
snapAuthor[&#39;<data:link.name/>&#39;]=&#39;<data:link.target.jsEscaped/>&#39;;
<b:else/>
<b:switch var='data:link.name'>
<b:case value='rank'/>snapAuthor.rank=&#39;<data:link.target.escaped/>&#39;;
<b:case value='about'/>snapAuthor.about=&#39;<data:link.target.escaped/>&#39;;
<b:default/>snapAuthor.links[&#39;<data:link.name/>&#39;]=&#39;<data:link.target/>&#39;;
</b:switch></b:if></b:loop>}})();</b:tag></b:if>
<b:tag name='script' type='text/javascript'>
console.log(snapAuthor);
</b:tag>
</b:includable>
</b:widget>

Please note that I want to call the variable from elsewhere on the page but the console tells me that it did not find it...

MSDesCUC
  • 17
  • 5

1 Answers1

1

Using Immediately-Invoked Function Expression (function(){})() in your code prevents varibles from being visible outside its scope

Try this

<b:widget id='LinkList7' locked='false' title='Joker Template' type='LinkList' visible='true'>

    <b:includable id='AUTH'>
        <b:if cond='data:widget.instanceId == "LinkList500"'>
            <b:tag name='script' type='text/javascript'>
                <b:loop values='data:links' var='link'>AuthorsInfo['<data:link.name/>']='<data:link.target.jsEscaped/>';</b:loop>
            </b:tag>
        <b:else/>
            <b:tag name='script' type='text/javascript'>

                var snapAuthor=AuthorsInfo.filter(function(a){return a.name==='<data:title/>'})[0];
                if(snapAuthor!==undefined){snapAuthor.provided=true;

                    <b:loop values='data:links' var='link'>

                        <b:if cond='data:link.name contains "-ad"'>
                            snapAuthor['<data:link.name/>']='<data:link.target.jsEscaped/>';
                        <b:else/>
                            <b:switch var='data:link.name'>
                                <b:case value='rank'/>snapAuthor.rank='<data:link.target.escaped/>';
                                <b:case value='about'/>snapAuthor.about='<data:link.target.escaped/>';
                                <b:default/>snapAuthor.links['<data:link.name/>']='<data:link.target/>';
                            </b:switch>
                        </b:if>

                    </b:loop>   
                }

            </b:tag>
        </b:if>

            <b:tag name='script' type='text/javascript'>
            console.log(snapAuthor);
            </b:tag>
    </b:includable>

</b:widget>