• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
  • Bear Bibeault
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Piet Souris
  • salvin francis
  • Stephan van Hulst
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Jj Roberts

Can anybody try to explain this script part code.

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can anybody try to explain the script part
window.status=myMsg.substring(i, myMsg.length) + myMsg.substring(0, i-1)
Below is the code.
<html>
<head>
<title>My JavaScript page</title>
<SCRIPT LANGUAGE=JAVASCRIPT TYPE="TEXT/JAVASCRIPT">
< !-- Hide script

myMsg = "Hey"

i=0
function scrollMsg() {
window.status=myMsg.substring(i, myMsg.length) + myMsg.substring(0, i-1)
if( i < myMsg.length) {
i++
}
else {
i=0
}

setTimeout("scrollMsg()",1000)
}
// End -->
</SCRIPT>
</head>
<body bgcolor=white onLoad="scrollMsg()">
<h2>I'm a kewl JavaScript dood with a scrolling status bar!</h2>
</body>
</html>
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your code was interpreted rather than displayed, because that's what normally happens when you post HTML code (though Paul may want to turn off HTML in this forum.) You can defeat this by putting a space after every < symbol, so that it doesn't look like part of an HTML tag to the interpreter. Of course we have to remember that the real code doesn't have these spaces:
<code><pre>
< html>
< head>
< title>My JavaScript page< /title>
< SCRIPT LANGUAGE=JAVASCRIPT TYPE="TEXT/JAVASCRIPT">
< !-- Hide script

myMsg = "Hey"

i=0
function scrollMsg() {
window.status=myMsg.substring(i, myMsg.length) + myMsg.substring(0, i-1)
if( i < myMsg.length) {
i++
}
else {
i=0
}

setTimeout("scrollMsg()",1000)
}
// End -->
< /SCRIPT>
< /head>
< body bgcolor=white onLoad="scrollMsg()">
< h2>I'm a kewl JavaScript dood with a scrolling status bar!< /h2>
< /body>
< /html>
</pre></code>
(Alternately, you could replace each "<" with "& lt;" (without the space), but that's more of a pain - and very confusing if you re-edit later and each "& lt;" has been replaced by "<". If you don't re-replace them each time you re-edit, it doesn't work correctly.
Anyway - I've never really used JavaScript, so I'll let someone else field the actual question.
[This message has been edited by Jim Yingst (edited March 02, 2000).]
 
Jim Yingst
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, taking another look, I can guess. It's erroring because the first time through, i = 0, and MyMsg.substring(0, i-1) evaluates to MyMsg.substring(0, -1) which is outside the range of legal values. If JavaScript is like Java in how substring() is set up, then you don't need the i-1 anyway - try it with just i instead. E.g. substring(0, 2) would get all characters from 0 to 1, omitting the right endpoint 2. I don't have a good JavaScript reference offhand, so hopefully someone will correct me if this is wrong.
 
I guess everyone has an angle. Fine, what do you want? Just know that you cannot have this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic