javascript - 如何简化这个 Javascript 文件

标签 javascript php file

我有一个这样的脚本:

<span>Famous Quote:
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script language="JavaScript">
var Quotation=new Array() // do not change this!
Quotation[0] = "Time is of the essence! Comb your hair.";
Quotation[1] = "Sanity is a golden apple with no shoelaces.";
Quotation[2] = "Repent! The end is coming, $9.95 at Amazon.";
Quotation[3] = "Honesty blurts where deception sneezes.";
Quotation[4] = "Pastry satisfies where art is unavailable.";
Quotation[5] = "Delete not, lest you, too, be deleted.";
Quotation[6] = "O! Youth! What a pain in the backside.";
Quotation[7] = "Wishes are like goldfish with propellors.";
Quotation[8] = "Love the river's \"beauty\", but live on a hill.";
Quotation[9] = "Invention is the mother of too many useless toys.";
Quotation[10] = "Things are only impossible until they're not.";
var Q = Quotation.length;
var whichQuotation=Math.round(Math.random()*(Q-1));
function showQuotation(){document.write(Quotation[whichQuotation]);}
showQuotation();
</script>

如何将引文放在它们自己的文件中,而不是源代码中?这是一个例子:http://mydomain.go/quote.js

最佳答案

这样会更好:

var Quotation=
[
    "Time is of the essence! Comb your hair.",
    "Sanity is a golden apple with no shoelaces.",
    "Repent! The end is coming, $9.95 at Amazon.",
    "Honesty blurts where deception sneezes.",
    "Pastry satisfies where art is unavailable.",
    "Delete not, lest you, too, be deleted.",
    "O! Youth! What a pain in the backside.",
    "Wishes are like goldfish with propellors.",
    "Love the river's \"beauty\", but live on a hill.",
    "Invention is the mother of too many useless toys.",
    "Things are only impossible until they're not."
]

如果你真的想的话,你可以把它放在一个单独的脚本中。除此之外,没有任何“简单”的方法来改进脚本。

编辑:

放在单独的文件中,然后添加脚本标签:

<script src="whatever.js"></script>

然后您将可以访问主文件中的 Quotation

关于javascript - 如何简化这个 Javascript 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27141522/

相关文章:

javascript - Firebase 匿名登录安全

javascript - JS 中切换音频

php - 更改 Wordpress 中的子菜单换行

jquery - 我可以设置文件下载到的文件名吗?

java - 使用文件参数通过 Java 运行 python 脚本

javascript - 如何创建一条线以在条形图中显示阈值

javascript - 如何在传单中使用javascript从MySql获取数据

php - 根据另一个 JSON 编码数组对另一个数组进行排序

php - 根据时间DESC连接两张表

java - 是否有 "embedded DBMS"来支持同一数据库文件上的多个编写器应用程序(进程)?