Enjoy an ad free experience by logging in. Not a member yet?
Register .
02-13-2013, 04:09 AM
PM User |
#1
Regular Coder
Join Date: Dec 2009
Posts: 740
Thanks: 149
Thanked 67 Times in 67 Posts
how to stop page jump, but still play sound?
Hi all
I'm trying to play a single short sound --- it works --- but the page jumps to bottom (i'm using chrome).
How can I stop the page jump?
Doing this stops the page jump, but also stops the sound!
Code:
if(e.preventDefault){
e.preventDefault();
} else {e.returnValue = false;
}
My html
Code:
<div id="audioPlay" onclick="playSound();">
function playSound() {
document.getElementById("audioPlay").innerHTML='<embed src=\"linkto_soundfile\" hidden=\"true\" />';
}
any ideas?
LT
__________________
Ask not what can I do for myself, but what can I do for others
"The greatest revenge is to accomplish what others say you cannot do."
~ Unknown
02-13-2013, 04:27 AM
PM User |
#2
Supreme Master coder!
Join Date: Feb 2009
Posts: 23,184
Thanks: 59
Thanked 3,995 Times in 3,964 Posts
Just move the focus to where you want it in the playSound function!
Example:
Code:
<a name="backHere"></a>
<div id="audioPlay" onclick="playSound();">...</div>
...
function playSound() {
document.getElementById("audioPlay").innerHTML=
'<embed src=\"linkto_soundfile\" hidden=\"true\" />';
location.href = "#backHere";
}
Other ways possible, but that looks like it would do what you want.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
02-13-2013, 04:32 AM
PM User |
#3
Supreme Master coder!
Join Date: Feb 2009
Posts: 23,184
Thanks: 59
Thanked 3,995 Times in 3,964 Posts
By the by, a sneaky way I do this:
Code:
<a href="someFile.wav" target="hiddenFrame"> Play sound </a>
<iframe name="hiddenFrame" style="width: 100%; height: 100ps; display: none;"></iframe>
Or you can pass in the file name and parse the query string:
Code:
<a href="playSound.html?file=someFile.wav" target="hiddenFrame"> Play sound </a>
<iframe name="hiddenFrame" style="width: 100%; height: 100ps; display: none;"></iframe>
And then your "playSound.html" (or it could be ".php" or ".asp" page! which might have advantages) looks at the query string and figures out what file to load and play.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Users who have thanked Old Pedant for this post:
02-13-2013, 05:08 AM
PM User |
#4
Regular Coder
Join Date: Dec 2009
Posts: 740
Thanks: 149
Thanked 67 Times in 67 Posts
Hi OP
I've tried method one in several ways and none of them worked.
The page just jumps to bottom. Even though the url changes it doesn't go to the anchor.
method one:
Code:
<a name="backHere"></a>
<div id="audioPlay" onclick="playSound();">...</div>
...
function playSound() {
document.getElementById("audioPlay").innerHTML=
'<embed src=\"linkto_soundfile\" hidden=\"true\" />';
location.href = "#backHere";
}
This embed gets the file from google --- I send word, it sends back sound, so I don't have an actual sound file.
Could I use the embed with the iFrame?
LT
__________________
Ask not what can I do for myself, but what can I do for others
"The greatest revenge is to accomplish what others say you cannot do."
~ Unknown
Last edited by low tech; 02-13-2013 at 05:11 AM ..
02-13-2013, 05:14 AM
PM User |
#5
Supreme Master coder!
Join Date: Feb 2009
Posts: 23,184
Thanks: 59
Thanked 3,995 Times in 3,964 Posts
Sure, I use <embed> with the IFrame, in fact.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
02-13-2013, 05:40 AM
PM User |
#6
Regular Coder
Join Date: Dec 2009
Posts: 740
Thanks: 149
Thanked 67 Times in 67 Posts
Hi OP
After some trial and lots of error I finally got it using your iframe method.
Thanks
LT
__________________
Ask not what can I do for myself, but what can I do for others
"The greatest revenge is to accomplish what others say you cannot do."
~ Unknown
Last edited by low tech; 02-13-2013 at 08:03 AM ..
02-13-2013, 10:21 AM
PM User |
#7
The fat guy next door
Join Date: Jan 2006
Location: Halle (Saale), Germany
Posts: 7,592
Thanks: 5
Thanked 865 Times in 842 Posts
Just to be a little pedantic, Old Pedant: the name attribute is not allowed on any element other than form controls. Named anchors are way outdated and I would be happy if you wouldn’t promote this practice so that newbies don’t get off on the wrong foot. Instead of name, id should be used.
The embed element is a different matter that would require a deeper discussion.
__________________
Don’t click this link !
02-13-2013, 08:06 PM
PM User |
#8
Supreme Master coder!
Join Date: Feb 2009
Posts: 23,184
Thanks: 59
Thanked 3,995 Times in 3,964 Posts
Hmmm...If I use an ID for the <iframe> then can
target= in the <a> tag "find" it? I've never tried that.
Actually, in my own code, I do use an ID for the <iframe> and then do
Code:
document.getElementById("hiddenFrame").src =
"playitAgainSam.asp?file=" + encodeURIComponent(someFileName);
I was trying to simplify that for use with the <a> tag.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Jump To Top of Thread
Thread Tools
Rate This Thread
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
All times are GMT +1. The time now is 02:03 AM .
Advertisement
Log in to turn off these ads.