# Dynamic JS or RJS?

**URL:** <https://www.ruby-forum.com/t/dynamic-js-or-rjs/121665>\
**Category:** Rails\
**Created:** [November 29, 2007, 7:44pm UTC](https://www.ruby-forum.com/t/dynamic-js-or-rjs/121665 "2007-11-29T19:44:41Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![comopasta](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/59ef9b/32.png) [@comopasta](https://www.ruby-forum.com/u/comopasta)\
**Post date:** [November 29, 2007, 7:44pm UTC](https://www.ruby-forum.com/t/dynamic-js-or-rjs/121665/1 "2007-11-29T19:44:41Z")

</div>

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:

The JS:

if(navigator.userAgent.indexOf(“Opera”)!=-1){  
document.write(’

’);  
document.write(’’);  
document.write(’’);  
document.write(’’);  
document.write(’’);  
document.write(’
’);  
}

else if(navigator.userAgent.indexOf(“Firefox”)!=-1){  
document.write(’

’);  
document.write(’’);  
document.write(’’);  
document.write(’’);  
document.write(’\<embed src=“file.mid” type=“application/x-mplayer2”  
width=“184” "height=“45” autostart=“0”\>’);  
document.write(’’);  
document.write(’
’);  
}

else {  
document.write(’

’);  
document.write(’’);  
document.write(’’);  
document.write(’’);  
document.write(’’);  
document.write(’
’);  
}

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!

---

<div class="post-metadata">

**Author:** ![comopasta](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/59ef9b/32.png) [@comopasta](https://www.ruby-forum.com/u/comopasta)\
**Post date:** [November 29, 2007, 7:52pm UTC](https://www.ruby-forum.com/t/dynamic-js-or-rjs/121665/2 "2007-11-29T19:52:28Z")

</div>

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 [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![comopasta](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/59ef9b/32.png) [@comopasta](https://www.ruby-forum.com/u/comopasta)\
**Post date:** [November 29, 2007, 7:59pm UTC](https://www.ruby-forum.com/t/dynamic-js-or-rjs/121665/3 "2007-11-29T19:59:01Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![comopasta](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/59ef9b/32.png) [@comopasta](https://www.ruby-forum.com/u/comopasta)\
**Post date:** [November 29, 2007, 8:38pm UTC](https://www.ruby-forum.com/t/dynamic-js-or-rjs/121665/4 "2007-11-29T20:38:57Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![comopasta](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/59ef9b/32.png) [@comopasta](https://www.ruby-forum.com/u/comopasta)\
**Post date:** [November 30, 2007, 3:37am UTC](https://www.ruby-forum.com/t/dynamic-js-or-rjs/121665/5 "2007-11-30T03:37:23Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![comopasta](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/59ef9b/32.png) [@comopasta](https://www.ruby-forum.com/u/comopasta)\
**Post date:** [November 30, 2007, 9:59am UTC](https://www.ruby-forum.com/t/dynamic-js-or-rjs/121665/6 "2007-11-30T09:59:40Z")

</div>

Anthony R. 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 🕶  
I found this site [http://kennybellew.com/tutorial/](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
