Accessing a javascript element in server side JSP - javascript

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.

Related

Spring message in javascript file

I'm trying spring:message tag to my javascript file, but nothing is displayed. In jsp file and tag spring:message works fine, but if I put js code to js file it's not working.
In js file I use:
password: '<spring:message code="account.enterPassword" />'
Sorry for my bad English.
Spring tags renders on the server side and javascript works in client after server side process is complete. But you can put an <script></script> tag inside your jsp, define your variable on it, and then access your global javascript variable in your external js file:
jsp:
<script>
var password: '<spring:message code="account.enterPassword" />'
//any other variables you want to use
</script>
Now, you can access the password variable inside any other js file.
Note, this script tag must be at top of your js files that you want to variables on it. Also, be careful when picking a name for global variables, because, local variables can override global variables in case of common name.
It is not possible for javascript to have access to a spring tag. spring:message is processed server-side before the page is sent to the client, javascript/jQuery is processed later on the client side.
As a workaround, put the message value in a hidden input on your jsp page. Then get its value in your javascript. In your case:
<c:set var="val"><spring:message code="account.enterPassword"/></c:set>
<input id="enterPasswordId" type="hidden" value="${val}"/>
In your javascript (using jquery) you can then use it as follows:
$('#enterPasswordId').val() //jquery
document.getElementById("enterPasswordId"); //javascript

Define a string java with a js variable [duplicate]

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.

How do I use javascript variable in LuCI Lua markup?

I am creating an HTML file for use with OpenWrt LuCI web interface. As discussed here: http://luci.subsignal.org/trac/wiki/Documentation/Templates I am using the Lua Markup language to run a Lua function called runDiag and I need to pass the javascript variable option to the runDiag function. I can't figure out how to make this work. I have tried various modifications to the markup inside the displayDiag function without success.
Can anyone help?
Example:
<%-
function runDiag(option)
return option
end
-%>
<script>
function displayDiag() {
var option = document.getElementById('iface').value;
document.getElementById('diag_text').innerHTML = '<%- write (runDiag(option)) -%>';
}
</script>
You can't do this. The Lua template is ran on the server, and the JavaScript code is ran on the client (i.e. web browser). They can't communicate.
The Lua code simply generates an HTML file to send to the client. It doesn't know about JavaScript; it's just some text that it's giving to the client. Here, option refers to a nonexistant Lua variable, which has the value of nil.
Conversely, the JavaScript code has no knowledge of the server-side Lua code. It just gets whatever the server generated. Thus, it only sees this line:
document.getElementById('diag_text').innerHTML = 'nil';
To communicate with the web server, you will need use AJAX or some other protocol.

Accessing property in my struts action from JavaScript

I am still trying to get my head around how client server comms work with JSP/js and Struts.
I have a Struts property in my action that I can display on my page using
As I understand it this works by looking in my Action for a method called getMyMessage(). So this works ok for me.
I now want to be able to access this value from inside my Javascript so I can dynamically populate a test area when the page loads.
I use
$(document).ready(function( data )
{
$('#contextsTextArea').html(data.myMessage);
});
but this does not work. I know I could assign a var in my JSP that stores the struts property and reference this var in my JS but is there a cleaner way to do this?
JSP runs on the server, JavaScript runs in your browser.
You cannot use a JSP variable like 'myMessage' as a variable in your JavaScript. You can, however, turn it into text on the server so it works as JS when it gets to your browser.
So, try something like this:
$('#contextsTextArea').val('<s:property value="myMessage"/>')
When this JSP is processed on the server the JS code will be left as text while the tag will be turned into the text in your myMessage variable.
Note that you really need to JS escape myMessage otherwise, for example, if the message contains a single quote then the JS will break.
Have a look at your page source to understand what's going on here.
If you are talking about textarea you have to use .val() instead. Like the following.
$('#contextsTextArea').val(data.myMessage);

Passing JavaScript variable to smarty

I'm having some trouble passing a variable from JavaScript to smarty.
Example:
{literal}
<script type="text/javascript">
var js_variable = 110;
</script>
{/literal}
jQuery('div.fakbox_msg').html("{/literal}{lang_sprintf id=100013 1=js_variable}{literal}");
You cannot do that in an easy way. PHP, and by extension Smarty, is parsed and run on the server side before the browser get the data. JavaScript is run on the client side when the browser parses the HTML, CSS and Javascript that the server sent.
You will have to make a new HTTP request in some way, sending the new data. You can do that by reloading the entire web page and sending stuff in the querystring (after ? in the URL), or slightly more advanced by doing an Ajax call from your JS code and make JS do the changes of the page that you desire. The latter is more complex and requires some knowledge of Javascript, but the page does not need to be reloaded in its entirety.
for more info :
Assign JavaScript variable to Smarty variable
I never used Smarty, so I may be wrong, but from what I see on your code. this should work:
jQuery('div.fakbox_msg').html("{/literal}{lang_sprintf id=100013 1=" + js_variable + "}{literal}");
Smarty cannot use client side variables, such as the ones created by JavaScript.

Categories

Resources