Using .NET Object from JavaScript

A

andrei.csibi

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?
 
J

Jeremy

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
 
A

Andrei Csibi

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.
 
J

Jeremy

Andrei said:
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
 
R

Richard Cornford

Andrei said:
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 - - 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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top