# ScriptableMember and Silverlight

**URL:** <https://www.ruby-forum.com/t/scriptablemember-and-silverlight/145896>\
**Category:** IronRuby\
**Created:** [August 14, 2008, 9:01pm UTC](https://www.ruby-forum.com/t/scriptablemember-and-silverlight/145896 "2008-08-14T21:01:08Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sean\_CSHess](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/94ad74/32.png) [@Sean\_CSHess](https://www.ruby-forum.com/u/Sean_CSHess)\
**Post date:** [August 14, 2008, 9:01pm UTC](https://www.ruby-forum.com/t/scriptablemember-and-silverlight/145896/1 "2008-08-14T21:01:08Z")

</div>

Hello all, I need some way to either access a javascript variable or  
expose  
a function to javascript to be called later. I’ve tried option #2, with  
the  
following call, but I don’t know how to specify a member function as  
scriptable. In C#, you use a meta-flag [ScriptableMember] above the  
function.  
HtmlPage.register\_scriptable\_object(‘mn’, self)

What about the other approach? I know you can access the dom, but I  
need to  
access a plain old variable from javascript.

Thanks!  
~sean

---

<div class="post-metadata">

**Author:** ![Sean\_CSHess](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/94ad74/32.png) [@Sean\_CSHess](https://www.ruby-forum.com/u/Sean_CSHess)\
**Post date:** [August 14, 2008, 9:45pm UTC](https://www.ruby-forum.com/t/scriptablemember-and-silverlight/145896/2 "2008-08-14T21:45:44Z")

</div>

Hey Sean,

Yeah, Iâ€™m well aware of the issue with not being able to use  
[ScriptableMember] for IronRuby. =(

1. Access Javascript variable from Ruby:

If in your HTML you have some variable:

Then in your Ruby script you can access it:

Include System::Windows::Browser  
HtmlPage.window.eval â€œjsVarâ€

1. Access Ruby function from Javascript

This is not directly possible because of not having attribute support.  
Curt might have more input into whether this will be fixed in the  
future.

However, if youâ€™re not opposed to hacky solutions, attach Ruby to a JS  
event (HtmlElement#attach\_event), and when you want data from Ruby fire  
the JS event and Ruby could write the data to the DOM (a hidden div,  
perhaps?). Ugh, that sucks, but itâ€™s what weâ€™ve got today.

Iâ€™d suggest trying the first option, and weâ€™ll keep you posted on making  
Ruby objects scriptable from JS.

~js

On 8/14/08 11:59 AM, “Sean C. Hess” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

Hello all, I need some way to either access a javascript variable or  
expose a function to javascript to be called later. I’ve tried option  
#2, with the following call, but I don’t know how to specify a member  
function as scriptable. In C#, you use a meta-flag [ScriptableMember]  
above the function.

HtmlPage.register\_scriptable\_object(‘mn’, self)

What about the other approach? I know you can access the dom, but I  
need to access a plain old variable from javascript.

Thanks!  
~sean

---

<div class="post-metadata">

**Author:** ![Sean\_CSHess](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/94ad74/32.png) [@Sean\_CSHess](https://www.ruby-forum.com/u/Sean_CSHess)\
**Post date:** [August 14, 2008, 10:00pm UTC](https://www.ruby-forum.com/t/scriptablemember-and-silverlight/145896/3 "2008-08-14T22:00:25Z")

</div>

Sweet… I just figured out how to call a js function from ruby, and in  
fact, that works better for what I want to do anyway.  
Thanks!

On Thu, Aug 14, 2008 at 1:44 PM, Jimmy S. \<

---

<div class="post-metadata">

**Author:** ![Sean\_CSHess](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/94ad74/32.png) [@Sean\_CSHess](https://www.ruby-forum.com/u/Sean_CSHess)\
**Post date:** [August 14, 2008, 10:03pm UTC](https://www.ruby-forum.com/t/scriptablemember-and-silverlight/145896/4 "2008-08-14T22:03:22Z")

</div>

I don’t know about the word “fixed” :). But attribute support is  
definitely one of the .NET interop scenarios on my radar.

From: removed\_email\_address@domain.invalid  
[[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)] On Behalf Of Jimmy  
Schementi  
Sent: Thursday, August 14, 2008 12:45 PM  
To: removed\_email\_address@domain.invalid  
Subject: Re: [Ironruby-core] ScriptableMember and Silverlight

Hey Sean,

Yeah, I’m well aware of the issue with not being able to use  
[ScriptableMember] for IronRuby. =(

1. Access Javascript variable from Ruby:

If in your HTML you have some variable:

Then in your Ruby script you can access it:

Include System::Windows::Browser  
HtmlPage.window.eval “jsVar”

1. Access Ruby function from Javascript

This is not directly possible because of not having attribute support.  
Curt might have more input into whether this will be fixed in the  
future.

However, if you’re not opposed to hacky solutions, attach Ruby to a JS  
event (HtmlElement#attach\_event), and when you want data from Ruby fire  
the JS event and Ruby could write the data to the DOM (a hidden div,  
perhaps?). Ugh, that sucks, but it’s what we’ve got today.

I’d suggest trying the first option, and we’ll keep you posted on making  
Ruby objects scriptable from JS.

~js

On 8/14/08 11:59 AM, “Sean C. Hess” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:  
Hello all, I need some way to either access a javascript variable or  
expose a function to javascript to be called later. I’ve tried option  
#2, with the following call, but I don’t know how to specify a member  
function as scriptable. In C#, you use a meta-flag [ScriptableMember]  
above the function.

HtmlPage.register\_scriptable\_object(‘mn’, self)

What about the other approach? I know you can access the dom, but I  
need to access a plain old variable from javascript.

Thanks!  
~sean
