I am working on one script which has been called from the websocket . This page of code is of html.erb
It pass variable to the javascript, and from that javascript variable i want to assign it to ruby variable ,
Here is the code
function set_color(val1,val2)
{
<%background_color_id = %>
var obj_color_id = '<%=background_color_id ='+val2+'%>' ;
console.log(obj_color_id)
}
The result from console log is +val2+
If I pass var obj_color_id = '<%=background_color_id ='val2'%>' ;
The result from console log is val2
Please help me assign javascript variable to ruby variable
You cannot do this. Javascript runs Client side, Ruby runs Server side.
You can't do that. All values <%= are translated on server side and their values are send to client. There is no ruby on client side. You have to send request to your websocket or http server in order to pass some data to server.
Actually, if I understand your code (your question is not well phrased, unfortunately), the simple solution is:
1- Assign a value via server-side code:
function set_color(val1,val2)
{
var bkgdColorId = "<%= background_color_id %>";
var obj_color_id = bkgdColorId;
console.log(obj_color_id)
}
2- (Or,) Assign a value from client-side code:
function set_color(val1,val2)
{
/** pseudo-code **/
on-click-event: makeAjaxCallToServer(){
urlForWebService, { color: val2 }
}
}
Using some jQuery (if assigning to server from client-side event) would greatly facilitate this process.
Related
I need to get the data of productSkus[ using python but I don't know how to access it. the javascript comes from http://www.ulta.com/lid-lingerie-eye-tint?productId=xlsImpprod15361061 .
This is how it looks like.
<script type="text/javascript">
var currentSkuId = '2502111';
var currentProductId = 'xlsImpprod15361061';
var productSkus = new Object();
productSkus[2502111] =
{"displayName":"Fame & Fortune","id":"2502111","imgUrl":"http://images.ulta.com/is/image/Ulta/2502111?$detail$","largeImgUrl":"http://images.ulta.com/is/image/Ulta/2502111?$lg$","swatchImgUrl":"http://images.ulta.com/is/image/Ulta/2502111sw?$50px$","swatchHoverImgUrl":"http://images.ulta.com/is/image/Ulta/2502111sm?$md$","skuSize":"0.13","skuSizeUOM":"oz"};........
Can anyone help me with this?
If you want to use productSkus on the server side, then you need to use AJAX to send the JS variable to the server.
As Django template is compiled server side. It is then sent to the client where their browser executes the JavaScript. Nothing that is changed by the JavaScript executing on the client browser can have an affect on the template. It's too late at that point.
However the JavaScript could do something like make another request from the server for more information. Or you could just pre-compute the value on the server before you send it to the client.
You can of course use Django templates to set JavaScript variables.
<script>
var myVar = '{{ py_var }}';
</script>
use html form to submit the data to server, or across api
I would like to call some variable (code and name -- String) from java class on html class (<label>). And I wonder whether it can be directly applied (without using jsp)? Or it necessary to use javascript?
And if it requires javascript I have added the code below, but it's not working.
<script type="text/javascript">
document.getElementById("lists").innerHTML = name;
</script>
And also this is my java file that store variable code and name.
List<Names> nameList = Common.getNameList(data.getName());
for (int i = 0; i < nameList.size(); i++) {
System.out.println(nameList.get(i).code + "=" + whsList.get(i).name);
}
And in HTML I had,
<div id="lists">
<label>Code<label>
<label>Name<label>
</div>
Please help. Thank you.
No, you can't directly. JavaScript is executed on the client side (browser)
JavaScript is client side scripting lang. and 'Java' run on server side. so you need to send request to server . if you want to use java lang on server side then we need to use servlet or framework like a spring, struts etc.
if you want to use java variable in client side JavaScript then send request to server. you can send request using ajax and send nameList in JSON type from servlet as response then all list available to use in JavaScript ... so you need to refer some example like ...
new link i hope this help
http://www.technicalkeeda.com/jquery/spring-framework-jquery-ajax-request-and-json-response-example
I am trying to set a ruby instance variable's value to the latitude and longitude values.
How would you set the value of an instance variable inside a javascript function?
Something like this?
<script>
if (geoPosition.init()){ geoPosition.getCurrentPosition(geoSuccess, geoError); }
function geoSuccess(p) {
<% #lat_lng = p.coords.latidude + "," + p.coords.longitude %>
}
function geoError(){ alert("No Location: Won't be able to use Maps functionality"); }
</script>
Something is telling me I need json.
You are not understanding where and when code runs.
Ruby (on Rails, or otherwise) runs on the server. It creates the HTML+JS+whatever that gets sent over the Internet to the client.
The client (web browser) receives this code and, among other things, executes JavaScript code.
You cannot, therefore, have your Ruby variables be set as the result of running JavaScript code…without contacting the web server again.
You need to use AJAX (or some other, lesser technology) to send a request back to the web server when that function is called.
For example (using jQuery's jQuery.post):
function geoSuccess(p){
$.post('/newcoords',{lat:p.coords.latitude, long:p.coords.longitude});
}
and in some controller somewhere:
def newcoords
#lat_lng = [params[:lat],params[:long]].join(",")
end
I need help. I need to call a Java function "getLocCountByWhId()" in a Java class; this Java function is being called within a JavaScript in a for-loop. I need to pass in a JavaScript variable as a parameter into this Java function "getLocCountByWhId()". I have been struggling for a week and reading numerous website to get some guidelines but I have not been able to resolve the problem. Thank you in advance for your help. The code is listing below:
<script language="JavaScript">
<!--
function onCreatePO()
{
<%long jspAllocId = alloc.getId();%>;
var recItemId = ""; // Local variable for item id.
var recWhId = ""; // Local variable for warehouse id.
for (var i=0, j=document.what_if_summary.elements.length; i<j; i++)
{
var recStr = document.what_if_summary.elements[i].value;
var splitStr = new Array();
splitStr = recStr.split('^');
recItemId = splitStr[1]; // Get the field value for Item_ID.
recWhId = splitStr[2]; // Get the field value for Warehouse_ID.
// Get a database connection with global 'conn' object and retrieve store count.
<%AfsGetVDSCountByWarehouseBean.setConnection(conn);%>;
// The below assignment from JavaScript variable to JSP variable do not work
// because of a second JavaScript tag. How can I get around this ?
//<% String jspItemId = "<script>document.writeln(recItemId)</script>"; %>
//<% String jspWhId = "<script>document.writeln(recWhId)</script>"; %>
currentStoreCount = <%= AfsGetVDSCountByWarehouseBean.getLocCountByWhId(jspAllocId, jspItemId, jspWhId)%>;
}
}
//-->
</script>
// I get this example of assigning Javascript to JSP variable but I got double tag problem.
<script>
var v="Roseindia";
</script>
<% String st="<script>document.writeln(v)</script>";
out.println("value="+st); %>
What you are trying to do is not possible, and (sorry!) reflects a basic misunderstanding as to how server-side code such as JSP (or ASP, PHP, etc) works.
A JSP page is basically a Java servlet running on the web server that dynamically generates a web page and returns it to the browser. The resulting web page may contain HTML, CSS, JavaScript, etc., but to the Java code all of that is just text. The Java/JSP code cannot understand or interact with the JavaScript because the JavaScript doesn't run on the web server, it is just more text to be sent as part of the response back to the browser.
When the browser gets the response it will display the web page and execute any JavaScript.
Further reading (I wouldn't put too much faith in what you read at the RoseIndia site):
http://www.ibm.com/developerworks/java/tutorials/j-introjsp/
http://www.oracle.com/technetwork/articles/javase/servlets-jsp-140445.html
http://java.sun.com/developer/onlineTraining/JSPIntro/
I am trying to send the value returned by a function to a database using client-server model. I am using Javascript as my client model. How can I connect to a server to send my data to the database.
For example; from the below code, I want to store abc in the database.
<blink>
<html>
<head><script>
var t = "abc";
function test(){
return t;} // RETURNED VALUE WILL BE "abc"
</script></head>
<body onload = "test()">
</body></html>
</blink>
You cannot interface with a database server directly with Javascript. You would need to use some sort of server side script like ASP.NET, PHP, Java, etc.
Reply in answer of your comment concerning AJAX I suggest you have a look at this: https://developer.mozilla.org/en/AJAX