如何在smarty-tpl中使用javascript函数

how to using javascript function in smarty tpl

本文关键字:javascript 函数 smarty-tpl      更新时间:2023-09-26

我有一个假的访问者计数器脚本,用javascript编码,但我想在smarty tpl文件中使用它。我正在尝试这样做,但它没有显示在我想要的地方。脚本代码低于

<!--Simply copy and paste it where you wish the counter to appear.-->

<SCRIPT language="JavaScript" type="text/javascript">
// counter - from http://rainbow.arch.scriptmania.com/scripts
function fakecounter(){
//decrease/increase counter value (depending on perceived popularity of your site!)
var decrease_increase=2460
var counterdate=new Date()
var currenthits=counterdate.getTime().toString()
currenthits=parseInt(currenthits.substring(2,currenthits.length-4))+decrease_increase
document.write("You are visitor # <b>"+currenthits+"</b> to my site!")
}
fakecounter()
</script>

并且我正在尝试在CCD_ 1之后使用它。

这个脚本应该可以正常工作。如果你把它放在干净的Smarty模板文件中,你会得到类似的信息:

你是我网站的访问者#945155!

然而,在smarty的旧版本中,您需要使用{literal}才能使用JavaScript,因此您的代码应该如下所示:

<!--Simply copy and paste it where you wish the counter to appear.-->

<SCRIPT language="JavaScript" type="text/javascript">
    {literal}
    // counter - from http://rainbow.arch.scriptmania.com/scripts
    function fakecounter() {
//decrease/increase counter value (depending on perceived popularity of your site!)
        var decrease_increase = 2460
        var counterdate = new Date()
        var currenthits = counterdate.getTime().toString()
        currenthits = parseInt(currenthits.substring(2, currenthits.length - 4)) + decrease_increase
        document.write("You are visitor # <b>" + currenthits + "</b> to my site!")
    }
    fakecounter()
    {/literal}
</script>