So I'm working on a WPF app that has a web page self hosting, and in my Javascript I need to use some data from my C#. I'm wondering if there's any way (in WPF/C#) to invoke a Javascript function WITH a parameter, or if the best way to accomplish this would just be to dump the parameters to a text file and use the Javascript to read it in again, which I'd strongly prefer not to do.
Thanks!
Thanks for responding, but the class I'm using is actually the ChromiumWebBrowser, part of CefSharp. I didn't think to google that and i actually found this How to pass inputs to javascript functions with cefsharp
Thanks!
Related
Put it simple I want to make small currency exchange app (pet project- I want free API( 1000 requests per month including more currency is a perfect option)). I dont like the free APIs I have found so far but I have found this website https://bg.coinmill.com/ and I wanna use it for my purpose. Reading an answer to similar question:
The only way to make use of JS in Flutter is using WebView.
Dart compiles to JS only for browser applications, for Flutter it compiles >to native machine code.
convert js code direcly to dart, using package js
package JS doesn't convert JS, it just creates proxies for JS functions to >be able to call them from Dart, but that is also only supported in Dart web >applications.
Put it simple it isn't possible without hitting some compilation errors and some workarounds. However https://github.com/pichillilorenzo/flutter_inappbrowser looks promissing. Embedding the webpage that will look ugly and I won't have any control over ui/settings. My options now are looking for another free currency API or trying to find a workaround. I incline for another API, but not sure which one. Any suggestions ?
So basically what you actually want to do is use that website to do the currency conversion in the background (enter value, press "Convert"), then display the result in your Flutter app? You don't need javascript for that.
After entering pressing the submit button, the site simply redirects you to a different page (GET request) with an URL like this:
https://bg.coinmill.com/CAD_USD.html?CAD=22
Use dart's http library to perform the same request with the right currency/value parameters. The result of the request contains the source code of the web page.
Instead of displaying the web page, you just need to read the value you need from the source code of the web page:
<div id="currencyBox1">
<input class="currencyField" ... value="16.46" ...>
САЩ долар (USD)
</div>
So, how I understand your question, you have some js library, and you want to use it from Dart?
If question so, yes, you can do it using Dart JS Intertop. The more information in the link.
Edit
Yes, you are right, you can call js from Flutter only using evalJavascript function from flutter_webview_plugin.
You can use Firebase Cloud Functions and wrap your function in a callable function. You'll have all node js environment and Dart code will only call a function.
The question is as following: when I write JavaScript inside my JSP page, using JSTL function, it renders normally, understanding everything I want from it. But to make my code clear, I want to move that JavaScript from tag in JSP to a separate file. But when I try to call same function from the file, it doesn't work, but just appends to my page as a simple text.
Here is code example to make this more understandable.
...other JSP stuff
<script>
$.each(data, function(index, item) {
$('#holder').append(
'<tr>' +
'<td>item.price + ' <fmt:message key="currency.default"/></td>'
'</tr>'
);
});
</script>
This works perfect for me. The actual message from the resource bundle is pulled and set instead of the fmt:message function.
But when I move the same code to a separate file, all this doesn't transform and stays plain text.
I understand, that JSP serves on the server, and all transformations with those functions is done much earlier than actual javascript is loaded.
But maybe somebody knows a certain hack to make this work?
Thanks in advance.
You can use DWR for that cause. An old framework but still holds good if that is what exactly you are looking for in your question.
http://directwebremoting.org/dwr/index.html
DWR is a Java library that enables Java on the server and JavaScript in a browser to interact and call each other as simply as possible.
Running java methods or jstl functions(also jstl functions are java methods) from JavaScript is impossible. Because java methods run on server-side but javascript on client-side.
If you want to run java methods in client-side anyway you must create java applet for this. You can run java methods with JavaScript inside your applet. For detailed information see this Java Applet Tutorial
I hope this will help you
In the admin web application site, Any way to make me use the same namespace javascript object in different page?for example, the first page is login page,if I create the javascript object, like below:
var App = {
username:"Tom",
password:"1234",
initLoginUI:function(){
......
},
initPageUI:function(){
......
}
}
After I login, I direct to index.html page, I want to call App.initPageUI() in index.html, is it impossible? How should I do that, could javascript expert give me some suggestion? Many Thanks!
Update:
I find one way is very close what I mean:
http://dotnetslackers.com/Community/blogs/bmains/archive/2009/05/30/ajax-and-type-registernamespace-how-it-works.aspx
It is Asp.Net Ajax, but if I don't know Asp.net, I only want to use javascript, maybe..... node.js??? But how to use node.js in my case?I want to some example.....I don't know too much about node.js...but I want to learn!!Or any popular(fancy) framework I can use?
Please give me any good suggestion, Many Thanks:)
How do I add a script such as the jQuery library so it is available for use in GWT? I know how to do JSNI, I just don't know how to make the jQuery library available to me so I can call it within JSNI.
GWT can call any JS function or reference any JS variable on the page using JSNI, so as long as the JQuery script is also loaded on the page, you can call any JQuery code you want.
For example:
public native void callJQuery() /*-{
$("p.neat").addClass("ohmy").show("slow");
}-*/;
They don't play well together, yet. The only way I know so far (correct me if I'm mistaken) is GWTQuery. Give it a try ;)
EDIT
I re-explained my thoughts, hope it'll be more clear now
It is possible to call any desired custom JavaScript code, see Jason's answer.
I recommend using GWTQuery, since it's successfull port of JQuery to Java-based GWT ecosystem and doesn't force you to use raw native JavaScript from Java code.
I have input HTML button(Not ASP.NET Button) click event in c#
Is there any way i can call c# function using javascript
Thank you very much....
Probably the easiest and most straightforward way will be to create an HTTPHandler on your ASP .NET site and then your JavaScript can make an AJAX call (easily done with jQuery, note the three links to three different methods, and there are likely more) to invoke that handler at its configured URL.
Additionally, does this method need to return anything? From the jQuery side your best bet will be to receive back a JSON object. This can be done pretty easily from the .NET side with the JavaScriptSerializer or even manually.
You need to use the MS Ajax JS libs. These will wire up things like that for you... but it has to be an ASP.NET button. Sounds like you want to make an AJAX Service call. Give us some more details as to how this is setup and how you want it to work. ASP.NET Forms is not very forgiving to simple buttons and JS... you need to think about viewstate.