Using .NET Object from JavaScript

Discussion in 'Javascript' started by andrei.csibi@gmail.com, Aug 23, 2006.

  1. Guest

    I've have a .NET Assembly, which is COM Visible. I would like to load
    objects from this assembly in javascript code. E.g. Assembly Book.dll
    has the class Book and I would like to use
    <script language="javascript" type="text/javascript">
    var book = new Book();
    document.write(book.Title);
    </script>
    Even more, if I could access book through the document object it would
    be even more helpful, like:
    <script language="javascript" type="text/javascript">
    var book = document.book;
    document.write(book.Title);
    </script>
    Is there any way to achieve this?
    , Aug 23, 2006
    #1
    1. Advertising

  2. Jeremy Guest

    wrote:
    > I've have a .NET Assembly, which is COM Visible. I would like to load
    > objects from this assembly in javascript code. E.g. Assembly Book.dll
    > has the class Book and I would like to use
    > <script language="javascript" type="text/javascript">
    > var book = new Book();
    > document.write(book.Title);
    > </script>
    > Even more, if I could access book through the document object it would
    > be even more helpful, like:
    > <script language="javascript" type="text/javascript">
    > var book = document.book;
    > document.write(book.Title);
    > </script>
    > Is there any way to achieve this?
    >


    JSON <http://www.json.org> and specifically JSON for .NET
    <http://www.newtonsoft.com/products/json/> (a free library) will enable
    you to pass .NET objects to JavaScript with relatively little effort.

    For example, something like this would probably work (although I've
    never used JSON for .NET)

    <script type="text/javascript">
    var book = eval('(<%=JavaScriptConvert.SerializeObject(MyBook)%>)');
    alert(book.Title); //or whatever else you want
    </script>

    where MyBook is your Book object in ASPX.

    Jeremy
    Jeremy, Aug 23, 2006
    #2
    1. Advertising

  3. Andrei Csibi Guest

    This solution works probably in the context of a server-side dll.
    It's my fault I didn't specify that the assembly is located on the
    client side, it's actually a Browser Helper Object (an Internet
    Explorer extension). So the question is how to bind the javascript code
    to this assembly code just on the client side. It needs to work only on
    Internet Explorer.

    Jeremy wrote:
    > wrote:
    > > I've have a .NET Assembly, which is COM Visible. I would like to load
    > > objects from this assembly in javascript code. E.g. Assembly Book.dll
    > > has the class Book and I would like to use
    > > <script language="javascript" type="text/javascript">
    > > var book = new Book();
    > > document.write(book.Title);
    > > </script>
    > > Even more, if I could access book through the document object it would
    > > be even more helpful, like:
    > > <script language="javascript" type="text/javascript">
    > > var book = document.book;
    > > document.write(book.Title);
    > > </script>
    > > Is there any way to achieve this?
    > >

    >
    > JSON <http://www.json.org> and specifically JSON for .NET
    > <http://www.newtonsoft.com/products/json/> (a free library) will enable
    > you to pass .NET objects to JavaScript with relatively little effort.
    >
    > For example, something like this would probably work (although I've
    > never used JSON for .NET)
    >
    > <script type="text/javascript">
    > var book = eval('(<%=JavaScriptConvert.SerializeObject(MyBook)%>)');
    > alert(book.Title); //or whatever else you want
    > </script>
    >
    > where MyBook is your Book object in ASPX.
    >
    > Jeremy
    Andrei Csibi, Aug 23, 2006
    #3
  4. Jeremy Guest

    Andrei Csibi wrote:
    > This solution works probably in the context of a server-side dll.
    > It's my fault I didn't specify that the assembly is located on the
    > client side, it's actually a Browser Helper Object (an Internet
    > Explorer extension). So the question is how to bind the javascript code
    > to this assembly code just on the client side. It needs to work only on
    > Internet Explorer.
    >


    In that case, I have no idea. Try asking in a Microsoft newsgroup on
    msnews.microsoft.com.

    Jeremy
    Jeremy, Aug 24, 2006
    #4
  5. Andrei Csibi wrote:
    > This solution works probably in the context of a server-side
    > dll. It's my fault I didn't specify that the assembly is
    > located on the client side, it's actually a Browser Helper
    > Object (an Internet Explorer extension). So the question is
    > how to bind the javascript code to this assembly code just
    > on the client side.


    You may find - news:microsoft.public.scripting.jscript - a better place
    to ask this question as this group tends towards an interest in
    cross-browser scripting, for which custom browser extensions are not of
    much value.

    > It needs to work
    > only on Internet Explorer.

    <snip>

    Don't you mean; 'it only needs to work on IE', as it would not be a
    problem if it did happen to work on some other browser as well.

    Richard.
    Richard Cornford, Aug 24, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Girish bharadwaj
    Replies:
    0
    Views:
    686
    Girish bharadwaj
    Sep 15, 2004
  2. jon wayne
    Replies:
    9
    Views:
    696
    Jim Langston
    Sep 22, 2005
  3. Rahul
    Replies:
    2
    Views:
    745
  4. whidbeywave
    Replies:
    3
    Views:
    197
    whidbeywave
    Jan 16, 2006
  5. Replies:
    0
    Views:
    359
Loading...

Share This Page