如何将此Javascript添加到我的AMP网页?

时间:2017-06-06 05:28:08

标签: amp-html

此脚本是圣诞节的倒计时

是否可以在AMP HTML网页中显示?如果可能,该怎么办?什么是我应该添加到AMP网页的minify Javascript,请帮助我。

<script type="text/javascript">// <![CDATA[
var today=new Date()

//Enter the occasion's MONTH (1-12) and DAY (1-31):
var theoccasion=new Date(today.getFullYear(), 12, 25)

//Customize text to show before and on occasion. Follow grammer below:
var beforeOccasionText="left until Christmas :) "
var onOccasiontext="Today is Christmas. Happy Holidays!"

var monthtext=new 
Array("Jan","Feb","Mar","April","May","June","July","Aug","Sep","Oct","Nov","Dec")
theoccasion.setMonth(theoccasion.getMonth()-1) //change to 0-11 month format
var showdate="("+monthtext[theoccasion.getMonth()]+" "+theoccasion.getDate()+")" //show date of occasion

var one_day=1000*60*60*24
var calculatediff=""

calculatediff=Math.ceil((theoccasion.getTime()-today.getTime())/(one_day))
if (calculatediff<0){ //if bday already passed
var nextyeartoday=new Date()
nextyeartoday.setFullYear(today.getFullYear()+1)
calculatediff=Math.ceil((nextyeartoday.getTime()-
today.getTime())/(one_day)+calculatediff)
}

//Display message accordingly
var pluraldayornot=(calculatediff==1)? "day" : "days"
if (calculatediff>0)
    document.write("<b>"+calculatediff+" "+pluraldayornot+" "+beforeOccasionText+" "+showdate+"!</b>")
else if (calculatediff==0)
document.write("<b>"+onOccasiontext+" "+showdate+"!</b>")
// ]]></script>

2 个答案:

答案 0 :(得分:0)

AMP网站中的JavaScript为forbidden

答案 1 :(得分:0)

您无法在AMP html中使用自定义JavaScript。相反,您应该使用AMP自定义组件之一来模仿脚本应该执行的任何操作。如果你告诉我你正在尝试用这个脚本做什么,我可能会指出你正确的方向。

编辑:你可以通过使用可以为你做数学运算的HTML模板语言来实现你想要做的事情。