Passing String variable from java to jsp - javascript

I am trying to pass a string variable from my java class as follow:
request.setAttribute("fdata",fileName);
in order to send it to my jsp file as follow:
var fd ="0";
fd=<%= (String) request.getAttribute("fdata").toString()%>;
document.getElementById("reed").value = fd;
the problem is that the 'fileName' which is a string variable in java is converted to integer for example if I pass this string : "5-9" the result that i see in 'reed' textbox is (-4) or if i pass "8-7" the result will be (1) and if I pass something else e.g. "5-8dfs" it doenst show anything. I think my jsp file sees the string variable as integer variables and mathematical operators instead of String variable itself, any hint or suggestions ??
Thanks very much.

Your JavaScript string doesn't have quotes. I think you want to do this instead:
fd="<%= (String) request.getAttribute("fdata").toString()%>";

Related

How do I create a custom javascript variable that selects part of an already existing javascript variable?

I am trying to create a custom javascript variable in GTM that returns part of a javascript variable that already exists.
Variable that already exists: window.ShopifyAnalytics.meta.product.variants.0.name
returns this: "Bamboo Basic String - Schwarz - S"
However I want to code a custom javascript variable to just return the Schwarz part, is this possible? If so what is the code that I would need?
Please can someone let me know what code to put into GTM to create this variable?
TIA
If all names are pretty much the same you could use split to get that part of string and then remove whitespaces. It would look like this:
window.ShopifyAnalytics.meta.product.variants.0.name.split('-')[1].replace(/
/g,'');
If the already existing variable is always structured the same way you could do something like this:
let variable = window.ShopifyAnalytics.meta.product.variants.0.name.split('-')
Then by calling varaible[1] you get the 'Schwartz' part of the variable.
If you want a return value you can use a function like the following and call it wherever you want.
Simply make sure to pass the correct argument content
// Declaring a function getColor that returns the second element in the list,
// trimmed (without spaces before and after)
const getColor = (content) => {
return content.split('-')[1].trim();
}
const test = "Bamboo Basic String - Schwarz - S";
console.log(getColor(test));
//console.log(getColor(window.ShopifyAnalytics.meta.product.variants.0.name));
You could split the string on the hypens (-) like this:
const productName = window.ShopifyAnalytics.meta.product.variants.0.name;
const part = productName.split(' - ')[1];
Assuming you have a consistent format, and you always want the second part after that hyphen.
split will separate parts of a string into an array where it finds a match for the argument. The first index [0] will be the product name, the second [1] will be the part you're looking for.
This could cause issues if you have a product name with a - in it too though so use with care!
If it needs to be an anonymous function for GTM, you could try the following (though I'm not a GTM expert):
function () {
const productName = window.ShopifyAnalytics.meta.product.variants.0.name;
return productName.split(' - ')[1] || 'Unknown';
}

When try to pass date the slashes occur division

I have a variable var that contains a date "29/5/2017" in my page. Now i am trying to pass this variable to a function that is stored in a separate js file. My issue is when i debug the js i see a number 0,0028... The js believes that this is a number a makes a division. how can i prevent this and pass just the date?
the var in my aspx file is :
var mydates = '29/5/2017';
the function call in my aspx file
calldate(mydates);
the function in the js file
function calldate(mydates) {
alert(mydates);
}
you need to use double quotations to single quotations mark during init variable
var mydates = "29/5/2017".toSting();
and user .toString() function for canvart to string.
Thanks
Instead of passing date as string try to pass variable as Date datatype.
there might be chances that the variable getting changed before calling the function.
<pre>
var mydates = Date("29/5/2017");
</pre>

MVC - reading model variable in jquery

I am trying to read model variable in jquery ready function.
var jsNotes1 =#Model.Notes1;
when the model.notes1 has an integer value, I was having no issues.
But when the model.notes1 has a string value, say for ex: "abcd", the line is getting converted as below
var jsNotes1 = abcd
and Jquery is assuming abcd as a variable rather than as string value and is throwing reference error that abcd is not defined.
Please help me if i am missing something here
try to this
var jsNotes1 ='#Model.Notes1';
You need to wrap it in quotes as it needs to be treated as a string, otherwise it's invalid JavaScript
var jsNotes = "#Model.Notes1";
Try this is in javascript...
var jsNotes1= #Html.Raw(Json.Encode(Model.notes1));

Node.JS - Filename won't accept a variable

doesn't work:
console.log(obj.html_template); // outputs "myfile.html"
var html = fs.readFileSync(JSON.stringify(obj.html_template)); // file not found.
works:
console.log(obj.html_template); // "myfile.html"
var html = fs.readFileSync("myfile.html"); // Works.
I'm going crazy.
> JSON.stringify('myfile.html')
""myfile.html""
Your code is looking for the file "myfile.html" (note the superfluous quotes) in the filesystem. It doesn't exist.
Just look for it without stringification:
var html = fs.readFileSync(obj.html_template);
When you call JSON.stringify, it will convert all the Strings to the JSON format Strings, with surrounding double quotes. Quoting ECMAScript 5.1 Specification for JSON.stringify,
If Type(value) is String, then return the result of calling the abstract operation Quote with argument value.
And the Quote operation, is defined here, which basically surrounds the string with " and takes care of special characters in the String.
So JSON.stringify converts, a string, for example, abcd.txt to "abcd.txt", like this
console.log(JSON.stringify("abcd.txt"));
// "abcd.txt"
which is not equal to abcd.txt.
console.log(JSON.stringify("abcd.txt") == "abcd.txt");
// false
but equal to "abcd.txt".
console.log(JSON.stringify("abcd.txt") == '"abcd.txt"');
// true
So, your program searches for a file named "abcd.txt" instead of abcd.txt. That is why it is not able to find the file and fails.
To fix this problem, just drop the JSON.stringify and pass the string directly, like this
var html = fs.readFileSync(obj.html_template);
why are you using JSON.stringify in the first place? you should be able to just do
var html = fs.readFileSync(obj.html_template);

passing JavaScript array of long to code behind?

I have returned object from signature device and when i make quick watch on it, it told me that its an array of long and when i pass it to web method in my code behind (vb.net) it gives me nothing.
i need some help..
note: i'm using an activeX to capture the signature from the device.
this is javascript code :
function OnSave() {
var sign = document.FORM1.SigPlus1.SignatureString;
PageMethods.Save(sign);
}
this is my webmethod:
<WebMethod()> _
Public Shared Function Save(ByVal obj As Object) As String
Dim obj1 As New PFSIGNATURELib.SigniShellSignature
obj1.SignatureMime = obj
obj1.SaveBitmapToFile(System.AppDomain.CurrentDomain.BaseDirectory() & "\sign1.bmp", 200, 200)
Return "\sign1.bmp"
End Function
I don't know much about ASP.Net, but it seems like the PageMethods.Save function can't handle an array of long. Another possibility is that the sign variable is null in the javascript code.
Try adding
alert(sign);
in the middle your Javascript function, or better yet, install firebug and do
console.log(sign);
instead. This way you'll make sure the sign var actually contains what you think it does.
If it indeed contains an array of numbers (javascript doesn't have a long type), maybe you need to convert it to something else before calling the PageMethods.Save function.
For example, this javascript snippet will convert sign into a space-separated string of numbers:
s = ""
for (i in sign) {
s += sign[i] + " ";
}
sign = s
If you manage to pass this string to your webmethod, you can use some string parsing to get back the original array.

Categories

Resources