Call an ActionScript function from Javascript - javascript

I want to call an Action Script function from javascript. But also I need the ActionScript function to return a value to the javascript call.
This is what I want to accomplish.
/* JS CODE */
var str = getStringFromFlash();
alert(str);
getStringFromFlash should be a function defined in ActionScript that can return a value.

On the Flash side:
ExternalInterface.addCallback("getValue", getValue);
Where getValue() is the function that returns the string.
Then on the JavaScript side:
var flashObject = document.getElementById("myFlashObject");
var str = flashObject.getValue();
alert(str);
See API documentation for the ExternalInterface class for a more complete example.

ExternalInterface.addCallback()

Related

Razor Syntax in External Javascript

So as you might know, Razor Syntax in ASP.NET MVC does not work in external JavaScript files.
My current solution is to put the Razor Syntax in a a global variable and set the value of that variable from the mvc view that is making use of that .js file.
JavaScript file:
function myFunc() {
alert(myValue);
}
MVC View file:
<script language="text/javascript">
myValue = #myValueFromModel;
</script>
I want to know how I can pass myValue directly as a parameter to the function ? I prefer to have explicit calling with param than relying on globals, however I'm not so keen on javascript.
How would I implement this with javascript parameters? Thanks!
Just have your function accept an argument and use that in the alert (or wherever).
external.js
function myFunc(value) {
alert(value);
}
someview.cshtml
<script>
myFunc(#myValueFromModel);
</script>
One thing to keep in mind though, is that if myValueFromModel is a string then it is going to come through as myFunc(hello) so you need to wrap that in quotes so it becomes myFunc('hello') like this
myFunc('#(myValueFromModel)');
Note the extra () used with razor. This helps the engine distinguish where the break between the razor code is so nothing odd happens. It can be useful when there are nested ( or " around.
edit
If this is going to be done multiple times, then some changes may need to take place in the JavaScript end of things. Mainly that the shown example doesn't properly depict the scenario. It will need to be modified. You may want to use a simple structure like this.
jsFiddle Demo
external.js
var myFunc= new function(){
var func = this,
myFunc = function(){
alert(func.value);
};
myFunc.set = function(value){
func.value = value;
}
return myFunc;
};
someview.cshtml
<script>
myFunc.set('#(myValueFromModel)');
myFunc();//can be called repeatedly now
</script>
I often find that JavaScript in the browser is typically conceptually tied to a specific element. If that's the case for you, you may want to associate the value with that element in your Razor code, and then use JavaScript to extract that value and use it in some way.
For example:
<div class="my-class" data-func-arg="#myValueFromModel"></div>
Static JavaScript:
$(function() {
$('.my-class').click(function() {
var arg = $(this).data('func-arg');
myFunc(arg);
});
});
Do you want to execute your function immediately? Or want to call the funcion with the parameter?
You could add a wrapper function with no parameter and inside call your function with the global var as a parameter. And when you need to call myFunc() you call it trough myFuncWrapper();
function myFuncWrapper(){
myFunc(myValue);
}
function myFunc(myParam){
//function code here;
}

pass string from C# to Javascript with Phonegap and Windows Phone 8

When developing a plugin, how is possible to get the result of my C# function to my javascript.
For instance having the echo plugin as in the official cordova doc
When I pass a result from C# to Javascript calling
DispatchCommandResult(new PluginResult(PluginResult.Status.OK, "{result:\"super awesome!\"}"));
Please How can I get the result inside my javascript code (how can I get: "super awesome")?
When you dispacth the plugin result with status OK from C#, the win callback is called, and it brigns the result with this.
Example, your javascript plugin calls this from javascript:
cordova.exec(win, fail, "Echo", "echo", ["input string"]);
So you have to create a win function that will get the result:
function win(result) {
alert(result);
}
You could just write the string directly :
<script> var JavascriptBlah = '<%=yourString%>'</script>
or use the method InvokeScript to call a function to set the string :
webBrowser.InvokeScript("yourStringSetter", "yourString");
demo of a simple setter in js
source for the first solution

Cannot resolve symbol #Url.Action

Should be a simple problem but i dont know exactly why its like this.
In my ASP.NET MVC 5 website i have a simple view with a grid, and a cell action that calls a js function sending some parameters to this function.
function OnCellClick(param1, param2) {
var urlAJAX = #Url.Action("GetJson", "PosicaoEstoque", new { p1 = param1 , p2 =param2}); }
So, like this i get the 'Cannot Resolve symbols' for the param1 and param2.
How can i solve it?
You can use placeholder. Generate url using place holder parameters and then replace them with param
function OnCellClick(param1, param2) {
var urlAJAX = '#Url.Action("GetJson", "PosicaoEstoque", new { p1 = -1 , p2 = -2})';
urlAJAX = urlAJAX.replace('-1', param1).replace('-2', param2);
}
Your problem is that you're mixing server-side and client-side code. You cannot simply put JavaScript variables in Url.Action(), as it runs on the server-side. What you can do is to put some dummy values as parameters and call JavaScript's replace() function on generated URL.
Check this for reference.
Without using Url.Action like below:
var urlAJAX = 'PosicaoEstoque/GetJson?p1=' + param1 + '&p2=' + param2;

large text as parameter in javascript function

I would like to process large amount of text inside javascript, I tried to feed the text as a parameter to my js function
function processText(myText){
//do some task
}
It seems that javascript cannot pass the parameter correctly.
Is there any work around for this problem?
in the main code call processText function like:
$yourText=urlencode($originalText);
processText($yourText);
above is php code sample
function processText(myText) {
var lsRegExp = /\+/g
var properText=unescape(String(myText).replace(lsRegExp, " "));
// do something with properText
}

Dynamically changing javascript variable

I am making an AJAX calling using JQuery. I get back a JSON object containing HTML and Javascript. Within the javascript, there is a function Initialize(). This returned javascript from the AJAX call should replace the initial definition of Initialize() which was there before the AJAX call was made. So basically, I'm trying to dynamically change the code of the javascript function to code I get back from an AJAX call. Help? Thanks in advance.
I think you might be looking for something like this:
init = function() { alert("init"); };
hash = { "newInit" : function() { alert("newInit"); }};
init(); // alerts "init"
init = hash.newInit;
init(); // alerts "newInit"

Categories