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.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Jun 2010
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Play sound if a certain term exists?

    Hi Everyone

    I don't have a clue what i'm doing with this and don't expect someone to just give me the code if it is actually possible (although it would be nice).

    I've created a site and in the admin area the page will update and tell me if there is a new message from a user, I have this page automatically refreshing every 5 minutes and wondered if it's possible to play a sound file if the term "unread" appears anywhere on the page?

    Thanks for any help you can provide.

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,015
    Thanks
    203
    Thanked 2,538 Times in 2,516 Posts
    Playing a sound cross-browser is not easy. You shuld have a look at http://www.boutell.com/newfaq/creating/scriptsound.html
    It may be simpler just to display a prominent message.

    You can identify whether a word appears in the <body> as follows:-

    Code:
    <html>
    <head>
    
    <script type="text/javascript" >
    var str2find = "unread";  // unmunged in the head
    </script>
    
    </head>
    
    <body>
    
    <p>This page is unread</p>
    
    <script type = "text/javascript">
    
    window.onload = function(){
    var str = document.getElementsByTagName('body')[0].innerHTML;
    //str = str.toLowerCase();  // to make case insensitive
    //str2find=str2find.toLowerCase();  // to make case insensitive
    var a = str.indexOf(str2find);
    if (a >= 0) {
    alert ("The BODY contains the string " + str2find);
    
    }
    else {
    alert ("Sorry - this sequence of characters was not found.");
    }
    }
    </script>
    
    </body>
    </html>

    "A little sincerity is a dangerous thing, and a great deal of it is absolutely fatal". - Oscar Wilde (Irish Poet, Novelist, Dramatist and Critic, 1854-1900)

  • Users who have thanked Philip M for this post:

    martin_75 (06-18-2010)

  • #3
    New to the CF scene
    Join Date
    Jun 2010
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thank you very much Philip, I was able to use your code very easily on my site.

    Playing the sound cross-browser isn't an issue really, I will be the only person that uses the admin area and will most probably always use the same browser.

    I assume this is the area that I will need to change to get a sound to play

    Code:
    if (a >= 0) {
    alert ("The BODY contains the string " + str2find);
    
    }
    else {
    alert ("Sorry - this sequence of characters was not found.");
    }
    I have set up SoundManager as per the guide but I am unsure what I would replace "alert ("The BODY contains the string " + str2find);" with.

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,015
    Thanks
    203
    Thanked 2,538 Times in 2,516 Posts
    Quote Originally Posted by martin_75 View Post
    I have set up SoundManager as per the guide but I am unsure what I would replace "alert ("The BODY contains the string " + str2find);" with.
    Sorry, I don't have any experience of Sound Manager but the command to play the sound should be placed, as you say, in place of
    alert ("The BODY contains the string " + str2find);

    After some experimentation, the following works in IE and FF:-

    Code:
    <html>
    <head>
    
    <script type="text/javascript">
    var str2find = "unread";  // this must be in the <head>
    </script>
    
    </head>
    
    <body>
    
    <script type = "text/javascript">
    function EvalSound(soundobj) {
    var mysound = document.getElementById(soundobj);
    mysound.Play();
    }
    </script>
    
    <input type = "button" value = "Stop Music" onclick = "document.embeds['sound1'].stop()">
    
    <p>This page is unread</p>
    
    <script type = "text/javascript">
    
    window.onload = function(){
    var str = document.getElementsByTagName('body')[0].innerHTML;
    //str = str.toLowerCase();  // to make case insensitive
    //str2find=str2find.toLowerCase();  // to make case insensitive
    var a = str.indexOf(str2find);
    if (a >= 0) {
    //alert ("The BODY contains the string " + str2find);
    setTimeout("EvalSound('sound1')",200);  // delay is essential
    }
    else {
    alert ("Sorry - this sequence of characters was not found.");
    // or, of course, do nothing
    }
    }
    </script>
    
    <embed src="beetfifth.mid" autostart="false" width=0 height=0 loop="true" id="sound1">
    
    </body>
    </html>
    The advantage of a music file with a loop is that it will continue to play until acknowledged.
    Last edited by Philip M; 06-19-2010 at 09:02 AM.


  •  

    Posting Permissions

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