[IronPython] Having problems calling javascript from ironpython in silverlight

Jimmy Schementi Jimmy.Schementi at microsoft.com
Tue Apr 8 15:58:28 CEST 2008


To use "Scriptable" you'd need to mark the method you want to call with an [Scriptable] attribute. Since Python doesn't support attributes, you can't use that method from Python. However, using Invoke will work just fine, since it's simply an eval.

~js


On 4/8/08 5:48 AM, "Vineet Jain (gmail)" <vinjvinj at gmail.com> wrote:

I found an easier way to call javascript from python and it worked the very first time:

With string arg:
       HtmlPage.Window.Invoke("callToJsWithStringSimple2", "string passed from python")

With no string arg:
       HtmlPage.Window.Invoke("callToJsWithStringSimple2")

Is there any reason why you have to use all the scriptable stuff that Michael was using instead of the above?

Vineet


From: Vineet Jain (gmail) [mailto:vinjvinj at gmail.com]
Sent: Monday, April 07, 2008 10:28 PM
To: 'users at lists.ironpython.com'
Subject: Having problems calling javascript from ironpython in silverlight

I can call from javascript to python but cannot call from python to javascript. I get the error on my onload function that
I'm trying to access a property (callToJsWithString) which does not exist. I've included a test template for python and
javascript code. Any help would be appreciated.

Python Code:------------------------>

        import clr
       clr.AddReference("Scriptable, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null")

        from Scriptable import (
           Scriptable, ScriptableForString,
           ScriptableEvent, ScriptableEventArgs
        )

        import sys
        from System.Windows.Browser.HtmlPage import Document
        import System.Windows.Browser.HtmlPage as HtmlPage

        #calling javascript function from python
       callToJsWithString = ScriptableEvent()

        #calling python from javascript
        class CallFromJs(ScriptableForString):
           def _method(self, code):
               Document.value_from_python.innerHTML = code
        callFromJs = CallFromJs()

        class CallFromJs2(ScriptableForString):
           def _method(self, code):
               args = ScriptableEventArgs()
               args.code = "calling javascript from python"
               callToJsWithString.OnEvent(args)
        callFromJs2 = CallFromJs2()

       HtmlPage.RegisterScriptableObject("callFromJs", callFromJs)
       HtmlPage.RegisterScriptableObject("callFromJs2", callFromJs2)
       HtmlPage.RegisterScriptableObject("callToJsWithString", callToJsWithString)


Javascript Code:------------------------>

        function callToJsWithString(s, e) {
           alert(e.code);
        };

      function onload() {
                                               $('#value_from_python').html("after 1");
                                               alert(document.getElementById("SilverlightControl").Content);
           document.getElementById("SilverlightControl").Content.callToJsWithString.Event = callToJsWithString;
                                               $('#value_from_python').val("after 2");
      };
                               var controlID = "SilverlightControl"
                               Silverlight.createObjectEx({
                                   source: "app.xap",
                                   parentElement: document.getElementById("SilverlightControlHost"),
                                   id: controlID,
                                   properties: {
                                                   width:'1',
                                                   height:'1',
                                                   inplaceInstallPrompt: false,
                                                   isWindowless: 'True',
                                                   version:'1.1'
                                   },
        events:{
           onError:null,              // OnError property value -- event-handler function name.
           onLoad:'onload'},           // OnLoad property value -- event-handler function name.
                                   initParams: "start=scan.py, debug=true"
                               });

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/ironpython-users/attachments/20080408/ed4c234d/attachment.html>


More information about the Ironpython-users mailing list