Page 1 of 1

Embed javascript in C# code

Posted: Fri Jan 23, 2015 1:25 pm
by justinl
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!

Re: Embed javascript in C# code

Posted: Mon Jan 26, 2015 2:29 am
by JS-Support @Userware
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

Re: Embed javascript in C# code

Posted: Mon Jan 26, 2015 10:33 am
by justinl
Worked great, thanks!

Re: Embed javascript in C# code

Posted: Mon Jan 26, 2015 11:11 am
by JS-Support @Userware
You are welcome :-)

JS-Support

Re: Embed javascript in C# code

Posted: Tue Mar 31, 2015 4:06 am
by JS-Support @Userware
In Beta 3 and above, the step 2 is no longer needed. You no longer need to modify the CSPROJ file.

Re: Embed javascript in C# code

Posted: Mon Dec 26, 2016 4:49 am
by njs123
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

Re: Embed javascript in C# code

Posted: Mon Dec 26, 2016 6:41 am
by JS-Support @Userware
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

Re: Embed javascript in C# code

Posted: Mon Dec 26, 2016 11:42 pm
by njs123
Thank you.

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