Init jQuery function and pass a variable with a string - javascript

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]}

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>

jQuery - console.log() returns an element but I can't access it

I happened to have super dumb issue and I'm stuck.
I do console.log(data) and I get exactly this:
<a href='http://www.someurl.com'>caption</a>
The question is how do I get this links "href" attribute.
I have absolutely no idea why, but these doesn't work:
data.text() == Uncaught TypeError: undefined is not a function (should be: caption)
$('a',data).attr('href') == undefined (should be: http://www.someurl.com)
Maybe this is not a string, but object or something else? How to check that? My JS looks like this:
window.send_to_editor = function(data) {
var videourl = data;
console.log(videourl.text()); // Uncaught TypeError: undefined is not a function
console.log(videourl); // <a href='http://www.someurl.com'>caption</a>
}
Using jQuery, you can do something like that:
var data = "<a href='http://www.someurl.com'>caption</a>";
var link = $(data).attr('href');
It will create dynamically your DOM element, then you will be able to get your attribute href.
You should first find out, what type data is. To do this you can use the JavaScript builtin function typeof(data).
It's not a jQuery object. That is why it is undefined. First, create a jQuery object.
var _videourl = $(videourl);
console.log(_videourl.text());
console.log(_videourl.attr('href'));
// caption (index)
// http://www.someurl.com
DEMO
In your case, data is a string, not a jQuery object and therefore does not have of jQuery's methods (like text).
If you are certain that data is a string containing a link, you can use a regular expression to extract the link like so:
var match = data.match(/href='(.*)'/g);
url = match && match[1];
console.log(url);
Alternately, you can create a jQuery object from your string. But that's a much more expensive operation if you just want to get the url.

oocharts invalid filter parameters after array is converted to string

I've been having trouble setting a dynamic filter for oocharts.
I've looped through some things and have created an array (scenefilters) which I've joined to make a string. I've then tried to use this as a filter, which returns the error:
*JSONP.callbacks.request_63 && JSONP.callbacks.request_63({"error":"Invalid param {filters}: Filter string is not in valid format"});*
Now if I console.log the variable 'locationfilter2' and copy and paste it directly into the filter, it works fine - but it doesn't work just with the variable. Do I need to do something else to the variable to make sure it's a string?
var locationfilter = scenefilters.join(",");
var locationfilter2 = '"'+ locationfilter +'"';
var visits = new oo.Metric("54190402", "12m");
visits.setMetric("ga:visits");
visits.query.setFilter(locationfilter2);
visits.draw(timeline);
EDIT
I've tried outputting the variable to a textfield and copying it from there, same thing. The output is "ga:pagePath=~s101$,ga:pagePath=~s102$,ga:pagePath=~s103$,ga:pagePath=~s104$,ga:pagePath=~s105$,ga:pagePath=~s106$,ga:pagePath=~s107$,ga:pagePath=~s108$,ga:pagePath=~s109$,ga:pagePath=~s110$,ga:pagePath=~s111$,ga:pagePath=~s112$,ga:pagePath=~s113$,ga:pagePath=~s114$,ga:pagePath=~s115$,ga:pagePath=~s116$,ga:pagePath=~s117$,ga:pagePath=~s118$,ga:pagePath=~s119$,ga:pagePath=~s293$,ga:pagePath=~s301$"
And if I copy this directly into the filter, i.e. visits.query.setFilter("ga:pagePath=~s101$,ga:pagePath=~s102$,ga:pagePath=~s103$,ga:pagePath=~s104$,ga:pagePath=~s105$,ga:pagePath=~s106$,ga:pagePath=~s107$,ga:pagePath=~s108$,ga:pagePath=~s109$,ga:pagePath=~s110$,ga:pagePath=~s111$,ga:pagePath=~s112$,ga:pagePath=~s113$,ga:pagePath=~s114$,ga:pagePath=~s115$,ga:pagePath=~s116$,ga:pagePath=~s117$,ga:pagePath=~s118$,ga:pagePath=~s119$,ga:pagePath=~s293$,ga:pagePath=~s301$");
It works well. But if I use the variable I get the error. Anyone any ideas?
OK stupid one. Removed the ""s and it works fine.

Jquery - Edit Object Parameters

I have an Object and I'm trying to use jquery to quickly change the parameter values, but the parameters keep coming back null. code brings back the list of parameters but I can't seem to change anything. Even if I put it at it's base of parameters to change everything - it still comes back as null.
Other than that it works, but if u look closely you will see some api error messages in black at the top left. I added a pastebin so you can see what I'm doing.
http://jsfiddle.net/f4qMe/
and below is the javascript I'm running to try and change the objects parameters. The object is called (id) twitchTV.
function test(){
var data = "http://www.twitch.tv/widgets/live_embed_player.swf?channel=day9tv";
var src = "hostname=www.twitch.tv&auto_play=true&start_volume=25&channel=day9tv";
var code = $("#twitchTV").html();
var newcode = $("param", code).attr("value", src).html();
$("#twitchTV").html(newcode);
$("#twitchTV").attr("data", data);
}​
Your problem is probably here:
var code = $("#twitchTV").html();
var newcode = $("param", code).attr("value", src).html();
html() returns a string so code is a string and you're using it as context in newcode which expects an DOM element or jquery object instead.

Categories

Resources