Question:

Can anyone tell me what's wrong with this code?

by  |  earlier

0 LIKES UnLike

<html>

<body>

<script language="Javascript">

colours=new Array("black","white");

index=0;

document.write(<h1>Blinking text</h1>);

ref=setInterval("func()",500);

Function func()

{

if(index>1)

{

index=0;

document.fgcolor=colours[index];

index++;

}

else

{

document.fgcolor=colours[index];

index++;

}

}

</script>

</body>

</html>

 Tags:

   Report

8 ANSWERS


  1. document.write(&lt;h1&gt;Blinking text&lt;/h1&gt;);

      -&gt;

    document.write(&quot;&lt;h1&gt;Blinking text&lt;/h1&gt;&quot;);

    Also your if statement will NEVER execute the second block because index can only ever be 0 or 1 (it is reset to 0 by the first block).


  2. Here&#039;s code that I tested that appears to work. There are a couple of things wrong with your logic. The color will always be white after the first time, you need to toggle between 0 &amp; 1 (or if there are more than 2 colors, toggle &quot;index&quot; between 0 &amp; n).

    Another thing is you shouldn&#039;t rewrite the entire page, which is what &quot;document.write()&quot; will do. Simple leave an element (or elements) on the page, and toggle their color, by modifying their style attribute.

    &lt;html&gt;

    &lt;HEAD&gt;

    &lt;script language=&quot;Javascript&quot;&gt;

    colours=new Array(&quot;black&quot;,&quot;white&quot;);

    index=0;

    function func() {

    alert(document.getElementById(&quot;myText&quot;...

    if(index==1) {

    index=0;

    document.getElementById(&quot;myText&quot;).styl...

    } else {

    index=1;

    document.getElementById(&quot;myText&quot;).styl...

    }

    //document.write(colours[index]);

    ref=window.setTimeout(&quot;func()&quot;,1000);

    }

    &lt;/script&gt;

    &lt;/HEAD&gt;

    &lt;body onload=&quot;func()&quot;&gt;

    &lt;h1 id=&quot;myText&quot;&gt;Blinking text&lt;/h1&gt;

    &lt;/body&gt;

    &lt;/html&gt;

  3. Know about webdesing tips,tricks and review

    of hosting services also post your coment

    join

    http://smtechnology.freeforums.org/


  4. I&#039;m not adept at JavaScript, but I do think that your error resides on the if-statement construction that you had. I&#039;ll highlight it for you.

    Function func()

    {

    if(index&gt;1)

    {

    index=0;

    document.fgcolor=colours[index];

    index++;

    }

    else

    {

    document.fgcolor=colours[index];

    index++;

    }

    }

    Right, you have initialized index to 0. But, the construction of your if-statement makes it static.

    When index = 1, you set it back to 0, and you increment it again. Thus, the program will always evaluate this part, will never leave this part, since index is always 1. Here&#039;s the proposed change:

    Function func()

    {

    if(index==1)

    {

    document.fgcolor=colours[index];

    index = 0;

    }

    else

    {

    document.fgcolor=colours[index];

    index = 1;

    }

    }

    In this way, your program just switches between 0 and 1.

    Hope this solves your problem.

  5. Try function in place of Function. That should remove the error you are getting.

    I would also suggest you look into something called jQuery [ http://jquery.com/ ] it allows writing of cross browser supported Javascript. And it it also much easier once you get it.

    Another recommendation is to download Firefox [ http://www.mozilla.com/en-US/firefox/ ] and install the Firebug addon [ https://addons.mozilla.org/en-US/firefox... ]. I used Firebug to find the error.

  6. what are you trying to do?

    are you trying to put it on your myspace as a layout?


  7. dude... if this is for myspace... it wont work because myspace doesnt allow javascript... try using normal HTML code!

  8. Ok, other answers have gotten pieces of the answer, but here is what I needed to do to get it working:

    &lt;html&gt;

    &lt;body&gt;

    &lt;script language=&quot;Javascript&quot;&gt;

    colours=new Array(&quot;black&quot;,&quot;white&quot;);

    index=0;

    document.write(&#039;&lt;h1&gt;Blinking text&lt;/h1&gt;&#039;);

    ref=setInterval(&quot;func()&quot;,500);

    function func()

    {

    if(index==1)

    {

    index=0;

    document.body.style.color =colours[index];

    }

    else

    {

    index=1;

    document.body.style.color = colours[index];

    }

    }

    &lt;/script&gt;

    &lt;/body&gt;

    &lt;/html&gt;

    1. Quotes around &lt;h1&gt;Blinking text&lt;/h1&gt;

    2. &quot;Function&quot; should be lower case &quot;function&quot;

    3. Change the way index is evaluated

    4. Use document.body.style.color instead of document.fgcolor which has been deprecated.

Question Stats

Latest activity: earlier.
This question has 8 answers.

BECOME A GUIDE

Share your knowledge and help people by answering questions.