How to retrieve setAttribute from servlet into JavaScript [duplicate] - javascript

This question already has answers here:
Access Java / Servlet / JSP / JSTL / EL variables in JavaScript
(5 answers)
Closed 6 years ago.
I'm trying to retrieve the value of my setAttribute into my javascript code but I get a syntax error when I run the page.
Here is the code of my setAttribute in my servlet:
request.setAttribute("codeIGA", codeIGA); //a string is being passed here
Here is the code of my JS:
$(document).ready(
function () {
var count = 0;
var lastCodeIGA = ${sessionScope.codeIGA}; // syntax error here
console.log(lastCodeIGA);
.
.
.
etc
How can I retrieve the value from my servlet and input it into my JS?

You can use following to retrieve the required value.
var lastCodeIGA = '${codeIGA}';

Try with scriptlet.
var test = <%=request.getAttribute("codeIGA");
Or
var lastCodeIGA = '${codeIGA}';

Related

How to use a JavaScript value inside EJS [duplicate]

This question already has answers here:
What is the difference between client-side and server-side programming?
(3 answers)
Closed 1 year ago.
How can I use a JavaScript value inside an EJS statement?
E.g. I grab the value of a select option with JavaScript and want to load data from an object depending on the value.
var gerichtSelectID = $("#gericht" + id).val(); //e.g. 8
var gerichtPreisID = <%= gerichte.data[gerichtSelectID].preis_id %>; // should be gerichte.data[8].preis_id
EJS values can't be modified in JavaScript, but the easiest solution is to convert the EJS object into JSON.
var gerichte = <%- JSON.stringify(gerichte) %>;
var gerichtSelectID = $("#gericht" + id).val();
var gerichtPreisID = gerichte.data[gerichtSelectID].preis_id;

how to select a value from drop down using jquery that got generated from txt file [duplicate]

This question already has answers here:
Access Java / Servlet / JSP / JSTL / EL variables in JavaScript
(5 answers)
Closed 5 years ago.
I am generating a drop down from a text file using "load" function in jquery. I am in need to select a dynamic value from this drop down based on my session variable.
$("#id").val(session variable) is not working.
Is there any other way to select value from this drop down?
you can use a scriptlet to transfer the session variable to javascript like so:
var sessVar = '<%= session.getAttribute("something") %>';
Then you can do the following to get the value:
var id = "#" + sessVar;
var dropValue = $(id).val();
and to set a value you do:
$("#id").val(sessVar);
lets suppose your Select dropdown id is : "dropdownlist1"
Use :
var SelectedValue = $('#dropdownlist1').val();
To Assign value using jquery:
$('#dropdownlist1').val("SomeText");

How to take a value from a link using jquery [duplicate]

This question already has answers here:
How can I get query string values in JavaScript?
(73 answers)
Closed 7 years ago.
I want to pass one value through ajax by taking the values from jQuery. But I am using link so I have problems taking the value. I tried the following,
<a id="addpa" class="ActionPopup" href="http://localhost:49951/admin/assignhome/Add?sPId=7">Add</a>
Jquery Code:
var spId = $("#addpa").prop("href"); // Here i am getting a whole Url
var thequerystring = getParameterByName("sPId");
The result is showing undefined. How to take the value of sPId? Give me ideas..
How to take the value of sPId?
Try using String.prototype.split() , Array.prototype.pop()
var spId = $("#addpa").prop("href").split(/=/).pop();

save jscript integer as a php integer [duplicate]

This question already has answers here:
What is the difference between client-side and server-side programming?
(3 answers)
Closed 8 years ago.
I'm trying to get the browser's width and then compare it as a php variable, but it is saving it as a string, I tried parsing it to another variable but it only returns 0.
$tam='<script>var j= $(window).width(); document.write(j)</script>';
I'm doing the parsing like this:
$ntam=(int) $tam;
You can't save a javascript value into a php variable directly. But you can....Put the php value into a javascript variable and use that for making your changes using javascript.
<?php echo '<script type="text/javascript">var jVar = "$phpVar";'; ?>
If you are importing your script from a js file you will want to add this before your import or course.
And I'm not sure what you're trying to accomplish but if you need a js value you can always send that value using an ajax Request...
var ajax = new XMLHttpRequest();
var form = new FormData();
form.append('win_size',window.innerWidth);
ajax.open("http://example.com/processJSWindowSize.php","POST",true);
ajax.send(form);
On the server side code, which in this example is processJSWindowSize.php, you can have something along the lines of
<?php
if($_SERVER['REQUEST_METHOD'] == 'POST'){
$jsWindowSize = strip_tags(stripslashes($_POST['win_size']));
//do what you need with the window size
}
?>
Your script needs to return the width and it needs to be a function
<script>
var tam= function()
{
var j= $(window).width();
document.write(j)
return $(window).width();
}
var ntam = tam;
</script>

How to pass global JS var within regexp [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How do you pass a variable to a Regular Expression JavaScript?
This is my regexp
regexp:{
spamcheck : /^[15]+$/
}
I need to do this
var spamcheck_sum = 15;
regexp:{
spamcheck : /^[spamcheck_sum]+$/
}
and
Side note: I cant use my spamcheck_sum var outside the js reason is my js file loads first and my var for sum loads after , any way to globalize that var to be acceptable no matter if js file loads first?
working with cms here and js file placement is automatic if I use the cms js placements calls . Joomla
var spamcheck_sum = 15;
regexp:{
spamcheck : new RegExp("^["+spamcheck_sum+"]+$")
}

Categories

Resources