I have written a function in javascript which take 10 parameters as input and make some calculations on them and then return answer.
I am taking input from html page and returning result back to it.
Now i want to make API for it which takes input and directly call javascript function and return results.
I am using ASP .net MVC for this purpose as I am good in MVC development, but I am little bit confused that how can I call js function directly from MVC controller and get results there.
Can any one please help me.
<script type="text/javascript">
function myFunction() { // Calling this function on button click
var a = 's';
var b = 'Small';
var c ='55';
var d ='88';
getOutput(a,b,c,d); // My javascript function which make calculations and return results
};
</script>
Why not use the API to calculate whatever you are calculating, and only returning the answer to you. You could use AJAX for that. Or I might not understand your question good enough. In that case please elaborate.
Related
I have a Jquery function in MVC View that check if at least one checkbox is clicked. Function is working properly if I use hardcoded string. But when I add
#Resources.myString into, it stops working, I can't figure out why
$('.form-horizontal').on('submit', function (e) {
if ($("input[type=checkbox]:checked").length === 0) {
e.preventDefault();
alert("This is working");
alert(#Resources.myString); //with this the function is not working anymore
return false;
}
});
I need to add the the string for multilingual purpose.
I tried diferent aproches
alert(#Resources.myString);
alert(#Html.Raw(Resources.myString))
var aaa = { #Html.Raw(Resources.myString)} //and calling the aaa
I think I am missing some basic knowlage of how this should work together
During page rendering, #Resources.myString will be injected as is in the code. For instance, if myString == "abc";, you'll end up with alert(abc); which is not what you want.
Just try to enclose your string in quotes:
alert("#Resources.myString");
As an aside, putting Razor code in Javascript logic is usually considered bad practice, as it prevents you from putting Javascript code in separate files (and therefore caching), and makes the code less readable.
Take a look as this question and the provided answer which gives a simple way to deal with that.
As ASP.NET dynamically generates HTML, CSS, JS code, the best way to find the error is to read the generated sources (Ctrl + U in most modern browsers).
You will see that your code
alert(#Resources.myString);
produces
alert(yourStringContent);
and should result in a console error yourStringContent is not defined.
You need to use quotes as you are working with a JavaScript string:
alert('#Resources.myString');
It will produce a correct JavaScript code like:
alert('yourStringContent');
I made a pure JavaScript file the past days. It's a fully operational BlackJack game. However i was told to try and use as much jquery as possible on my existing file. So I can understand the basics of jQuery.
Things like
$("#Result").empty();
and
$("#hit").attr("disabled", "disabled");
$("#hit").removeAttr("disabled");
all work fine. But i have a problem with this one for example:
function player_blackjack() {
$("#Result").text("BlackJack!");
var bet = parseInt($("#bet").val());
var bank = parseInt(document.getElementById("Bank").innerHTML);
bank = bank + (bet * 2.5);
document.getElementById("Bank").innerHTML = bank;
start_game_buttons();
}
The first lines work, all except the var bank and the line above start_game_buttons();, thats why they are still javascript written.
If i change the var bank line to this one:
var bank = parseInt($("#Bank").html);
It just doesn't work. When i run this and make myself get a blackjack ill get a NaN where the bank should be. I also tried:
var bank = parseInt($("#Bank").val());
Im basically just trying whatever i can find, but since i can't figure it out im trying my luck here.
Thanks in advance!
You forgot the () after the html method, it should be:
var bank = parseInt($("#Bank").html());
Although, why are you using parseInt on an html() call? If this is an input -- use .val()
I have googled it several times but i can't get a solution. I want to make javascript function call from the bean class in jsf and i get that using the following code.
RequestContext.getCurrentInstance().execute("handleResize()");
and is workign fine. But I want to give two parameters to that function height and width. How can it be done ? please help
You seem to fail to grasp the fact that in the context of Java/JSF, all the HTML, CSS and JavaScript code are merely plain vanilla Strings and you seem to expect that HTML/CSS/JS somehow magically runs inside Java/JSF code. This is not true. Java/JSF is a HTML/CSS/JS code producer, not executor. The webbrowser retrieves them all as one big String and then parses and executes it.
If you want to invoke a JS function with parameters supplied, like so when you would do in real JS code:
handleResize(500, 300);
And you have those values as Java variables, then you just need to make sure that you write Java code in such way that exactly the above String is produced (again, this is just Java code, no JS code):
String call = "handleResize(" + w + ", " + h + ")";
You can verify beforehand by printing it to the stdout/logger:
System.out.println(call);
It must print exactly the desired valid JS function call syntax handleResize(500, 300);.
If it does, then just pass that unmodified to RequestContext#execute().
RequestContext.getCurrentInstance().execute(call);
i know this question may be a little out of curiosity but please help me understand because i have done research on this already but may needed an explanation since it wasn't clear enough
is it possible to pass a form object and a variable at the same time in jquery post
for example
var review=$('#review').val();
var post_url="url.com";
$.post(post_url,{'review':review, $('#registration_form').serialize()}, function(returned_form)
{
$('#show_items').html(returned_form);
});
can the above code work well
Yes, you can pass multiple values. You just need to name each one:
var review=$('#review').val();
var post_url="url.com";
$.post(post_url,
{
'review': review,
'registrationForm' : $('#registration_form').serialize()
},
function(returned_form) {
$('#show_items').html(returned_form);
}
);
I have a textbox and i have values in database like ® which is equal to ® .I fetch data and write it into textbox but it writes the data as it is.The code part is like this
var data=database_values;//here there is data like this "DOLBY®"
document.getElementById(id).value = data;
I want to make the textbox value DOLBY® not DOLBY®
If you are getting ® as ® then unescape it.
document.getElementById(id).value = unescape(data);
Assuming you're using a server side language (i.e php) there are functions for that.
for example this will work with php:
html_entity_decode($data);
if you're set on using javascript, there's still a way.
see the code here.
Hi i found a way to unescape html code.Here is the function
function unescapeHTML(html) {
var tempHtmlNode = document.createElement("tempDiv");
tempHtmlNode.innerHTML = html;
if(tempHtmlNode.innerText)
return tempHtmlNode.innerText; // IE
return tempHtmlNode.textContent; // FF
}
Thanks for your help anyway