开发者

javascript arrays in Chrome & Internet Explorer

I have this code and it worked in Google Chrome, but it didn't work in Internet Explorer.

<html><head>
 <script type ="text/JavaScript">
<!--
function status()
{
    var text = newArray();
    text[0] = "Life is traveled only once..Today's moment becomes tomorrow's memory..Enjoy every moment; good, bad, happy, or sad;because the gift of life is LIFE itself";
    text[1] = "never design your character like a garden where any one can walk. but design your character like the sky, where everyone likes to reach you";
    text[2] = "Crying is never a symbol of weakness... From the time we are born, it has always been a sign that we are alive...";
    text[3] = "If you believe, you can achieve!";
    text[4] = "BELIEVE: To hope for the best, endure the stress, passing every test & accepting nothing less.";
    text[5] = "Never give up on your dreams and goals, if you do,your life end up no where.";
    text[6] = "The only thing that is guaranteed forever is your family... they will always be your family.. everything else c开发者_开发技巧an change.";
    var i = Math.floor(7*Math.random())
    var g=window.alert(text[i]);
    return g ;
}
//-->
</script>
</head></html>


newArray(); should be new Array(); or even better, [].

Additionally, you cannot have linebreaks inside of JavaScript strings - replace the physical linebreaks with \n.

It also makes no sense to return the return value of alert().


For this sort of thing, an array literal is usually your best bet:

var text = [
    "Life is traveled only once..Today's moment becomes tomorrow's memory..Enjoy every moment; good, bad, happy, or sad;because the gift of life is LIFE itself",
    "never design your character like a garden where any one can walk. but design your character like the sky, where everyone likes to reach you",
    "Crying is never a symbol of weakness... From the time we are born, it has always been a sign that we are alive...",
    "If you believe, you can achieve!",
    "BELIEVE: To hope for the best, endure the stress, passing every test & accepting nothing less.",
    "Never give up on your dreams and goals, if you do,your life end up no where.",
    "The only thing that is guaranteed forever is your family... they will always be your family.. everything else can change."
];

That creates a new array with the entries you listed. The array entries are separated with commas. There's no comma after the last entry (if you put one there, some browsers will put an extraneous undefined entry at the end of the array because the spec used to be vague; it isn't anymore, but some older browsers still have the issue).

I don't recall the last time I actually had to write var a = new Array() (note the space). Even for a blank array, use var a = [];. It's not only shorter, but it avoid the possibility of Array (the symbol) having been shadowed.


Change the line

var g=window.alert(text[i]);

to

var g = text[i];
alert(g);


Correct code is: var text = new Array();


I would write your code like this:

var status = function() {
    var text = [
        "Life is traveled only once..Today's moment becomes tomorrow's memory..Enjoy every moment; good, bad, happy, or sad;because the gift of life is LIFE itself"
        , "never design your character like a garden where any one can walk. but design your character like the sky, where everyone likes to reach you"
        , "Crying is never a symbol of weakness... From the time we are born, it has always been a sign that we are alive..."
        , "If you believe, you can achieve!"
        , "BELIEVE: To hope for the best, endure the stress, passing every test & accepting nothing less."
        , "Never give up on your dreams and goals, if you do,your life end up no where."
        , "The only thing that is guaranteed forever is your family... they will always be your family.. everything else can change."
    ], i = Math.floor(7*Math.random());
    alert(text[i]);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜