Page 1 of 1

using jQuery and what is the syntax?

Posted: Mon Oct 10, 2016 1:56 am
by Sesztak
Dear JS-Support,

Is it possible to use jQuery with CSHTML ? What is the basic syntax to pass & get objects between CSHTML5 and jQuery ?

Waiting for your kind reply,
Br,
Péter

Re: using jQuery and what is the syntax?

Posted: Mon Oct 10, 2016 2:39 am
by JS-Support @Userware
Hi,

To use jQuery in a CSHTML5 project, follow these steps:

1) Copy the jQuery library file (.js) into your CSHTML5 project

2) When the application starts (for example in the "Loaded" event of "MainPage.xaml.cs"), call "await CSHTML5.Interop.LoadJavaScriptFile(...)" and pass as parameter the URI of the jQuery library file (.js). You can use either the "ms-appx:///AssemblyName/FolderName/FileName.js" syntax, or the Silverlight-style "component;" syntax.

3) Now jQuery is available to all your JavaScript calls. This means that you can use it in your calls to "CSHTML5.Interop.ExecuteJavaScript" method. For people who are new to Interop calls, here is the reference:
http://cshtml5.com/links/how-to-call-javascript.aspx
Please note that inside the "ExecuteJavaScript" method, the "$" syntax may collide with the "$0", "$1", "$2", etc. which is used to pass objects from the C# world to the JS world (see reference link above). Therefore, you may want to replace "$" with "jQuery". To do so, simply call "jQuery.noConflict()" at the beginning. You can learn more here:
https://api.jquery.com/jquery.noconflict/

Regards,
JS-Support

Re: using jQuery and what is the syntax?

Posted: Mon Oct 10, 2016 2:45 am
by Sesztak
Great Thanks !
br,
Péter