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!

Attacking the scroller problem from a different angle 2

Status
Not open for further replies.

Dragonfish

Programmer
Apr 25, 2001
62
UG
First thanks for all the help I´ve had up till now.

I´ve posted this already under the heading "Bad syntax with a scroller" but I´m only now beginning to understand the problem. The script I found in a book works - I´ve just tried it - but it means writing the script in the HTML file, which I really don´t want to do, I want to work with external .js files only and they obviously behave differently !!!

In the second script - the one I want to use - I hat trouble handing over the values and I solved the problem this way:

.
.
scrolling(scrollmessage,textlength,textwidth,pos);
}

function scrolling(scrollmessage,textlength,textwidth,pos) {
.
.

This seems to work until:

.
.
setTimeout("scrolling()", 100);
}

Which calls the function scrolling again this time apparently without the values (scrollmessage,textlength,textwidth,pos)

Changeing the line to:
setTimeout("scrolling(scrollmessage,textlength,textwidth,pos)", 100);
does´nt help either.

For those who don´t want to read further I´ll say thanks for any advice
DavidinGermany

Here´s the first script (which works) all in one HTML file
***************************************************************
<html>
<head>
<script language=&quot;JavaScript&quot;>
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);

function scrolling() {
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;, 100);
}
</script>
</head>
<body onLoad=&quot;scrolling()&quot;>

</body>
</html>
***************************************************************
Now here´s the script I want to use (without any JavaSript code in the HTML file)
<html>
<head>
<script language=&quot;JavaScript&quot; src=&quot;main.js&quot; type=&quot;text/javascript&quot;></script>
</head>
<body onLoad=&quot;textscroller()&quot;>

</body>
</html>
***************************************************************
The .js file
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;, 100);
}
 
setTimeout works like eval in that you have to use a string.
so before you call it, make up a code segment which can then be used in the timed out function. You have to insert the values - you can't use variable.

e.g.

var scrollMessage = &quot;someString&quot;;
var codeSegment = &quot;scrolling(&quot; + scrollMessage +&quot;)&quot;;
setTimeout(codeSegment,1000);

With something like this you probably want to iron ut all these litle kinks first, otherwise you will not get a true idea of where the bugs are in the full code.

[bb]
 
Thank activeX,

to tell the truth I´m still confused about why setTimeout can call a function in a HTML file and the function has no trouble working with changeing values and why that does´nt work in an external file - but the rest will have to wait till tomorrow.

DavidinGermany
 
The browser makes no distinction between external JavScript files, and script in the document provided there are no variable conflicts, and everything is in the right place. Make sure you load the external file first.

So have you got it running without the external file?
 
also there is no function textScroller() which is called from your onLoad on the ext. code sample -Greg :-Q

flaga.gif
 
ok, I take that back...but why are you still initializing variables in a seperate function, taking then out would solve the problem....

this code works fine and i tested it internal and external:

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);

function scrolling() {
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;, 100);
} -Greg :-Q

flaga.gif
 
No I have´nt got it running (in the external Script file version) and to be honest I simply don´t know how values are passed and kept in various situations. In the book version <body onLoad=&quot;scrolling()&quot;> starts the function which already has the values from the JavaScript at the beginning of the head section, WHITHOUT THE VALUES BEEING EXPLICITLY PASSED and the Timeout is started with simple setTimeout(&quot;scrolling()&quot;, 100);
All tha works.

Using an external .js Script I have to specifically pass the required values:
scrolling(scrollmessage,textlength,textwidth,pos);
and specifically recieve them:
function scrolling(scrollmessage,textlength,textwidth,pos)
which seems to change the whole picture dramatically. A simple:
setTimeout(&quot;scrolling()&quot;, 100);
at the end of the function obviously does´nt work, which means that the function does´nt keep it´s values - which it does in the first version. At the moment I seem to have bitten off more than I can chew.

Thanks DavidinGermany

 
that code i just posted is tested and it works, put that in an external script and call it:

<script language=&quot;javascript&quot; src=&quot;scroller.js&quot;></script>
<script language=&quot;javascript&quot;>

other jscript on page here

</script>

if you can't get it to work, then you are doin somethin wrong

-Greg :-Q

flaga.gif
 
Hi Greg,

it worked, which to be honest I expected it to. But it did´nt solve the real problem. Could be you´re pretty fed up with this problem, but to finally put the lid on this case - this is what I was really trying to attchieve. As a newbie to JavaScript I could simply write up the various examples, watch them work and lern very little. What surprises me is that the many routines that can be downloaded (or are found in books) are all written IN THE HTML PAGE. None of them using external .js scripts. And as this case shows there is a difference in the way values are passed inside and outside HTML files. When I finally find out how this routine works with an external .js file I´ll let everybody know.

I appreciate your help!!! - on to pastures new :)).
DavidinGermany
 
there is no difference in interaction, put that script inside or outside a page it will work fine...sorry if i sounded a little fustrated before, i was just in a hurry... -Greg :-Q

flaga.gif
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top