Forum Moderators: open

Message Too Old, No Replies

href/onclick vs flash/no flash

         

Martini

7:13 pm on Aug 16, 2008 (gmt 0)

10+ Year Member



Q: Is it possible to use both href and onclick, so that one works only for flash browsers and the other for non flash?

this is what I have right now:

<a href="#" onclick="flashplayer_loadAndPlay('my-mp3-file.mp3');">

My links load mp3 files into a flash player. I want to make them work also for no-flash users (like iPhone). I tried to put a link to an mp3 file into href, but this breaks the whole functionality for flash browsers (it just opens a link).

Fotiman

1:19 am on Aug 17, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Welcome to WebmasterWorld!
You could use one of the libraries that detects whether or not the user has Flash (swfobject, for example).

Your link would look like this:

<a href="my-mp3-file.mp3" onclick="return flashplayer_loadAndPlay('my-mp3-file.mp3');">

Then change your flashplayer_loadAndPlay function to do something like this:


function flashplayer_loadAndPlay(fileToPlay) {
if (!swfobject.hasFlashPlayerVersion("6.0.0"))
return true;
}
// Do your thing to play file in flash here...
return false;
}

Obviously, you'd need to include the swfobject code [code.google.com] for this example to work. What this does is if it doesn't detect Flash (my example checks for version 6.0.0 or greater), then the function will return true, which will cause the link to follow the href value. Otherwise, it will play the file in the Flash player and return false, causing the href value to be ignored.

Hope that helps.

Martini

6:27 am on Aug 18, 2008 (gmt 0)

10+ Year Member



I did as you say, but if I have a link in href, the browser tries to open it anyway.
What I need is a simple link to be used ONLY in case flash is unsupported.

Fotiman

1:08 pm on Aug 18, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Look closely at my example:

onclick="return flashplayer_loadAndPlay('my-mp3-file.mp3');">

This is important. If you return false in your onclick handler, the browser will not try to open the href link. My example, as I posted it, will do exactly what you want.

Martini

4:46 pm on Aug 18, 2008 (gmt 0)

10+ Year Member



I got it now! I had a problem with missing an opening brace of the IF statement and outdated swfobject library. Thanks for helping me Fotiman, really appreciate that :)