2

A friend of mine wanted me to gather some statistics for his website so I gave him the following code to insert in his page footers:

<div>
    <script>
       var wandTopSitesUserId;
       match = document.cookie.match(new RegExp("WandTopWildlifeSites" + '=([^;]+)'));
       if (match)
          wandTopSitesUserId = match[1];
       else {
          wandTopSitesUserId = (+new Date * Math.random()).toString(36).slice(2, 12);
          document.cookie = 'WandTopWildlifeSites=' + wandTopSitesUserId + '; expires=Tue, 1 Jan 2030 00:00:00 UTC; path=/';
       }

       document.write('<div style="visibility: hidden;"><a href="http://www.www.fxxxxx.com/"><img src="http://xxxxxx.azurewebsites.net/Log/LogVisit/?siteId=2&userId=' + wandTopSitesUserId + '&pageName=' + location.pathname + '" alt="WAND Top Wildlife Sites" /></a></div>');
    </script>
 </div>

What I didn't know is that he's using PHP and I know absolutely nothing about PHP. When he attempts to insert my code into his PHP script, he gets the following error:

string(125) "Smarty error: [in globalcontent:footer line 9]: [plugin] unknown tag - 'wandTopSitesUserId' (core.load_plugins.php, line 198)" string(137) "Smarty error: [in globalcontent:footer line 9]: syntax error: unrecognized tag 'wandTopSitesUserId' (Smarty_Compiler.class.php, line 590)"

Not knowing anything about PHP, I have idea how to fix this.

0

1 Answer 1

3

He uses the samrty template engine which requires you to escape javascript by encapsulating it within {literal}...{/literal}:

<div>
    {literal}
    <script>
       var wandTopSitesUserId;
       match = document.cookie.match(new RegExp("WandTopWildlifeSites" + '=([^;]+)'));
       if (match)
          wandTopSitesUserId = match[1];
       else {
          wandTopSitesUserId = (+new Date * Math.random()).toString(36).slice(2, 12);
          document.cookie = 'WandTopWildlifeSites=' + wandTopSitesUserId + '; expires=Tue, 1 Jan 2030 00:00:00 UTC; path=/';
       }

       document.write('<div style="visibility: hidden;"><a href="http://www.www.fxxxxx.com/"><img src="http://xxxxxx.azurewebsites.net/Log/LogVisit/?siteId=2&userId=' + wandTopSitesUserId + '&pageName=' + location.pathname + '" alt="WAND Top Wildlife Sites" /></a></div>');
    </script>
    {/literal}
 </div>
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you. I just sent this to him and I should know soon if it works.
Send him this link for further documentation: smarty.net/docs/en/language.function.literal.tpl

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.