Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7708143
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T00:32:08+00:00 2026-06-01T00:32:08+00:00

I’m tring to get some json to contain a function so that the option

  • 0

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.

The set up is as such:

default.txt

    [
        {
            "element":"a[href$='.jpg']",
            "options":{
                "label":"test"
                }
        },{
            "element":"a#hover",
            "options":{
                "label":(function(ele){ return 'test'; })()
            }
        }
    ]

and the plug-in is

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

and the control is

$.getJSON('default.txt' , function(data){
    $.set({
        actions:data // this si where we pull in the options
    });
});

I seems to work if I put in an alert inside the anonymous function, so from

"label":(function(ele){ return 'test'; })()

to

"label":(function(ele){ alert('test') })()

but after that I can’t seem to get it to print right in the console as I only get the message

Tracking label:'(function(ele){ return 'test'; })

[EDIT]
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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T00:32:09+00:00Added an answer on June 1, 2026 at 12:32 am

    Your label member is not a function; it’s a value returned by a function. The final () at the end means “execute this function immediate whenever the value of label is requested and provide its return value as the value of label“.

    That said, if you must include functions in your JSON object (and from what you’ve said, you do) your best bet is to stringify-and-eval your functions. You should also be able to use raw functions in your JSON, like {foo : (function(){ … }) }, though I’ve personally had a tricky time getting this right (but that’s just me — if you can get it right, go for it).

    (As an aside, it’s not possible to stringify functions from existing objects, but if you’re getting your functions as text from user/programmer input, that’s not an issue.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.