jnf555 Posted January 27, 2013 Share Posted January 27, 2013 hi any one help with this script i want it to go from one name to another one without stopping <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html><title>new project</title><head></head><body><script type="text/javascript">var names = ['john','gail','julie','jason','zoe'];var allnames = names.length;var i = 0;function loop(){if (i > (allnames - 1))i = 0;{document.write(names);}i++;loopTimer = setTimeout('loop()',3000);}loop();</script></body></html> thanks for any help Link to comment Share on other sites More sharing options...
Ingolme Posted January 27, 2013 Share Posted January 27, 2013 document.write() is the first problem with this script. The page will never stop loading because it stops at the script and keeps trying to write. Link to comment Share on other sites More sharing options...
jeffman Posted January 27, 2013 Share Posted January 27, 2013 What you really want is for the document to have an element, like a div or something, and update its innerHTML property every time the loop iterates. The div should have an id so you can get a reference to it using document.getElementById(). Link to comment Share on other sites More sharing options...
jnf555 Posted January 27, 2013 Author Share Posted January 27, 2013 thanks for repies Link to comment Share on other sites More sharing options...
jnf555 Posted January 27, 2013 Author Share Posted January 27, 2013 there we go this works fine <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html><title>new project</title><head></head><body><div id="jnf"></div><script type="text/javascript">var jnf2 = document.getElementById('jnf'); var names = ['john','gail','julie','jason','zoe'];var allnames = names.length;var i = 0;function loop(){if (i > (allnames - 1))i = 0;{jnf2.innerHTML = (names);}i++;loopTimer = setTimeout('loop()',3000);}loop();</script></body></html> Link to comment Share on other sites More sharing options...
xhtmlchamps1 Posted February 1, 2013 Share Posted February 1, 2013 <p>Here is the code</p><p> </p><div><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"></div><div><html></div><div><title>new project</title></div><div><head></div><div></head></div><div><body onload="loop()"></div><div> </div><div> </div><div><script type="text/javascript"></div><div>function loop(){</div><div>var names = ['john','gail','julie','jason','zoe'];</div><div>var allnames = names.length;</div><div> </div><div>for (var i = 0; i < names.length; i++) {</div><div> </div><div> </div><div> document.write(names+"<br>");</div><div> </div><div>}</div><div> </div><div>}</div><div></script></div><div></body></div><div> </div><div> </div><div> </div><div> </div><div></html></div><div> </div> Link to comment Share on other sites More sharing options...
Mudsaf Posted February 1, 2013 Share Posted February 1, 2013 (edited) <p>Here is the code</p><p> </p><div><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"></div><div><html></div><div><title>new project</title></div><div><head></div><div></head></div><div><body onload="loop()"></div><div> </div><div> </div><div><script type="text/javascript"></div><div>function loop(){</div><div>var names = ['john','gail','julie','jason','zoe'];</div><div>var allnames = names.length;</div><div> </div><div>for (var i = 0; i < names.length; i++) {</div><div> </div><div> </div><div> document.write(names+"<br>");</div><div> </div><div>}</div><div> </div><div>}</div><div></script></div><div></body></div><div> </div><div> </div><div> </div><div> </div><div></html></div><div> </div> Why there is so many divs? its like total mess. Try adding code to codearea it might help bit. Cleared the code bit<p>Here is the code</p><p> </p><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html><title>new project</title><head></head><body onload="loop()"><script type="text/javascript">function loop(){var names = ['john','gail','julie','jason','zoe'];var allnames = names.length;for (var i = 0; i < names.length; i++) {document.write(names[i]+"<br>");}}</script></body></html> Edited February 1, 2013 by Mudsaf Link to comment Share on other sites More sharing options...
Ingolme Posted February 1, 2013 Share Posted February 1, 2013 That code has the same problem as before. As soon as the first document.write() is called the whole page is erased. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now