get javascript variable into webbrowser control winforms - javascript

I'm using a webbrowser control in visual studio 2010 to invoke JS script.I'm able to call a function from the web browser but i'm looking to get a varible value from JS and use it in the winform.
I have this JS code for exemple :
<script type="text/javascript">
function f() {
var val=0;
return val;
}
</script>
The C# code is not working :
webBrowser.Document.InvokeScript("f");
what's the right way to invoke the JS function and get the variable value?

InvokeScript will return the value which the javascript function returns. You just need to be a bit careful about it's type. Numbers and strings will be returned a c# strings.

Related

How to get the result of eval when I use Javascript in PHP?

I actually work on a tool named jedox. With this tool I can make macro(like excel) but in PHP. Jedox provide some example of macro and in one of these example there is this code:
function test()
{
return array(array('eval', "(function(){
console.log('salut')
}())"));
}
It's a PHP code that run JS code. But the problem is I don't know how this code work, the only thing I know about it is that it execute JS code but can't return anything, so if you put return in the code it will not return any value. I have no way to retrieve the value returned.
So my question is how should I supposed to retrieve a value from this ?
PS: if I try to investigate about the value returned by test() I get an array nested with another array with those 2 values 'eval' and the function.
PS2: Apparently you can't run this code correctly with traditional tool. So to help me you should have jedox, I guess :/ ...
On the client side, someone must be getting those two strings and executing them. The PHP code ("host side") is not actually doing that.
You may could put the Javascript code into a file. Then execute the file using NodeJS and get the value.
For example:
function test() {
file_put_contents('test.js', <<< TEXT
(function(){
console.log('salut')
}())
TEXT);
return shell_exec('node test.js');
}
echo test(); // Return: sault
Also notice that in most shared hosts and servers shell_exec function is disabled by default. You can enable it through you php.ini.

pass data from javascript to asp.net core 2 razor page method

Is there any to pass some data from HTML property on changed event data to asp.net core razor pages?
I want to get an ID from dropdown list from HTML using JS and pass it to Razor Pages (asp.net core 2) and get the result from the custom method ?
Code I want to be look like below if possible :)
JS code
$('#Neighborhood_DistrictId').on('change', function () {
#Model.GetDistrictName($('#Neighborhood_DistrictId').val());
});
On the Razor page
public string GetDistrictName(Guid id)
{
return httpSystemApi.GetByIdAsync<District>("Districts", id).Result.Name;
}
GetDistrictName method is connecting to API and returning the value. I don't want to direct connect to API with JS if there is a way to do what I want
I am playing around with Razor Pages, and I have the same issue. Below is my work around. It seems like there should be some event handler will do the same thing, but I have not found another way yet. I tried treating it like the MVC controller, but I believe there is some form token that it is expecting so that did not work [name="__RequestVerificationToken"].
Basically what I am doing here, is tricking the page into thinking I clicked a button and then telling it which function to look at. Additionally, you have access to all your model fields so you do not need to pass them.
Here is the select list:
<div class="col-md-2"><select id="ddlPortalName" asp-for="selectedPortalName" asp-items="Model.portalNames" onchange="ConcatenateURL();"></select></div>
And then here is the JS function, notice I had to change the form action to tell it which page function to look at.
<script type="text/javascript">
function ConcatenateURL() {
document.forms[0].action = "VisibilityTest?handler=ConcatURL";
document.forms[0].submit();
}
</script>
And then finally here is the c# file method.
public void OnPostConcatURL()
$('#Neighborhood_DistrictId').on('change', function () {
#Model.GetDistrictName($('#Neighborhood_DistrictId').val());
});
#Model.GetDistrictName is a server side method not Usage directly inside script
$('#Neighborhood_DistrictId').on('change', function () {
var url="http://localhost/{controllername}/{methodname}/id=";
url=url+$('#Neighborhood_DistrictId').val()
$.get(url,function(data){
...some code
});
});
var url="http://localhost/{controllername}/{methodname}/id=";
In mvc 5 genrate url from server side and set the client side variable
var url='Url.Action("{action}","{controllername}","actionname")';
this is only way for call the controller using javascript or jquery not Directly use server side method in javascript.

Access row data via client side API of GridView

I have a DevExpress GridView in my Asp.NET MVC 4 application and want to access row data on the client side via JavaScript. At the moment I am doing the following:
Specify which values should be transmitted to js function ChangeDetailsTab:
function OnGridFocusedRowChanged(s, e) {
s.GetRowValues(s.GetFocusedRowIndex(),
'MOL_REGID;BATCH_NAME;MOL_NAME;MOL_ENTERED_BY;', ChangeDetailsTab);
}
Access values from array received by ChangeDetailsTab:
function ChangeDetailsTab(rowData) {
var molRegId= rowData[0];
var batchName= rowData[1];
var molName= rowData[2];
var molEnteredBy= rowData[3];
}
This approach makes it quite bad to access a large number of values or add/remove values later because the column names have to be specified in one large string (see example 1 line 3).
Has anyone a better solution for this problem?
The client-side GetRowValues is specially designed for this purpose.
I believe it is the best solution.
this is best way,Of course a any way for this,you can called in C# Code,in CustomCallback you can run it,in client side on the javascript you can perform,such
ASPxGridView1.PerformCallback()(ASPxGridView1 has a event that named CustomCallback)with this you without reload page can get value of C# code
in C# Code :
ASPxGridView1.GetRowValues(ASPxGridView1.FocusedRowIndex,"column1","column2",....)
of course you remember that should called this event from java script in client-side
Which method is faster? I'm loading multiple values but it seems slow for some reason.
I'm doing the setup similar to what you have but I have about 25 - 30 values being loaded.
function insuranceselectionchange() {
insuranceselection.GetRowValues(insuranceselection.GetFocusedRowIndex(), 'CarrierName;CarrierAddress;CarrierAddress2')
}
function SetInsuranceValues(values) {
CarrierName.SetText(values[0]);
CarrierAddress.SetText(values[1]);
CarrierAddress2.SetText(values[2]);
}

Registering a javascript method in GWT java code so that it can be invoked based on an event in GWT

I tried looking for a similar post but couldn't find any, hence posting this query for your help. Essentially, I have a custom UI created on the GWT side. Now, I want to send events occuring at the GWT side over to the javascript/jsp page. For this I was wondering if there's a way for the jsp/javascript to register a method in the GWT code, and whenever, any event happens at the GWT side, the GWT java code can simply invoke this javascript method (which is like a function pointer/object), and the information would be notified at the jsp page. Though I can directly call javascript methods from within the GWT code, however, that means that the GWT code also need to know the javascript method name, and this results in a tight coupling. Instead the javascript could simply pass in a handle to the function to the GWT code, which would simply invoke this handle to pass in necessary events on the jsp/javascript code. Any ideas would be very helpful.
You can use a Dictionary to pass the function name to your application.
In the JSP host page:
<script type="text/javascript">
function myFunction() {
// Do stuff...
}
var MyDictionary = {
myCallback = "myFunction"
};
</script>
And in your application:
public void onEvent(EventType event) {
Dictionary d = Dictionary.getDictionary("MyDictionary");
invokeNativeCallback(d.get("myCallback"));
}
private native void invokeNativeCallback(String callbackName) /*-{
if (typeof $wnd[callbackName] === "function") {
$wnd[callbackName]();
}
}-*/;

how to read a global javascript variable from actionscript

Given that there is a global javascript variable on my web page named myVar, how can I access the value of the variable myVar from within my flash movie using javascript?
I see plenty of examples of using external interface in order to execute javascript from actionscript, but I am unable to find examples of returning values back into the flash movie using actionscript.
Thanks in advance. I hope my question is clear enough.
ExternalInterface works by allowing JavaScript to invoke an ActionScript function in the movie, and vice-versa. You can optionally receive a return value back from the invoked function. Here's a very simple example:
JavaScript:
<script language="JavaScript">
function getMyVar()
{
return myVar;
}
</script>
Flash/AS:
import flash.external.ExternalInterface;
var result:string = ExternalInterface.call("getMyVar");
You can also provide an anonymous function that returns your global variable value to the ExternalInterface.call method as follow:
ExternalInterface.call("function(){ return myGlobalVariable; }");
I've noticed Rex M's answer is a bit incomplete.
He was right about using...
import flash.external.ExternalInterface;
var result:string = ExternalInterface.call("getMyVar");
Then in your javascript you can use
<script language="JavaScript">
function getMyVar() {
return myVar;
}
</script>
However in order to use this, the flash movie must be in an html accessed over http. Not using file://
Here is a tutorial for communicating from actionscript to javascript and vice versa.
http://www.youtube.com/watch?v=_1a6CPPG-Og&feature=plcp
You can also do this:
ExternalInterface.call("eval","getVar=function(obj){return obj}");
var yourVar:String = ExternalInterface.call("eval","getVar(JSvar)");

Categories