...

View Full Version : OnClick Script



JeStEr
07-22-2002, 02:10 AM
I need an OnClick script that when you click on an image it plays a sound and then links to a website. Are there any suggestions for a script?

Mrs G
07-22-2002, 09:41 PM
The following will allow you to do what you require.

The loading of the page is delayed until the sound has finished playing.

The example delay is 3 seconds but you will have to alter this for the length of your sound file.

You must enter:

1) The page to load
2) The sound file name
3) The picture name
4) The new delay time



<SCRIPT LANGUAGE=JavaScript>
function playSound(){
document.music.play();
setTimeout("window.location='yourpage.html'",3000 )
}
</SCRIPT>

<EMBED SRC="yoursound " autostart=false HIDDEN=TRUE NAME="music" MASTERSOUND>

<a href="#null" onclick="playSound();return false" ><IMG SRC="yourpic.gif"></a>

JeStEr
07-23-2002, 05:11 PM
when i insert this javascript into my html website it causes an error. Do you have any suggestions on why there is an error and how to fix the error?

Mrs G
07-23-2002, 09:31 PM
Because this uses the "EMBED" method if you have not got the path correct to the sound file you will get an error message

JeStEr
07-24-2002, 06:01 AM
i have tried everything to get this to work, but it doesn't seem to work. I have put the whole javascript in the body but that doesn't work and i have put the <script> part in the head of the html and the rest in the body, but that doesn't work either and i have double checked the paths to the sound files, but no good. Any suggestions?

Pooh
07-24-2002, 06:15 AM
It would help if you post the code you're using and what type of error you're receiving.

JeStEr
07-24-2002, 08:02 AM
This is the code from above that i am using:

<SCRIPT LANGUAGE=JavaScript>
function playSound(){
document.music.play();
setTimeout("window.location='yourpage.html'",3000 )
}
</SCRIPT>

<EMBED SRC="yoursound " autostart=false HIDDEN=TRUE NAME="music" MASTERSOUND>

<a href="#null" onclick="playSound();return false" ><IMG SRC="yourpic.gif"></a>

This is the error message i am receiving:

Object doesn't support this property or method.

Is there any specific way i should put this script in or any specific html editor i should use to put it in?

Mrs G
07-24-2002, 07:28 PM
All you have shown is the code as I posted it.

We need to see your entire page so that we can determine what is wrong.

If you are using the code without making the changes I detailed it will not work.

JeStEr
07-25-2002, 06:22 AM
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

<html>
<head>
<title>Untitled</title>
</head>

<body>

<SCRIPT LANGUAGE=JavaScript>
function playSound(){
document.music.play();
setTimeout("window.location='www.google.com'",1000 )
}
</SCRIPT>

<EMBED SRC="C:/Documents and Settings/My Documents/Website/sound.wav" autostart=false HIDDEN=TRUE NAME="music" MASTERSOUND>

<a href="#null" onclick="playSound();return false" ><IMG SRC="C:/Documents and Settings/My Documents/Website/pic.jpg" width="70" height="70"></a>

</body>
</html>

www.google.com is an example of the website i would put in there, the real webpage would be uploaded to the server.

I can't get the following javascript above to work.

Mrs G
07-25-2002, 07:54 PM
....OOPS..........



The reason the script will not work is because you have put in a path to the sound file which is situated on your hard drive.

C:/Documents and Settings/My Documents/Website/sound.wav"


This should be a relative path to the folder containing the sound file on your website.

JeStEr
07-25-2002, 11:04 PM
the javascript works, thanks

now, i have to have several links that play sounds and i put the javascript in the html editor several times but when i open it on the web it gives me an error, but when i take away all except one it works. How would you be able to use this code several times on a webpage without the errors?

Mrs G
07-26-2002, 11:43 PM
Without going into a more complex script the following should be ok.

The script is set up for three sound files, three images, and three link locations.

Add more by copying the relevent lines.

<SCRIPT LANGUAGE=JavaScript>
function playSound(name){
document.embeds[name].play();

if(name=="music1"){
setTimeout("window.location='yourpage1.html'",3000 )
}

if(name=="music2"){
setTimeout("window.location='yourpage2.html'",3000 )
}

if(name=="music3"){
setTimeout("window.location='yourpage3.html'",3000 )
}
}
</SCRIPT>

<EMBED SRC="yoursound1" autostart=false HIDDEN=TRUE NAME="music1" MASTERSOUND>
<EMBED SRC="yoursound2" autostart=false HIDDEN=TRUE NAME="music2" MASTERSOUND>
<EMBED SRC="yoursound3" autostart=false HIDDEN=TRUE NAME="music3" MASTERSOUND>

<a href="#null" onclick="playSound('music1');return false" ><IMG SRC="yourpic1.gif"></a>
<a href="#null" onclick="playSound('music2');return false" ><IMG SRC="yourpic2.gif"></a>
<a href="#null" onclick="playSound('music3');return false" ><IMG SRC="yourpic3.gif"></a>

PS:
REMEMBER TO PUT IN THE CORRECT PATHS.

JeStEr
07-27-2002, 05:44 AM
<body>
<SCRIPT LANGUAGE=JavaScript>
function playSound(name){
document.embeds[name].play();

if(name=="music1"){
setTimeout("window.location='www.google.com'",1000 )
}

if(name=="music2"){
setTimeout("window.location='www.yahoo.com'",1000 )
}

if(name=="music3"){
setTimeout("window.location='www.msn.com'",1000 )
}
}
</SCRIPT>

<EMBED SRC="sound/1.wav" autostart=false HIDDEN=TRUE NAME="music1" MASTERSOUND>
<EMBED SRC="sound/1.wav" autostart=false HIDDEN=TRUE NAME="music2" MASTERSOUND>
<EMBED SRC="sound/2.wav" autostart=false HIDDEN=TRUE NAME="music3" MASTERSOUND>
<a href="#null" onclick="playSound('music1');return false" ><IMG SRC="pictures/pic.jpg"></a>
<a href="#null" onclick="playSound('music2');return false" ><IMG SRC="pictures/pic1.jpg"></a>
<a href="#null" onclick="playSound('music3');return false" ><IMG SRC="pictures/pic2.jpg"></a>
</body>

This is what the code looks like when i fill in the information and it gives me an error. the paths to the files should be correct because i have a folder called sound and pictures and the files are inside them and are uploaded to the server. so i don't know what is wrong.

Mrs G
07-27-2002, 10:25 AM
I have used the code you posted and created a folder called sound containing 3 sound files.

The script works perfectly.


Question:

Is the folder named sound in the same folder as the file containing this script



Note:
You are using the same sound for links 1 & 2.
If you are going to use the same sound for more than one link you only Embed the sound once, then call it with links 1 & 2


<EMBED SRC="sound/1.mid" autostart=false HIDDEN=TRUE NAME="music1" MASTERSOUND>
<EMBED SRC="sound/3.mid" autostart=false HIDDEN=TRUE NAME="music3" MASTERSOUND>
<a href="#null" onclick="playSound('music1');return false" ><IMG SRC="pictures/pic.jpg"></a>
<a href="#null" onclick="playSound('music1');return false" ><IMG SRC="pictures/pic1.jpg"></a>
<a href="#null" onclick="playSound('music3');return false" ><IMG SRC="pictures/pic2.jpg"></a>

JeStEr
07-27-2002, 04:39 PM
<SCRIPT LANGUAGE=JavaScript>
function playSound(name){
document.embeds[name].play();

if(name=="music1"){
setTimeout("window.location='www.google.com'",1000 )
}

if(name=="music1){
setTimeout("window.location='www.yahoo.com'",1000 )
}

if(name=="music1"){
setTimeout("window.location='www.google.com'",1000 )
}

if(name=="music1"){
setTimeout("window.location='www.yahoo.com'",1000 )
}

if(name=="music1"){
setTimeout("window.location='www.google.com'",1000 )
}

if(name=="music1"){
setTimeout("window.location='www.yahoo.com'",1000 )
}

if(name=="music2"){
setTimeout("window.location='www.google.com'",1000 )
}
}
</SCRIPT>

<EMBED SRC="sounds/1.wav" autostart=false HIDDEN=TRUE NAME="music1" MASTERSOUND>
<EMBED SRC="sounds/2.wav" autostart=false HIDDEN=TRUE NAME="music2" MASTERSOUND>

<a href="#null" onclick="playSound('music1');return false" ><IMG SRC="pictures/pic1.jpg"></a>
<a href="#null" onclick="playSound('music1');return false" ><IMG SRC="pictures/pic2.jpg"></a>
<a href="#null" onclick="playSound('music1');return false" ><IMG SRC="pictures/pic3.jpg"></a>
<a href="#null" onclick="playSound('music1');return false" ><IMG SRC="pictures/pic4.jpg"></a>
<a href="#null" onclick="playSound('music1');return false" ><IMG SRC="pictures/pic5.jpg"></a>
<a href="#null" onclick="playSound('music1');return false" ><IMG SRC="pictures/pic6.jpg"></a>
<a href="#null" onclick="playSound('music2');return false" ><IMG SRC="pictures/pic7.jpg"></a>

this is what i want my javascript to look like but it doesn't work maybe i'm putting the files in the wrong folder.

i would like to have 2 sound files in a sounds folder on the server, 7 images that are in a pictures folder on the server that are used as the link and i need 7 different links. is that possible to do with 2 sound files or do i need 7?

what folders should everything be in?

Mrs G
07-27-2002, 10:45 PM
Ok we try again :)

If you make additions to the script without making the necessary modifications the script will never work.

I have taken your page, made the additions, adjustments required, tested it, and zipped it.

I send you azip file containing all the necessary files in their appropriate folders.

Right click on the zip file and select Extract to C:/WINDOW/Desktop/click.
When unzipped there should be a folder on your desktop called "click".

In this folder there are two folders called "pictures" and "sounds".
These folders contain images and sounds.
Also in the folder is an HTML file called "onclick".

Double click on this file to open it and click on any image.

I have set the sound delay to three seconds. Change this to suit your own sound files

If you are not connected to the internet when you click an image you will get the "Page not found" messsage.
This is not an error

The links in the script use an absolute path and you must be connected to the internet.

Below is the page as set out in the HTML file "onclick"

<HTML><HEAD><TITLE>Document Title</TITLE>
<SCRIPT LANGUAGE=JavaScript>
<!--
timer=""
whereto=new Array()
whereto[whereto.length]="http://www.yahoo.com"
whereto[whereto.length]="http://www.google.com"
whereto[whereto.length]="http://www.huntingground.net"
whereto[whereto.length]="http://www.microsoft.com"
whereto[whereto.length]="http://www.freeserve.co.uk"
whereto[whereto.length]="http://www.yahoo.com"
whereto[whereto.length]="http://www.google.com"

function playSound(name,where){
clearTimeout(timer)
document.embeds[name].play();
here=where
timer=setTimeout("document.location=whereto[here]",3000)
}
// -->
</SCRIPT>
</HEAD><BODY>

<EMBED SRC="sounds/1.au" autostart=false HIDDEN=TRUE NAME="music1" MASTERSOUND>
<EMBED SRC="sounds/2.au" autostart=false HIDDEN=TRUE NAME="music2" MASTERSOUND>

<a href="#null" onclick="playSound('music1','0');return false" ><IMG SRC="pictures/devil.gif"></a>
<a href="#null" onclick="playSound('music1','1');return false" ><IMG SRC="pictures/devil.gif"></a>
<a href="#null" onclick="playSound('music1','2');return false" ><IMG SRC="pictures/devil.gif"></a>
<a href="#null" onclick="playSound('music1','3');return false" ><IMG SRC="pictures/devil.gif"></a>
<a href="#null" onclick="playSound('music1','4');return false" ><IMG SRC="pictures/devil.gif"></a>
<a href="#null" onclick="playSound('music1','5');return false" ><IMG SRC="pictures/devil.gif"></a>
<a href="#null" onclick="playSound('music1','6');return false" ><IMG SRC="pictures/devil.gif"></a>

</BODY></HTML>

Mrs G
07-27-2002, 10:47 PM
PS Download the zip file first

JeStEr
07-28-2002, 12:35 AM
ok when i put my own pics and sounds in the javascript and open it in netscape and click a link (image) it doesn't play a sound but it links to the webpage. But when i open it in internet explorer and click a link (image) it gives me a runtime error and asks me if i want to debug it, and it doesn't play the sound and stops the link to the webpage. i don't know what to do. what is an html editing program that is good to use, that might correct this or could you give any other suggestions. I also opened that zip file you posted on the website but it did the same thing.

Mrs G
07-28-2002, 11:31 AM
Did you unzip to your desktop.
Did you open the onclick file.
Did it work on your desktop in Internet Explorer.

JeStEr
07-28-2002, 04:32 PM
i unzipped the folder to my desktop and all 3 files, sounds, pictures and the html onclick where there. i then tried to open the html onclick in netscape and internet but in netscape it wouldn't play the sounds but it linked to the next page and in internet explorer it caused an error. so i don't know what the problem is.

is this script made for netscape and internet explorer?

Mrs G
07-28-2002, 08:00 PM
I have come to the conclusion that you are as your name suggests a jester, and you are just having a bit of fun with me.

The files I sent you worked perfectly in Internet Explorer.

good bye

JeStEr
07-28-2002, 09:53 PM
no i'm serious i can't get the files to work i have tried everything. i swear i'm not messing around or nothin.

i made a sounds folder and it's on my desktop with my two sounds i want to hear when the link is clicked on.

i have a pictures folder with all of my link pictures in it on my desktop.

i have tried everyone of the javascripts you gave me, but with no success. i don't know what's goin on. it worked once and now it doesn't work. i'm getting mad.

does the <script> </script> go in the <head> of the html and the <embed> part go in the body or does the whole script go in the <body> of the html?

JeStEr
07-28-2002, 10:43 PM
ok i went back and tried all of the javascripts again just to check. the problem is with my internet explorer i think because i can get them to work in netscape with the sound. so the scripts do work, i was just using internet explorer all of the time. when i use internet explorer it gives me errors but in netscape it works fine. i will try out the website on another computer just to check about the internet explorer.

thanks for the scripts and your time.

JeStEr
07-28-2002, 10:48 PM
is it possible to combine two of the javascripts you gave me because for one script i can make all of the links one sound with the same amount of time before it links to a different website.

then i would combine another javascript to have a different sound and a different amount of time before the link.

below are the only two scripts i got to work so hopefully i can combine these two, right now when i do this all of the links play the same sounds.

<HTML>
<HEAD>
<TITLE>Document Title</TITLE>
<SCRIPT LANGUAGE=JavaScript>
<!--
timer=""
whereto=new Array()
whereto[whereto.length]="http://www.yahoo.com"
whereto[whereto.length]="http://www.google.com"
whereto[whereto.length]="http://www.huntingground.net"
whereto[whereto.length]="http://www.microsoft.com"
whereto[whereto.length]="http://www.freeserve.co.uk"
whereto[whereto.length]="http://www.yahoo.com"
whereto[whereto.length]="http://www.google.com"

function playSound(name,where){
clearTimeout(timer)
document.embeds[name].play();
here=where
timer=setTimeout("document.location=whereto[here]",3000)
}
// -->
</SCRIPT>
</HEAD>

<BODY>

<EMBED SRC="sounds/1.au" autostart=false HIDDEN=TRUE NAME="music1" MASTERSOUND>
<EMBED SRC="sounds/2.au" autostart=false HIDDEN=TRUE NAME="music2" MASTERSOUND>

<a href="#null" onclick="playSound('music1','0');return false" ><IMG SRC="pictures/devil.gif"></a>
<a href="#null" onclick="playSound('music1','1');return false" ><IMG SRC="pictures/devil.gif"></a>
<a href="#null" onclick="playSound('music1','2');return false" ><IMG SRC="pictures/devil.gif"></a>
<a href="#null" onclick="playSound('music1','3');return false" ><IMG SRC="pictures/devil.gif"></a>
<a href="#null" onclick="playSound('music1','4');return false" ><IMG SRC="pictures/devil.gif"></a>
<a href="#null" onclick="playSound('music1','5');return false" ><IMG SRC="pictures/devil.gif"></a>
<a href="#null" onclick="playSound('music1','6');return false" ><IMG SRC="pictures/devil.gif"></a>

</BODY>
</HTML>

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

<SCRIPT LANGUAGE=JavaScript>
function playSound(){
document.music.play();
setTimeout("window.location='yourpage.html'",3000 )
}
</SCRIPT>

<EMBED SRC="yoursound " autostart=false HIDDEN=TRUE NAME="music" MASTERSOUND>

<a href="#null" onclick="playSound();return false" ><IMG SRC="yourpic.gif"></a>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum