Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Bad syntax with a scroller 1

Status
Not open for further replies.

Dragonfish

Programmer
Apr 25, 2001
62
UG
I´m trying this scroller I found in a book. I made a minor change in that I put the entire code into an external js. script. I´ve added some document.write(); to see where the thing crashes and it does´nt like pos++; here´s the text:

function TextScroller() {
var scrollmessage=" *** Dies ist ein Testtext *** Dies ist ein 2 Testtext *** Dies ist ein 3 Testtext";
var textlength = scrollmessage.length;
var textwidth = 50;
var pos = -(textwidth + 2);
document.write(scrollmessage + &quot;<br>&quot;);
document.write(textlength + &quot;<br>&quot;);
document.write(textwidth + &quot;<br>&quot;);
document.write(pos + &quot;<br>&quot;);
ScrollStatus();
}

function ScrollStatus() {
document.write(&quot;hallo<br>&quot;);
pos++;
document.write(pos);
var scroller = &quot; &quot;;
if (pos == textlength) {
pos = -(textwidth + 2);
}
if (pos < 0) {
for (var i = 1; i <= math.abs(pos); i++) {
// Leerzeichen einfügen
scroller = scroller + &quot; &quot;;
}
scroller = scroller + scrollmessage.substring(0, textwidth - i + 1);
}
else {
scroller = scroller + scrollmessage.substring(pos, textwidth + pos);
}
window.status = scroller;
setTimeout(&quot;Scroll()&quot;, 1000);
}

IE says the error was in line 16 - object expected. the js. is called with <body onLoad=&quot;TextScroller()&quot;> I´m wondering wether the first function is parsing the content of pos to the second function but I´m new to JavaScript so I´d appreciate some help - thanks DavidinGermany
 
Well it's two functions with the end of the first one calling the second, so at first glance I'd say make them one function. In other words:
Code:
...
document.write(pos + &quot;<br>&quot;);
document.write(&quot;hallo<br>&quot;);
...
Variables are local to a function, so unless you specifically pass the pos variable to the second function it would have no idea what to do -- when you try to do pos++ on a variable that doesn't exist then it would break.

Also math.abs() should be Math.abs()

You may want to find other scroller scripts, might be fastest just to replace it if it isn't working (though not as good as a learning experience). Good luck!
 
Thanks Glowball for the reply,

I´m teaching myself js so the learning is more important than a better scroller. I´m off to bed now - been doing night shift - and I´ll try the improvements in about 12 hours.

I thought pos hat been passed to the second function as I´d document.write(pos + &quot;<br>&quot;); and got a result. Just for the record - how do I pass one variabl to a second function?

Davidin Germany
 
Thanks TA for the reply,

I had´nt see that one about setTimeout(&quot;Scroll()&quot;, 1000);
the original function was named Scroll() and I changed it to ScrollStatus() being mindfull of reserved names. I´ve changed the setTimeut to setTimeout(&quot;ScrollStatus()&quot;, 1000); but that does´t affect the problem. The lines:

document.write(&quot;hallo<br>&quot;);
pos++;
document.write(pos);

are only there to check when he function crashes - the scroller does´nt need all those document.writes. document.write(&quot;hallo<br>&quot;); gets written - IE points to the line after as the error (object expected) and document.write(pos); does´nt get written so being naive I´m just assumung that the value of pos has´nt been passed from one functon to the other - and that´s where I´m stuck.

DavidinGermany
 
yes the variables aren't being passed onto the next function...the first line of your jscript is where constant global variables are usually declared like this:

var variable1, variable2, variable3

or

var variable1 = &quot;blah&quot;;
var variable2 = 12;

so in your case you need:

var scrollmessage=&quot; *** Dies ist ein Testtext *** Dies ist ein 2 Testtext *** Dies ist ein 3 Testtext&quot;;
var textwidth = 50;
var textlength = scrollmessage.length;
var pos = -(textwidth + 2);

function TextScroller() {

document.write(scrollmessage + &quot;<br>&quot;);
document.write(textlength + &quot;<br>&quot;);
document.write(textwidth + &quot;<br>&quot;);
document.write(pos + &quot;<br>&quot;);

ScrollStatus();
}

function ScrollStatus() {......

any variable that is going to be used in another function has to be declared either way i mentioned above outside of a function (so could just declare the variable name and then fill it inside your function) or you can just pass the variable into the next function:

function getTxt() {

var inText1 = document.form.element.value;
var inText2 = document.form.element2.value;
alertTxt(inText1, inText2);

}
function alertTxt(txt1, txt2) {

alert(txt1);
alert(txt2);

}

here the variables 'inTxt1' and 'inTxt2' are sent to the function alertTxt() where the variable names are changed to 'txt1' and 'txt2' for processing in the function...if you didnt want to change the name you could just declare the function like this: function alertTxt(inTxt1, inTxt2) {}

-hope that helps!


-Greg :-Q

flaga.gif
 
Thanks Greg for the reply,

yes the problem was the passing of variables. After having done a bit of reading I found out how to do it, which means I´ve cleaned up the code (dropped the document.writes). The code is almost identical with the example out of the book but I´m now getting an error for line 24 object undefined ??? We´re getting close !!!!!!!!!!!!!!!!!!!!!! Here´s the cleand up code and thanks everyone for the help

DavidinGermany

function textscroller() {
var scrollmessage=&quot; *** Dies ist ein Testtext *** Dies ist ein 2 Testtext *** Dies ist ein 3 Testtext&quot;;
var textlength = scrollmessage.length;
var textwidth = 50;
var pos = -(textwidth + 2);
scrolling(scrollmessage,textlength,textwidth,pos);
}

function scrolling(scrollmessage,textlength,textwidth,pos) {
pos++;
var scroller = &quot; &quot;;
if (pos == textlength) {
pos = -(textwidth + 2);
}
if (pos < 0) {
for (var i = 1; i <= Math.abs(pos); i++) {
// Leerzeichen einfügen
scroller = scroller + &quot; &quot;;
}
scroller = scroller + scrollmessage.substring(0, textwidth - i + 1);
}
else {
scroller = scroller + scrollmessage.substring(pos, textwidth + pos);
}
window.status = scroller;
setTimeout(&quot;scrolling()&quot;, 1000);
}
 
well first of all, you don't need the first function..declare those variables right under the <script> tag and onLoad to textScroller...now, i think you have a logic error in your code...your tests are returning invalid substrings, look over the tests and then get out the pencil and paper and follow a string through your script -Greg :-Q

flaga.gif
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top