Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 24

Thread: OnClick Script

  1. #1
    New Coder
    Join Date
    Jul 2002
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts

    OnClick Script

    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?

  • #2
    New Coder
    Join Date
    Jul 2002
    Posts
    56
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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>

  • #3
    New Coder
    Join Date
    Jul 2002
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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?

  • #4
    New Coder
    Join Date
    Jul 2002
    Posts
    56
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Because this uses the "EMBED" method if you have not got the path correct to the sound file you will get an error message

  • #5
    New Coder
    Join Date
    Jul 2002
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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?

  • #6
    New Coder
    Join Date
    Jul 2002
    Posts
    47
    Thanks
    0
    Thanked 0 Times in 0 Posts
    It would help if you post the code you're using and what type of error you're receiving.

  • #7
    New Coder
    Join Date
    Jul 2002
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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?

  • #8
    New Coder
    Join Date
    Jul 2002
    Posts
    56
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #9
    New Coder
    Join Date
    Jul 2002
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts
    <!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.

  • #10
    New Coder
    Join Date
    Jul 2002
    Posts
    56
    Thanks
    0
    Thanked 0 Times in 0 Posts
    ....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.

  • #11
    New Coder
    Join Date
    Jul 2002
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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?

  • #12
    New Coder
    Join Date
    Jul 2002
    Posts
    56
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #13
    New Coder
    Join Date
    Jul 2002
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts
    <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.

  • #14
    New Coder
    Join Date
    Jul 2002
    Posts
    56
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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>

  • #15
    New Coder
    Join Date
    Jul 2002
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts
    <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?


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •