Embed javascript in C# code

justinl
Posts: 4
Joined: Fri Jan 23, 2015 1:17 pm

Embed javascript in C# code

Postby justinl » Fri Jan 23, 2015 1:25 pm

Is there a way to embed javascript code in your C# code in such a way so that it just gets passed onto the output when it's converted from C# to javascript?

In my particular example I want to add a "window.location.href = 'http://site.com'" at a particular location in my C# functions so that is gets ignored by the C# compiler but gets passed into the converted Javascript. I could edit the resulting Javascript after it's converted however but that can become cumbersome. Maybe some type of code annotations?

Thanks!

JS-Support @Userware
Site Admin
Posts: 1142
Joined: Tue Apr 08, 2014 3:42 pm

Re: Embed javascript in C# code

Postby JS-Support @Userware » Mon Jan 26, 2015 2:29 am

Hi justinl,

Welcome to the forums.

Yes, this will be super easy to achieve with the next build (Beta 3) coming soon.

In the meantime, with the Beta 2, please follow these steps:


UPDATE: It's now much easier: follow this link for documentation: http://cshtml5.com/links/how-to-call-javascript.aspx


1) Add a reference to the following file:
C:\Program Files (x86)\MSBuild\CSharpXamlForHtml5\InternalStuff\Compiler\JSIL.Meta.dll
(Note: the "Program Files" path may be slightly different on x86 systems)


2) Add the following line to your .CSPROJ file:
<CSharpXamlForHtml5AllowAnyReferences>True</CSharpXamlForHtml5AllowAnyReferences>
(Note: you can add it anywhere, for example in the first PropertyGroup)


3) Use the following C# code to inject JS code into your C# code:

Code: Select all

JSIL.Verbatim.Expression("window.location.href = 'http://www.userware-solutions.com'");



Enjoy :-)

Note: this will be part of a separate business pack that will provide lots of cool advanced features like this.

Best regards,

JS-Support

justinl
Posts: 4
Joined: Fri Jan 23, 2015 1:17 pm

Re: Embed javascript in C# code

Postby justinl » Mon Jan 26, 2015 10:33 am

Worked great, thanks!

JS-Support @Userware
Site Admin
Posts: 1142
Joined: Tue Apr 08, 2014 3:42 pm

Re: Embed javascript in C# code

Postby JS-Support @Userware » Mon Jan 26, 2015 11:11 am

You are welcome :-)

JS-Support

JS-Support @Userware
Site Admin
Posts: 1142
Joined: Tue Apr 08, 2014 3:42 pm

Re: Embed javascript in C# code

Postby JS-Support @Userware » Tue Mar 31, 2015 4:06 am

In Beta 3 and above, the step 2 is no longer needed. You no longer need to modify the CSPROJ file.

njs123
Posts: 25
Joined: Sun Dec 25, 2016 11:10 pm

Re: Embed javascript in C# code

Postby njs123 » Mon Dec 26, 2016 4:49 am

How do I access javascript from a js file in cshtml5 class library?

e.g. I have controls in class library
Test.Lib/Controls/TestControl/test.js

JS-Support @Userware
Site Admin
Posts: 1142
Joined: Tue Apr 08, 2014 3:42 pm

Re: Embed javascript in C# code

Postby JS-Support @Userware » Mon Dec 26, 2016 6:41 am

njs123 wrote:How do I access javascript from a js file in cshtml5 class library?

e.g. I have controls in class library
Test.Lib/Controls/TestControl/test.js


Hi,

You can do that by calling the method "Interop.LoadJavaScriptFile(...)". You will find the documentation at:
http://forums.cshtml5.com/posting.php?mode=quote&f=2&p=8888

Regards,
JS-Support

njs123
Posts: 25
Joined: Sun Dec 25, 2016 11:10 pm

Re: Embed javascript in C# code

Postby njs123 » Mon Dec 26, 2016 11:42 pm

Thank you.

I have one more question,
How do I access contents of the file
e.g.
Test.Lib/Controls/TestControl/test.txt


Return to “General Discussion and Other”

Who is online

Users browsing this forum: No registered users and 22 guests

 

 

cron