Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-13-2013, 04:09 AM   PM User | #1
low tech
Regular Coder

 
low tech's Avatar
 
Join Date: Dec 2009
Posts: 740
Thanks: 149
Thanked 67 Times in 67 Posts
low tech is on a distinguished road
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
low tech is offline   Reply With Quote
Old 02-13-2013, 04:27 AM   PM User | #2
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,172
Thanks: 59
Thanked 3,994 Times in 3,963 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is offline   Reply With Quote
Old 02-13-2013, 04:32 AM   PM User | #3
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,172
Thanks: 59
Thanked 3,994 Times in 3,963 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is offline   Reply With Quote
Users who have thanked Old Pedant for this post:
low tech (02-13-2013)
Old 02-13-2013, 05:08 AM   PM User | #4
low tech
Regular Coder

 
low tech's Avatar
 
Join Date: Dec 2009
Posts: 740
Thanks: 149
Thanked 67 Times in 67 Posts
low tech is on a distinguished road
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..
low tech is offline   Reply With Quote
Old 02-13-2013, 05:14 AM   PM User | #5
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,172
Thanks: 59
Thanked 3,994 Times in 3,963 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is offline   Reply With Quote
Old 02-13-2013, 05:40 AM   PM User | #6
low tech
Regular Coder

 
low tech's Avatar
 
Join Date: Dec 2009
Posts: 740
Thanks: 149
Thanked 67 Times in 67 Posts
low tech is on a distinguished road
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..
low tech is offline   Reply With Quote
Old 02-13-2013, 10:21 AM   PM User | #7
VIPStephan
The fat guy next door


 
VIPStephan's Avatar
 
Join Date: Jan 2006
Location: Halle (Saale), Germany
Posts: 7,587
Thanks: 5
Thanked 864 Times in 841 Posts
VIPStephan is a jewel in the roughVIPStephan is a jewel in the roughVIPStephan is a jewel in the rough
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!
VIPStephan is offline   Reply With Quote
Old 02-13-2013, 08:06 PM   PM User | #8
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,172
Thanks: 59
Thanked 3,994 Times in 3,963 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 01:22 PM.


Advertisement
Log in to turn off these ads.