How to access variables from the ModelAndView in JavaScript code [duplicate] - javascript

This question already has answers here:
Access Java / Servlet / JSP / JSTL / EL variables in JavaScript
(5 answers)
Closed 3 years ago.
I have a controller which returns a ModelAndView object
return new ModelAndView("operatorDetail", model);
I can then access the elements in the model object from my JSP page like this
<c:if test="${not empty operator and operator.regionId eq item.regionId}"
my question is, is there a way I can access those elements in my javascript code ?.
I thought about putting those values in an input element in HTML and make it hidden and access their values, but is there a more practical way?
thank you.

Yes of course you can have something like this. Declare var in jsp
<c:set var="yourJspVar" scope="session" value=" your object"/>
and in java-script set variable like
<script>
var jsVar = '${yourJspVar}';
alert(jsVar);
</script>

Related

how to pass javascript value to viewbag value [duplicate]

This question already has an answer here:
Assigning ViewBag property in javascript
(1 answer)
Closed 4 years ago.
I would to ask, is it possible to pass a javascript value to a viewbag? I have tried to serach on stakoverflow, it seems that it is possible to pass viewbag value to javasript, but not vice versa.
Thank you very much
Your Server end :
ViewBag.Js = "alert()";
Your CsHtml Page :
<script type='text/Javascript'>
#Html.Raw(#ViewBag.Js);
</script>;

How to include a PHP variable in Javascript code? [duplicate]

This question already has answers here:
How do I pass variables and data from PHP to JavaScript?
(19 answers)
Closed 5 years ago.
[1] http://imgur.com/a/Ny8f7 "html code"
[2] http://imgur.com/a/kzEN2 "php code"
I try to use the variables from php in javascript to generate a chart with them.
Try using an id e.g.
In your html
name='someName'>
Then use that ID in your JS.
Use can use something like this:
var data = '<?=$dataFromPhp?>';

creating an array in jsp from list of objects [duplicate]

This question already has answers here:
Access Java / Servlet / JSP / JSTL / EL variables in JavaScript
(5 answers)
Closed 6 years ago.
I am trying to build an array of DemoNames.name attribute, dynamically in jsp, how can i create an array and then pass to my js file for further validation?
<c:forEach items="${demoNames}" var="DemoNames">
${DemoNames.name}
</c:forEach>
You'll want to make this code declaration somewhere in the html after the js file is declared.
<script>
var nameArray = [];
<c:forEach items="${demoNames}" var="DemoNames">
nameArray.push("${DemoNames.name}");
</c:forEach>
yourJSFileFunction( nameArray );
</script>
If you viewed source on this I believe you would have a push line for each of the names.

How can I send a Javascript variable to php variable? [duplicate]

This question already has answers here:
How do I pass JavaScript variables to PHP?
(16 answers)
Closed 8 years ago.
<script>
function myHandler(){
var idproduct = myHandler.caller.arguments[0].target.id;
document.getElementById("product").value = idproduct;
}
</script>
<input class="form-control" id="product" name="view" type="text" disabled " />
<?php $product_view = X ?>
Some one know what can I add on my javascript so I can send the variable "idproduct" to product_view variable
Use a form and Post or Get You can also optionally use WebSockets or you can probably use a request to a .php page setup to handle such of another type of request.
I suggest to create a hidden field and then treat that in your post back or call back

Something similar like <?php echo $_GET['height'];?> in HTML? [duplicate]

This question already has answers here:
How can I get query string values in JavaScript?
(73 answers)
Closed 9 years ago.
im wondering if there is a function or something like php's <?php echo $_GET['height'];?> but in HTML or javascript.
I need it to get variables of the URL
Use location.search. It returns the entire query string, which you then can split, if you need to.
Html is a markup language, so you can't do such things.
Try using javascript.
here's an example:
//returns the height of the client
function height(){
var clientHeight = document.body.clientHeight;
alert(clientHeight);
}

Categories

Resources