how to pass javascript value to viewbag value [duplicate] - javascript

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>;

Related

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

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>

Displaying value transferred from URL via javascript [duplicate]

This question already has answers here:
Get the values from the "GET" parameters (JavaScript) [duplicate]
(63 answers)
Closed 3 years ago.
I'm having an issue with my codes concerning the transfer of information from one page to another using the url as shown below:
window.location.href ="form.html?uname="+uname;
The value is displaying in the url box but when I try to display it on the form.html page using the following code:
window.onload = function ()
{
var name = document.getElementById("uname");
alert(name);
}
The alert keep displaying null.
What is the issue because after an hour of troubleshooting, I can't seem to figure it out.
Is the null being displayed in the alert box means that the value is not being retrieve from the url?
Thanks in advance.
document.getElementById('uname')
looks for an HTML element with the corresponding id. What you probably want to do is:
alert(uname)

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();

How to escape # in javascript to retrive object property [duplicate]

This question already has answers here:
JavaScript property access: dot notation vs. brackets?
(17 answers)
Closed 7 years ago.
i have Javascript object as below.
How can i get value for SERIAL#, like i can retrieve *.INST_ID or *.INSTANCE.
how can i escape # and get the value required.
So far i have tried SERIAL#23% but none helped so far.
var t = {"INST_ID":"1","INSTANCE":"xina","SID":"27","SERIAL#":"48810", "PROGRAM":"Perl#app01"}
console.log(kSess.SERIAL%23); gives syntax error
I am parsing variable "t"
This data is coming from java code, so there is nothing much i can do to change SERIAL# to something else
Any idea?
Try to retrieve the value like this...
t["SERIAL#"]; // will return you the value..
Store it somewhere or play with it as you like. :)

Jquery is not accepting hypen in the jsp page? [duplicate]

This question already has answers here:
How do I reference a JavaScript object property with a hyphen in it?
(11 answers)
Closed 8 years ago.
I have a field sent by SERVICE which is having hypen in it. Eg., first-name (As JSON object)
But when I try to get the value of that field through the jsp. I am getting a script error.
Please let me know how to access the hypen also in this?
var nameList = msg.RESPONSE.DATA.NAME-LIST;
The above way when I try to access it is throwing script error
A variable or property name with an hyphen is indeed wrong in javascript (Jquery).
However, you can access the "problematic" property like this :
var nameList = msg.RESPONSE.DATA["NAME-LIST"];
I would recommend to rename the property(ies)
without hyphen if you control the content of this response

Categories

Resources