/    Sign up×
Community /Pin to ProfileBookmark

accessing variable

as a follow up to my other post [URL=”http://www.webdeveloper.com/forum/showpost.php?p=1110261&postcount=2″]here[/URL], 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

to post a comment
JavaScript

11 Comments(s)

Copy linkTweet thisAlerts:
@OfekmeisterAug 24.2010 — [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]



[CODE]
function WriteSong()
{

var x = document.getElementById('song').src;
var y = document.getElementById('text').innerHTML;

y = "This song is " + x;

}
[/CODE]



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>
[/CODE]
Copy linkTweet thisAlerts:
@wrathkegauthorAug 24.2010 — [lots of silly code deleted. see my next post.]
Copy linkTweet thisAlerts:
@wrathkegauthorAug 24.2010 — oops, there were loads of errors in the html code in my previous post :eek: 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
<i>
</i>&lt;html&gt;
&lt;script language="javascript" type="text/javascript"&gt;
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=
"&lt;embed id="song" src=""+soundfile+"" hidden="true" autostart="true" loop="false" /&gt;";
function WriteSong(){
var x = document.getElementById('song').src;
var y = document.getElementById('text').innerHTML;
y = "This song is " + x;
}
}
&lt;/script&gt;
&lt;body&gt;
&lt;a href="#" onclick="playRandomSoundNoWindow(); WriteSong();"&gt;Click to
hear music!&lt;/a&gt;
&lt;p id="text"&gt;No music is playing!&lt;/p&gt;
&lt;span id="dummy"&gt;&lt;/span&gt;
&lt;/body&gt;
&lt;/html&gt;
Copy linkTweet thisAlerts:
@FangAug 24.2010 — 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;
}[/CODE]
Copy linkTweet thisAlerts:
@wrathkegauthorAug 24.2010 — that's great, thanks. I (stupidly) thought the functions would have to be nested to access the variable.

thanks again.

wrathkeg
Copy linkTweet thisAlerts:
@wrathkegauthorAug 24.2010 — 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>[/CODE]

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
Copy linkTweet thisAlerts:
@FangAug 24.2010 — [CODE]var x = document.getElementById('sound').src.replace(/(.+)/(w+).mov/, "$2");[/CODE]
Copy linkTweet thisAlerts:
@wrathkegauthorAug 24.2010 — that's fantastic. thanks so much.

wrathkeg
Copy linkTweet thisAlerts:
@wrathkegauthorAug 24.2010 — 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
Copy linkTweet thisAlerts:
@FangAug 24.2010 — [CODE]var x = document.getElementById('sound').src.replace(/(.+/)?(w+).mov/, "$2");[/CODE]
Copy linkTweet thisAlerts:
@wrathkegauthorAug 24.2010 — that's great, fang. thanks a million.

wrathkeg
×

Success!

Help @wrathkeg spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.24,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...