Note that there are some explanatory texts on larger screens.

plurals
  1. POget value of function in json string
    primarykey
    data
    text
    <p>I'm tring to get some json to contain a function so that the option in the plugin gets the returned value of the function that was in the json string.</p> <p>The set up is as such:</p> <p>default.txt</p> <pre><code> [ { "element":"a[href$='.jpg']", "options":{ "label":"test" } },{ "element":"a#hover", "options":{ "label":(function(ele){ return 'test'; })() } } ] </code></pre> <p>and the plug-in is</p> <pre><code>(function($) { function defined(obj){return typeof(obj)!=='undefined';} function evaluate(ele, obj) { if(typeof obj === 'function') { //alert('is function'); obj = obj(ele); }else{ try{ //alert('thinking it may be a function still'); obj = eval("("+obj+"("+ele+"));"); //obj = (obj)(ele); }catch(err){ //Handle errors here //alert('not function'); } } return obj; } function debug(message) { console.debug(message); } $.set = function(options){ var settings = $.extend({}, {}, options); if(defined(settings.actions)){ $.each(settings.actions, function(index, value) { $(value.element).do_action(defined(value.options)?value.options:null); }); } } $.fn.do_action = function(options) { // Add event handler to all matching elements return this.each(function() { var ele = $(this); var settings = $.extend({}, $.fn.do_action.defaults, options); var label= evaluate(ele, settings.label); var message = "label:'" + label + "'"; debug('Tracking ' + action + ' ' + message); }); }; $.fn.do_action.defaults = { label : function(ele) { return ele.attr('href'); } }; }(jQuery)); </code></pre> <p>and the control is</p> <pre><code>$.getJSON('default.txt' , function(data){ $.set({ actions:data // this si where we pull in the options }); }); </code></pre> <p>I seems to work if I put in an alert inside the anonymous function, so from </p> <pre><code>"label":(function(ele){ return 'test'; })() </code></pre> <p>to</p> <pre><code>"label":(function(ele){ alert('test') })() </code></pre> <p>but after that I can't seem to get it to print right in the console as I only get the message</p> <pre><code>Tracking label:'(function(ele){ return 'test'; }) </code></pre> <p><strong>[EDIT]</strong> Please stop saying that "you should not put code in json". If you work for Google and are their top level programmers getting paid the big super bucks then I'll listen to the why's, but I'm sorry if it's good for them, it'll work for me. Don't know what else to say, I already understand the point of why you should avoid it which I aim to do as well. There are times when you just can't and why i supect Google does the same.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload