Click to See Complete Forum and Search --> : Turn external script on/off via form/buttons???
jaydb
03-14-2003, 06:01 PM
Hello- Can any of you help me learn how to turn an external script on/off via a form/buttons on a given page?
I have a page, and I would like users to be able to turn on/off script which causes a given mouse-trail effect. I have saved the mouse-trail script as an external script, but I do not know how to create the "run external script" command or function in my webpage.
Thank you for your help!
Jay
Sure, in the .js file, (open it with Notepad) what is the name of the function? Where it says, "function" and then a word. What is that word and everything inside of the parenthesis? The ( and ).
khalidali63
03-14-2003, 06:45 PM
Originally posted by Jona
Sure, in the .js file, (open it with Notepad) what is the name of the function? Where it says, "function" and then a word. What is that word and everything inside of the parenthesis? The ( and ).
.......gosh..am I having problem understanding....or..??
Cheers
Khalid
Nedals
03-14-2003, 06:46 PM
Better yet, post a link to the web page!
I guess I wasn't clear.... what's the name of the function in the .js file? That better?
jaydb
03-14-2003, 09:30 PM
Here is the code I found on the internet that does the bouncing trailing mousetrail thing; I would cut out all above the <script> tag and all after the </script> to create the .js file
<HTML>
<HEAD>
<TITLE>Javascript Lab | Bouncing Bullets</TITLE>
</HEAD>
<BODY bgcolor="#FFFFFF" text="#000066" link="#CC6600" vlink="#CC6600" alink="#CC6600">
<div id="dot0" style="position: absolute; visibility: hidden; height: 11; width: 11;"><img src="bullet.gif" height=11 width=11></div>
<div id="dot1" style="position: absolute; height: 11; width: 11;"><img src="bullet.gif" height=11 width=11></div>
<div id="dot2" style="position: absolute; height: 11; width: 11;"><img src="bullet.gif" height=11 width=11></div>
<div id="dot3" style="position: absolute; height: 11; width: 11;"><img src="bullet.gif" height=11 width=11></div>
<div id="dot4" style="position: absolute; height: 11; width: 11;"><img src="bullet.gif" height=11 width=11></div>
<div id="dot5" style="position: absolute; height: 11; width: 11;"><img src="bullet.gif" height=11 width=11></div>
<div id="dot6" style="position: absolute; height: 11; width: 11;"><img src="bullet.gif" height=11 width=11></div>
<P>
<SCRIPT LANGUAGE="JavaScript">
<!-- Begin
var nDots = 7;
var Xpos = 0;
var Ypos = 0;
var DELTAT = .01;
var SEGLEN = 10;
var SPRINGK = 10;
var MASS = 1;
var GRAVITY = 50;
var RESISTANCE = 10;
var STOPVEL = 0.1;
var STOPACC = 0.1;
var DOTSIZE = 11;
var BOUNCE = 0.75;
var isNetscape = navigator.appName=="Netscape";
var followmouse = true;
var dots = new Array();
init();
function init() {
var i = 0;
for (i = 0; i < nDots; i++) {
dots[i] = new dot(i);
}
if (!isNetscape) {
// I only know how to read the locations of the
// <LI> items in IE
//skip this for now
// setInitPositions(dots)
}
for (i = 0; i < nDots; i++) {
dots[i].obj.left = dots[i].X;
dots[i].obj.top = dots[i].Y;
}
if (isNetscape) {
startanimate();
} else {
// let dots sit there for a few seconds
// since they're hiding on the real bullets
setTimeout("startanimate()", 3000);
}
}
function dot(i) {
this.X = Xpos;
this.Y = Ypos;
this.dx = 0;
this.dy = 0;
if (isNetscape) {
this.obj = eval("document.dot" + i);
} else {
this.obj = eval("dot" + i + ".style");
}
}
function startanimate() {
setInterval("animate()", 20);
}
function setInitPositions(dots) {
var startloc = document.all.tags("LI");
var i = 0;
for (i = 0; i < startloc.length && i < (nDots - 1); i++) {
dots[i+1].X = startloc[i].offsetLeft
startloc[i].offsetParent.offsetLeft - DOTSIZE;
dots[i+1].Y = startloc[i].offsetTop +
startloc[i].offsetParent.offsetTop + 2*DOTSIZE;
}
dots[0].X = dots[1].X;
dots[0].Y = dots[1].Y - SEGLEN;
}
function MoveHandler(e) {
Xpos = e.pageX;
Ypos = e.pageY;
return true;
}
function MoveHandlerIE() {
Xpos = window.event.x + document.body.scrollLeft;
Ypos = window.event.y + document.body.scrollTop;
}
if (isNetscape) {
document.captureEvents(Event.MOUSEMOVE);
document.onMouseMove = MoveHandler;
} else {
document.onmousemove = MoveHandlerIE;
}
function vec(X, Y)
{
this.X = X;
this.Y = Y;
}
// adds force in X and Y to spring for dot[i] on dot[j]
function springForce(i, j, spring)
{
var dx = (dots[i].X - dots[j].X);
var dy = (dots[i].Y - dots[j].Y);
var len = Math.sqrt(dx*dx + dy*dy);
if (len > SEGLEN) {
var springF = SPRINGK * (len - SEGLEN);
spring.X += (dx / len) * springF;
spring.Y += (dy / len) * springF;
}
}
function animate() {
var start = 0;
if (followmouse) {
dots[0].X = Xpos;
dots[0].Y = Ypos;
start = 1;
}
for (i = start ; i < nDots; i++ ) {
var spring = new vec(0, 0);
if (i > 0) {
springForce(i-1, i, spring);
}
if (i < (nDots - 1)) {
springForce(i+1, i, spring);
}
var resist = new vec(-dots[i].dx * RESISTANCE, -dots[i].dy * RESISTANCE);
var accel = new vec((spring.X + resist.X)/ MASS, (spring.Y + resist.Y)/ MASS + GRAVITY);
dots[i].dx += (DELTAT * accel.X);
dots[i].dy += (DELTAT * accel.Y);
if (Math.abs(dots[i].dx) < STOPVEL &&
Math.abs(dots[i].dy) < STOPVEL &&
Math.abs(accel.X) < STOPACC &&
Math.abs(accel.Y) < STOPACC) {
dots[i].dx = 0;
dots[i].dy = 0;
}
dots[i].X += dots[i].dx;
dots[i].Y += dots[i].dy;
var height, width;
if (isNetscape) {
height = window.innerHeight + document.scrollTop;
width = window.innerWidth + document.scrollLeft;
} else {
height = document.body.clientHeight + document.body.scrollTop;
width = document.body.clientWidth + document.body.scrollLeft;
}
if (dots[i].Y >= height - DOTSIZE - 1) {
if (dots[i].dy > 0) {
dots[i].dy = BOUNCE * -dots[i].dy;
}
dots[i].Y = height - DOTSIZE - 1;
}
if (dots[i].X >= width - DOTSIZE) {
if (dots[i].dx > 0) {
dots[i].dx = BOUNCE * -dots[i].dx;
}
dots[i].X = width - DOTSIZE - 1;
}
if (dots[i].X < 0) {
if (dots[i].dx < 0) {
dots[i].dx = BOUNCE * -dots[i].dx;
}
dots[i].X = 0;
}
dots[i].obj.left = dots[i].X;
dots[i].obj.top = dots[i].Y;
}
}
// End -->
</script>
<FONT color="#000066" face="Arial, Helvetica, sans-serif" size="3">This page
demonstrates a Javascript special effect using the cursor and a small image.
(bullet.gif: <IMG src="bullet.gif" width="11" height="11">)</FONT></P>
<P> </P>
<P> </P>
<P> </P>
<P> </P>
<P> </P>
<P><FONT face="Arial, Helvetica, sans-serif" size="3"><A href="home.html">JAVASCRIPT
LAB HOME</A></FONT></P>
<P><FONT color="#CC6600" face="Arial, Helvetica, sans-serif" size="3"><A href="../index.html">LAB
HOME</A></FONT></P>
</BODY>
</HTML>
jaydb
03-14-2003, 09:32 PM
so it looks like there are a few differnet functions in the .js file......
Thank you for your help!!!
Jay
jaydb
03-14-2003, 09:38 PM
The demo of the cursor thing and the code can be found here:
http://www.christopherbarth.com/lab/lab_javascripts/bouncing.html
Again, thank you!
Jay
jaydb
03-14-2003, 09:41 PM
Jona, no problem making it run. The issue is, I want visitors to my site to be able to turn the effect on/off via a button or hyperlink.
Can I somehow put a script in my page that says to run the bouncing cursor-trailer external script if user presses "on" button and disables cursor effect of they press an "off" button?
thanks!
Jay
Nedals
03-14-2003, 11:06 PM
This function is the one that runs the animation. What you need to do is stop it on command.
function startanimate() { setInterval("animate()", 20); }
Change it to....
function startanimate() { animate = setInterval("animate()", 20); }
Now add a function
function stopanimate() {
clearInterval(animate);
return true;
}
Then put a button on your page
<input type="button" value="Stop Animation" onClick="return stopanimate()">
and, if I got that right, it should work.
What nedals said, only... he said it first.:eek:
jaydb
03-14-2003, 11:56 PM
Nedals, I am in Pleasanton too. Small world. Anyway, sorry to be so helpless - I am new to html and javascript; but I can see how this would stop the animation; but I want a visitor to my page to be able to start the animation by clicking one button, or stop the animation by clicking another button.
Can you tell me how I would do that?
Thank you again.
Jay
jaydb
03-15-2003, 12:10 AM
I tried using the advice but does not work; sure error is on my part. Anyone care to check and see why this is not working?
Here is the code:
<HTML>
<HEAD>
<TITLE>Javascript Lab | Bouncing buttons</TITLE>
</HEAD>
<BODY bgcolor="#FFFFFF" text="#000066" link="#CC6600" vlink="#CC6600" alink="#CC6600">
<div id="dot0" style="position: absolute; visibility: hidden; height: 11; width: 11;"><img
src="button.gif" height=11 width=11></div>
<div id="dot1" style="position: absolute; height: 11; width: 11;"><img
src="button.gif" height=11 width=11></div>
<div id="dot2" style="position: absolute; height: 11; width: 11;"><img
src="button.gif" height=11 width=11></div>
<div id="dot3" style="position: absolute; height: 11; width: 11;"><img
src="button.gif" height=11 width=11></div>
<div id="dot4" style="position: absolute; height: 11; width: 11;"><img
src="button.gif" height=11 width=11></div>
<div id="dot5" style="position: absolute; height: 11; width: 11;"><img
src="button.gif" height=11 width=11></div>
<div id="dot6" style="position: absolute; height: 11; width: 11;"><img
src="button.gif" height=11 width=11></div>
<P>
<SCRIPT LANGUAGE="JavaScript">
<!-- Begin
var nDots = 7;
var Xpos = 0;
var Ypos = 0;
var DELTAT = .01;
var SEGLEN = 10;
var SPRINGK = 10;
var MASS = 1;
var GRAVITY = 50;
var RESISTANCE = 10;
var STOPVEL = 0.1;
var STOPACC = 0.1;
var DOTSIZE = 11;
var BOUNCE = 0.75;
var isNetscape = navigator.appName=="Netscape";
var followmouse = true;
var dots = new Array();
init();
function init() {
var i = 0;
for (i = 0; i < nDots; i++) {
dots[i] = new dot(i);
}
if (!isNetscape) {
// I only know how to read the locations of the
// <LI> items in IE
//skip this for now
// setInitPositions(dots)
}
for (i = 0; i < nDots; i++) {
dots[i].obj.left = dots[i].X;
dots[i].obj.top = dots[i].Y;
}
if (isNetscape) {
startanimate();
} else {
// let dots sit there for a few seconds
// since they're hiding on the real buttons
setTimeout("startanimate()", 3000);
}
}
function dot(i) {
this.X = Xpos;
this.Y = Ypos;
this.dx = 0;
this.dy = 0;
if (isNetscape) {
this.obj = eval("document.dot" + i);
} else {
this.obj = eval("dot" + i + ".style");
}
}
function startanimate() {
animate = setInterval("animate()", 20);
}
function stopanimate() {
clearInterval(animate);
return true;
}
function setInitPositions(dots) {
var startloc = document.all.tags("LI");
var i = 0;
for (i = 0; i < startloc.length && i < (nDots - 1); i++) {
dots[i+1].X = startloc[i].offsetLeft
startloc[i].offsetParent.offsetLeft - DOTSIZE;
dots[i+1].Y = startloc[i].offsetTop +
startloc[i].offsetParent.offsetTop + 2*DOTSIZE;
}
dots[0].X = dots[1].X;
dots[0].Y = dots[1].Y - SEGLEN;
}
function MoveHandler(e) {
Xpos = e.pageX;
Ypos = e.pageY;
return true;
}
function MoveHandlerIE() {
Xpos = window.event.x + document.body.scrollLeft;
Ypos = window.event.y + document.body.scrollTop;
}
if (isNetscape) {
document.captureEvents(Event.MOUSEMOVE);
document.onMouseMove = MoveHandler;
} else {
document.onmousemove = MoveHandlerIE;
}
function vec(X, Y)
{
this.X = X;
this.Y = Y;
}
// adds force in X and Y to spring for dot[i] on dot[j]
function springForce(i, j, spring)
{
var dx = (dots[i].X - dots[j].X);
var dy = (dots[i].Y - dots[j].Y);
var len = Math.sqrt(dx*dx + dy*dy);
if (len > SEGLEN) {
var springF = SPRINGK * (len - SEGLEN);
spring.X += (dx / len) * springF;
spring.Y += (dy / len) * springF;
}
}
function animate() {
var start = 0;
if (followmouse) {
dots[0].X = Xpos;
dots[0].Y = Ypos;
start = 1;
}
for (i = start ; i < nDots; i++ ) {
var spring = new vec(0, 0);
if (i > 0) {
springForce(i-1, i, spring);
}
if (i < (nDots - 1)) {
springForce(i+1, i, spring);
}
var resist = new vec(-dots[i].dx * RESISTANCE, -dots[i].dy * RESISTANCE);
var accel = new vec((spring.X + resist.X)/ MASS, (spring.Y + resist.Y)/ MASS + GRAVITY);
dots[i].dx += (DELTAT * accel.X);
dots[i].dy += (DELTAT * accel.Y);
if (Math.abs(dots[i].dx) < STOPVEL &&
Math.abs(dots[i].dy) < STOPVEL &&
Math.abs(accel.X) < STOPACC &&
Math.abs(accel.Y) < STOPACC) {
dots[i].dx = 0;
dots[i].dy = 0;
}
dots[i].X += dots[i].dx;
dots[i].Y += dots[i].dy;
var height, width;
if (isNetscape) {
height = window.innerHeight + document.scrollTop;
width = window.innerWidth + document.scrollLeft;
} else {
height = document.body.clientHeight + document.body.scrollTop;
width = document.body.clientWidth + document.body.scrollLeft;
}
if (dots[i].Y >= height - DOTSIZE - 1) {
if (dots[i].dy > 0) {
dots[i].dy = BOUNCE * -dots[i].dy;
}
dots[i].Y = height - DOTSIZE - 1;
}
if (dots[i].X >= width - DOTSIZE) {
if (dots[i].dx > 0) {
dots[i].dx = BOUNCE * -dots[i].dx;
}
dots[i].X = width - DOTSIZE - 1;
}
if (dots[i].X < 0) {
if (dots[i].dx < 0) {
dots[i].dx = BOUNCE * -dots[i].dx;
}
dots[i].X = 0;
}
dots[i].obj.left = dots[i].X;
dots[i].obj.top = dots[i].Y;
}
}
// End -->
</script>
<FONT color="#000066" face="Arial, Helvetica, sans-serif" size="3">This page
demonstrates a Javascript special effect using the cursor and a small image.
(button.gif: <IMG src="button.gif" width="11" height="11"> )</FONT></P>
<P> </P>
<P> </P>
<P> </P>
<P> </P>
<P> </P>
<input type="button" value="Stop Animation" onClick="return stopanimate()">
</BODY>
</HTML>
Try putting the <SCRIPT> in the <HEAD> tag instead of after the images.
Nedals
03-15-2003, 01:07 PM
Would you believe...
<input type="button" value="Start Animation" onClick="return startanimate()">
Nedals
03-15-2003, 03:09 PM
OK!
Here's the problem. When I proposed my solution I had not read through all the code. It turns out that you have a function called 'animate' which is the name I picked for the Interval timer.
So. change as shown...
function startanimate() { timer = setInterval("animate()", 20); }
function stopanimate() { clearInterval(timer); return true; }
and no changes needed for the buttons (both)
IN ADDITION, as suggested earlier, move your javascript into the <head> section of your document.
ALSO
Change this 'init();' to, onload = init; // no ()
AND, before you ask, to hide the dots when the animation stops and show then again, use this...
function startanimate() {
timer = setInterval("animate()", 20);
for (i = 0; i < nDots; i++) { dots[i].obj.visibility = 'visible' }
return true;
}
function startanimate() {
timer = setInterval("animate()", 20);
for (i = 0; i < nDots; i++) { dots[i].obj.visibility = 'visible' }
return true;
}
Works in IE (but not netscape)
MINI TUTORIAL:
Always (well, nearly always) put your Javascript (always one word), in the <HEAD> section of your document.
Notable acception: document.write();
jaydb
03-16-2003, 11:41 PM
Thank you for the help!