繁体   English   中英

如何简化这个Javascript文件

[英]How to simplify this Javascript 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

那就是数组,你可以这样

var Quotation=["Time is of the essence! Comb your hair.","Sanity is a golden apple with no shoelaces.",... and so on]

您的第二种方式是维护XML,这将使您可以自由地扩展您的报价列表。

创建一个类似quot.xml的xml并通过js访问它

<QuotList>
 <quotation>Time is of the essence! Comb your hair.</quotation>
 <quotation>Sanity is a golden apple with no shoelaces</quotation>
</QuotList>

创建一个文件,我们称之为quotes.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="path/to/quotes.js"></script>

并从这里做任何你想做的事情:

<script>
var Q = Quotation.length;
var whichQuotation=Math.round(Math.random()*(Q-1));
function showQuotation(){document.write(Quotation[whichQuotation]);}
showQuotation();
</script>

注意:

从html5开始,您不再需要脚本标记中的javascript和text / javascript,因为它们现在是脚本标记的默认值。

笔记2:

你不应该在生产中使用document.write!

在HTML文件中包含文件quotes.js

HTML部分:

<!DOCTYPE html>
<html>
    <head>
        <script src="quote.js"></script>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    </head>
    <body>
        <span>Famous Quote:

        <script language="JavaScript">
            console.log(Quotation);
            var Q = Quotation.length;
            var whichQuotation=Math.round(Math.random()*(Q-1));
            function showQuotation(){document.write(Quotation[whichQuotation]);}
            showQuotation();
        </script>
    </body>
</html>

注意:创建一个新文件quote.js并将此代码放在一个文件中,此文件已包含在HTML页面中。

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."
]

首先,简化数组声明:

var Q = [
  "Time is of the essence! Comb your hair.",
  "Sanity is a golden apple with no shoelaces."
];

然后,你的随机是不平等的。 请改用Math.floor(Math.random()*Q) ;

document.write(Q[Math.floor(Math.random()*Q.length)]);

然后将数组声明移动到自己的js文件( quotes.js )中,只需包括:

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

就这样:

<script src="quotes.js"></script>
document.write(Q[Math.floor(Math.random()*Q.length)]);

这很棒! 感谢大家!

 var quotes = [ "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." ]; var randQuote = quotes[Math.floor(Math.random()*quotes.length)]; document.write(randQuote); 

这是我将如何做代码...

 var quotes = [ "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." ]; var randQuote = quotes[Math.floor(Math.random()*quotes.length)]; document.write(randQuote); 

您可以使用模块加载器将代码拆分为不同的文件。

index.html内容:

<script data-main="scripts/main.js" src="//cdnjs.cloudflare.com/ajax/libs/require.js/2.1.15/require.js"></script>

scripts/main.js内容:

require(['quotations'], function (quotations) {
    var whichQuotation = Math.floor(Math.random() * quotations.length);
    function showQuotation(){
        document.write(quotations[whichQuotation]);
    }
    showQuotation();
});

scripts/quotations.js内容:

define(function () {
    return [
        "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."
    ];
});

JavaScript没有import语句。 <script>标签拆分代码很困难,因为像Quotation这样的变量会成为全局变量。 因此,如果在您的网站上工作的任何人试图定义一个名为Quotation的变量,那么你们两个都搞砸了。 使用模块加载器可以共享引用列表而不使用该变量。

我还修复了你的随机索引算法。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM