Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to simplify this Javascript File

I have a script such as this one:

<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>

How can I put the quotations in their own file, rather than the source code? Here is an example: http://mydomain.go/quote.js

like image 422
Racer Avatar asked Jun 04 '26 20:06

Racer


2 Answers

This would be better:

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

You could put that in a separate script if you really wanted to. Beyond that, there isn't any 'simple' way to improve the script.

Edit:

Put it in a separate file, then add a script tag:

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

Then you will have access to Quotation in your main file.

like image 75
JayArby Avatar answered Jun 07 '26 08:06

JayArby


Well that is array , you can go like this

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

Your second way could be maintaining XML, Which will give you the freedom to expand your Quotations list in fututre.

Create an xml like quot.xml And access it via js

<QuotList>
 <quotation>Time is of the essence! Comb your hair.</quotation>
 <quotation>Sanity is a golden apple with no shoelaces</quotation>
</QuotList>
like image 37
Tushar Avatar answered Jun 07 '26 08:06

Tushar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!