Ruby Forum Ruby on Rails > Dynamic JS or RJS ?

Posted by comopasta Gr (comopasta)
on 29.11.2007 19:44
Hi,

while I search the net for hints I leave this here, probably you will be
faster than me finding an answer.

The next:

I have a static site where I embed a player using an external js

The html:

<script type="text/javascript"
src="http://www.mysite.com/scripts/player.js"></script>

The JS:

if(navigator.userAgent.indexOf("Opera")!=-1){
document.write('<div id="tab" style="position: absolute;
background-color: #000000; left: 635px; top: 85px; height: 10px; width:
184px;" >');
document.write('<object id="MediaPlayer" width="184" height="10"
classid="clsid:22D6F312-B0F6-11D0-94AB-0080C74C7E95" >');
document.write('<param name="AutoStart" value="0" />');
document.write('<param name="FileName" value="file.mid" >');
document.write('</object>');
document.write('</div>');
}

else if(navigator.userAgent.indexOf("Firefox")!=-1){
document.write('<div id="tab" style="position: absolute; left: 635px;
top: 50px; height: 45px; width: 184px; background-color: #000000;" >');
document.write('<object id="MediaPlayer" width="184" height="45"
classid="clsid:22D6F312-B0F6-11D0-94AB-0080C74C7E95">');
document.write('<param name="AutoStart" value="0">');
document.write('<param name="FileName" value="file.mid">');
document.write('<embed src="file.mid" type="application/x-mplayer2"
width="184" "height="45" autostart="0"></embed>');
document.write('</object>');
document.write('</div>');
}

else {
document.write('<div id="tab" style="position: absolute; left: 635px;
top: 50px; height: 25x; width: 184px; background-color: #000000;" >');
document.write('<object id="MediaPlayer" width="184" height="45"
classid="clsid:22D6F312-B0F6-11D0-94AB-0080C74C7E95" >');
document.write('<param name="AutoStart" value="0" />');
document.write('<param name="FileName" value="file.mid" >');
document.write('</object>');
document.write('</div>');
}

So when the page loads it uses that js to embed the player.

My question:

- I want to have a template js (or rjs?) where I can modify the file to
be played dynamically. What do you think is the best approach with Ruby
on Rails/AJAX?

- As you can see I take care of browsing checking, maybe there is some
ready made functions in AJAX for this, or even for player embedding (?)

Thanks in advance!
Posted by Thorsten (Guest)
on 29.11.2007 19:52
(Received via mailing list)
you should wrap that code in a function that you can pass the filename
and call the function the layout/view, maybe in the header:

<%= javascript_tag
"my_player_function('#{@my_ruby_variable_with_filename_for_player}')"
%>

then you just have to set @my_ruby_variable_with_filename_for_player
somwhere in you controller, like the current action or a before_filter
(don't know what you need, depends on your goal here)

On 29 Nov., 19:44, comopasta Gr <rails-mailing-l...@andreas-s.net>
Posted by comopasta Gr (comopasta)
on 29.11.2007 19:59
Thanks Thorsten! I will try that very soon.

Uuff, I didn't even have time to open my browser to continue searching 
the net. That was indeed quick!

Cheers!
Posted by comopasta Gr (comopasta)
on 29.11.2007 20:38
Excellent!! It worked just beautifully.

Btw the embedding I made works fine on Windows with Opera, Explorer, 
Netscape and Firefox. I also add a direct link to download the audio 
file for the Linux users since I can't test on Linux.

Is there a quick hint to get the same script working on Linux? Not a 
critical thing though.

Thanks again!
Posted by Anthony Richardson (Guest)
on 30.11.2007 03:37
(Received via mailing list)
you could test on linux by:
A) booting into a live cd (heaps available on the net).
B) running linux in a VM (just download a prebuilt one for your VM 
software
of choice).
C) use the live CD in an existing VM if you have one (save building 
one).

If you don't have an VM software installed I can recommend VirtualBox, 
it's
free and available for Windows, Mac & Linux hosts.

If the "MediaPlayer" referred to in your script is the Windows Media 
Player
then it will only work in Windows however and not on Linux or Mac. You 
would
want to investigate using a Flash based player for the most 
compatibility.

Cheers,

Anthony
Posted by comopasta Gr (comopasta)
on 30.11.2007 09:59
Anthony Richardson wrote:
> You would want to investigate using a Flash based player for the most 
> compatibility.

Yeah flash as always been an option. And a new topic to learn 8-)
I found this site http://kennybellew.com/tutorial/ with very good 
information. Probably when I finish the current basic tasks I can jump 
into that. It is a very basic player I need, but I'm not sure yet if 
flash can play midi...

Thanks