I have a script tag in a JSP and in that, I am reading a server side variable which I have read from the session.
I need to use a javascript variable (campaignIndex) in that server side variable ( getCreditAmountMax) like so:
<script>
var campaignIndex = jq111("select#campaigns").find(':selected').index();
GT.utilities.updateData(creditAmountDiv, "maxAmount", '<%=creditCampaignsModel.getCreditCampaigns().get(campaignIndex).getCreditAmountMax()%>', false);
</script>
Can I do that?
It is possible to dynamically generate Javascript code in JSP. It is not possible that JSP-expressions get reevaluated at client. From the point of view of the Javascript code it's only a constant.
Similar question: Access to Java model List from Javascript/JQuery dynamically.
I believe that I'm trying something that is not possible, but wanted to make sure. I'm trying to set a JavaScript variable inside of an html page. The index.html page to be specific.
I'm doing this:
<script type="text/javascript"> var theLimit = '<%=ConfigurationManager.AppSettings["TheLimit"]%>'</script>
Will it be recognized in an html file, or does it need to be converted to an aspx file?
HTML will not process the server-side command "<%", so you will have to change the page to a format that will process it, such as .aspx (not .ashx).
I have the following file structure:
Now I want to use the variables from the dropdown menu's (that are in the get_attributes.php, get_fields.php and in the get_tables.php) in my main.js and in getData.php . Now the question is how can use a variable from another .php file in a .js file?
You can't directly use PHP variables from JS scripts.
Dynamic access
If you want get variables dynamically you can use AJAX (see jQuery AJAX doc ).
Static access
If you want to access PHP variables when your JS app start you can put PHP variables in the DOM by using this syntax :
<input type="hidden" id="php_name" value="<?=$name?>" />
And use jQuery to get it :
$('#php_name').val();
I hope it will help.
Ello there,
I'm trying to assign the value of a javascript variable to a java variable. But I don't have clue how to do this? Say for example I have this:
<html>
<head>
<script type="text/javascript">
function return variable(){
var a = "hello";
return a;
}
</script>
</head>
<body>
<%
//The java code
String b = //how do I get that javascript variable here?
%>
</body>
</html>
Java script plays on browser where java code is server side thing so you can't simply do this.
What you can do is submit the calculated variable from javascript to server by form-submission, or using URL parameter or using AJAX calls and then you can make it available on server
HTML
<input type="hidden" id="hiddenField"/>
make sure this fields lays under <form>
Javascript
document.getElementById("hiddenField").value=yourCalculatedVariable;
on server you would get this as a part of request
You need to read something about a JSP's lifecycle. Try this: http://en.wikipedia.org/wiki/File:JSPLife.png
JavaScript runs on the client, but in order to change the jsp, you need access to the server. This can be done through Ajax(http://en.wikipedia.org/wiki/Ajax_%28programming%29).
Here are some Ajax-related links: http://www.openjs.com/articles/ajax_xmlhttp_using_post.php
http://www.w3schools.com/ajax/tryit.asp?filename=tryajax_first
The answer is You can't. Java (in your case JSP) is a server-side scripting language, which means that it is compiled and executed before all javascript code. You can assign javascript variables to JSP variables but not the other way around. If possible, you can have the variable appear in a QueryString or pass it via a form (through a hidden field), post it and extract the variable through JSP that way. But this would require resubmitting the page.
Hope this helps.
JavaScript is fired on client side and JSP is on server-side. So I can say that it is impossible.
I think there's no way to do that, unless you pass the value of the JavaScript var on the URL, but it's a ugly workaround.
you cant do it.. because jsp is compiled and converted into html server side whereas javascript is executed on client side.
you may set the value to a hidden html element and send to servlet in request just in case you want to use for further
As JavaScript is client side and JSP is Server side.
So Javascript does not execute until it gets to the browser,
But Java executes on the server.
So, Java does not know the value of the JavaScript variable.
However you assign value of Java variable to JavaScript variable.
aspx file
string firsrName="jafer";
myscript.js
GetMyName();
function GetMyName() {
alert('<%=firstName%>');
}
I am not getting my value
The line alert('<%=firstName%>'); use the Web Form Page syntax. It is actually not possible to get the value like this because this syntax cannot be used in external JS files.
The simpliest (but not cleanest) method is to write the JS method into the layout file or another aspx file.
Read How to get asp.net client id at external javascript file
You could make a global variable in your aspx page and access it in your js using window.objectName