When try to pass date the slashes occur division - javascript

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>

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

Passing String variable from java to jsp

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

Init jQuery function and pass a variable with a string

Hardcoded i would initilize (a plugin in this case) like so, which is working:
var cal = $("#calendar").calendario({
caldata : {
'09-11-2015_1':['09-11-2015',0,19]
}
});
Now i want to pass the caldata option a variable with the content like that:
var init_events = $("#init_events").val();
var cal = $("#calendar").calendario({
caldata : init_events
});
init_events has the value {'09-11-2015_1':['09-11-2015',0,19]}
But that doesnt work. If I log the output of the option inside the plugin it just returns a string in the console where as if I log the first Code it logs an Object.
I tried jQuery.parseJSON(init_events) but this returns an Unexpected token error.
Any idea how i could get this working with passing a variable?
init_events is not valid JSON. JSON only allows double quotes around strings, not single quotes, so it should be:
{"09-11-2015_1":["09-11-2015",0,19]}

Javascript giving string not function on line 2 3

I am trying to link the javascript to my html file but it is giving me Unknown error String is not a function! Could somebody please tell me what is wrong!!!
Also I do not want the validation to take place on submission but on click, I mean every time I press tab. Is the following code correct?
function validate()
{
var name=document.forms[0].name.value();
var fac= document.forms[0].fac.value();
var erp= document.forms[0].erp.value();
var contact=document.forms[0].contact.value();
var studh=document.forms[0].studh.value();
var sc=document.forms[0].sc.value();
var reg=document.forms[0].reg.value();
}
Replace all:
.value();
to:
.value;
Because value is an attribute and not a function.
.value is an attribute, not a function. If there is Harshita Pathak in your name field, document.forms[0].name.value is a string "Harshita Pathak". You are trying to call "Harshita Pathak" as if it was a function when you append ().

Javascript syntax error in Date object

I have the following function in Javascript
function validateDates() {
Date fromDateObj = Date.parse(GetControlDate('CalDate'));
Date toDateObj = Date.parse(GetControlDate('ToDate'));
}
The two lines are causing a syntax error saying 'missing ';' before statement'. I can't figure out what's causing this, if I replace those lines with a simple alert statement it works fine, so I know the issue is in those lines. Also replacing those function calls GetControlDate('CalDate') with an actual date string doesn't fix the issue either. Can anyone tell me what the issue is?
Javascript is weak type. all variable is "var" other than strong type in Java or C#. Hence use var instead of Date
function validateDates() {
var fromDateObj = Date.parse(GetControlDate('CalDate'));
var toDateObj = Date.parse(GetControlDate('ToDate'));
}
JavaScript doesn't have "typed" variables. You don't need the Date before the variable name, you need var.
Also, Date.parse returns an int (the UNIX timestamp), not a Date object.
You want:
function validateDates() {
var fromDateObj = new Date(GetControlDate('CalDate'));
var toDateObj = new Date(GetControlDate('ToDate'));
}
Date is not a valid datatype for a variable use a weakly typed var.
Use var rather than Date to declare your variables:
function validateDates() {
var fromDateObj = Date.parse(GetControlDate('CalDate'));
var toDateObj = Date.parse(GetControlDate('ToDate'));
}
JS variables aren't fixed to a particular type of value and thus aren't declared with Date or int or whatever.

Categories

Resources