www.webdeveloper.com
Results 1 to 12 of 12

Thread: accessing variable

  1. #1
    Join Date
    Aug 2010
    Posts
    18

    accessing variable

    as a follow up to my other post here, is it possible to print out the variable from within a function? In this particular case, I am trying to show the 'soundfile' variable I set in the playRandomSoundNoWindow function.
    Thanks in advance,
    wrathkeg

  2. #2
    Join Date
    Aug 2010
    Location
    Baltimore, Maryland, United States
    Posts
    138
    Code:
    <!-- code for random links to play sound files with no new window-->
    <script language="javascript" type="text/javascript">
     function playRandomSoundNoWindow() {
     var myrandom=Math.round(Math.random()*2)
     var links=new Array()
     links[0]="one.mov"
     links[1]="two.mov"
     links[2]="three.mov"
     soundfile=links[myrandom]
     document.getElementById("dummy").innerHTML=
     '<embed id="song" src="soundfile" hidden="true" autostart="true" loop="false" />';
    }
    </script>

    Code:
    function WriteSong()
    {
    
    var x = document.getElementById('song').src;
    var y = document.getElementById('text').innerHTML;
    
    y = "This song is " + x;
    
    }

    with this in the body:

    Code:
    <a href="#" onclick="playRandomSoundNoWindow(); WriteSong();">Click to hear music!</a><br />
    
    <p id="text">No music is playing!</p>
    Last edited by Ofekmeister; 08-23-2010 at 10:12 PM.

  3. #3
    Join Date
    Aug 2010
    Posts
    18
    [lots of silly code deleted. see my next post.]
    Last edited by wrathkeg; 08-24-2010 at 05:25 AM. Reason: html code full of mistakes

  4. #4
    Join Date
    Aug 2010
    Posts
    18
    oops, there were loads of errors in the html code in my previous post which can't be helping... Thanks for your reply, Ofekmeister. So now I have the following, which will play the sound, but still won't write out the filename. any ideas, anyone? like I've said, I'm really new to this so any suggestions appreciated. thanks in advance,
    wrathkeg
    Code:
    <html>
    <script language="javascript" type="text/javascript">
     function playRandomSoundNoWindow() {
      var myrandom=Math.round(Math.random()*2)
      var links=new Array()
      links[0]="one.mov"
      links[1]="two.mov"
      links[2]="three.mov"
      soundfile=links[myrandom]
      document.getElementById("dummy").innerHTML=
      "<embed id=\"song\" src=\""+soundfile+"\" hidden=\"true\" autostart=\"true\" loop=\"false\" />";
      function WriteSong(){
       var x = document.getElementById('song').src;
       var y = document.getElementById('text').innerHTML;
       y = "This song is " + x;
      }
     }
    </script>
    <body>
    <a href="#" onclick="playRandomSoundNoWindow(); WriteSong();">Click to
    hear music!</a>
    <p id="text">No music is playing!</p>
    <span id="dummy"></span>
    </body>
    </html>
    Last edited by wrathkeg; 08-24-2010 at 05:26 AM.

  5. #5
    Join Date
    Apr 2003
    Location
    Netherlands
    Posts
    21,654
    WriteSong is nested and the references are incorrect:
    Code:
    function playRandomSoundNoWindow() {
    var myrandom=Math.round(Math.random()*2)
    var links=new Array()
    links[0]="../../Sample.mov"
    links[1]="../../Sample.mov"
    links[2]="../../Sample.mov"
    soundfile=links[myrandom]
    document.getElementById("dummy").innerHTML=
    "<embed id=\"song\" src=\""+soundfile+"\" hidden=\"true\" autostart=\"true\" loop=\"false\" />";
    }
    
    function WriteSong(){
    var x = document.getElementById('song').src;
    var y = "This song is " + x;
    document.getElementById('text').innerHTML = y;
    }

  6. #6
    Join Date
    Aug 2010
    Posts
    18
    that's great, thanks. I (stupidly) thought the functions would have to be nested to access the variable.
    thanks again.
    wrathkeg

  7. #7
    Join Date
    Aug 2010
    Posts
    18
    okay, so with your (pl.) help, I am getting really close to what I want (which is a listening test for students). this code does almost exactly what I want
    Code:
    <html>
    <script language="javascript" type="text/javascript">
    function playRandomSoundNoWindow() {
    var myrandom=Math.round(Math.random()*2)
    var links=new Array()
    links[0]="one.mov"
    links[1]="two.mov"
    links[2]="three.mov"
    soundfile=links[myrandom]
    document.getElementById("dummy").innerHTML=
    "<embed id=\"sound\" src=\""+soundfile+"\" hidden=\"true\" autostart=\"true\" loop=\"false\" />";
    }
    function WriteSound(){
    var x = document.getElementById('sound').src;
    var y = "That was " + x;
    document.getElementById('text').innerHTML = y;
    }
    </script>
    <body>
    <OL>
    <LI><a href="#" onclick="playRandomSoundNoWindow(); WriteSound();">Click to hear sound</a>
    <LI><a href="#" onclick="playRandomSoundNoWindow(); WriteSound();">Click to hear sound</a>
    <LI><a href="#" onclick="playRandomSoundNoWindow(); WriteSound();">Click to hear sound</a>
    </OL>
    <p id="text">Click the sound to see what it is.</p>
    <span id="dummy"></span>
    </body>
    </html>
    what i am wondering now is whether there is a nifty way to change the message which pops up when clicking on the link so that some text string comes up. i.e. "if the filename is "one.mov" output "you just heard the word 'one'; if the filename is "two.mov" output "you just heard the word 'two' etc "

    I guess it's possible with a series of "if" statements, but I can't figure them out. As you already know, my coding skills are pretty poor....

    Thanks in advance for any suggestions, and thanks for all the help so far.

    wrathkeg

  8. #8
    Join Date
    Apr 2003
    Location
    Netherlands
    Posts
    21,654
    Code:
    var x = document.getElementById('sound').src.replace(/(.+)\/(\w+)\.mov/, "$2");

  9. #9
    Join Date
    Aug 2010
    Posts
    18
    that's fantastic. thanks so much.
    wrathkeg

  10. #10
    Join Date
    Aug 2010
    Posts
    18
    one strange thing I've just noticed: the file extension gets chopped off in my usual browser (firefox on a mac), and on safari, but not Internet Explorer on PC. Anyone got any ideas why? I've googled for a while, but not come up with anything I can use.
    thanks in advance,
    wrathkeg

  11. #11
    Join Date
    Apr 2003
    Location
    Netherlands
    Posts
    21,654
    Code:
    var x = document.getElementById('sound').src.replace(/(.+\/)?(\w+)\.mov/, "$2");

  12. #12
    Join Date
    Aug 2010
    Posts
    18
    that's great, fang. thanks a million.
    wrathkeg

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles