Daniele Cruciani
Programmatore Developer PHP/MySQL Freelance
mobile: +39 3489215204

icq skype msn linkedin fb t ff youtube picasa google
seconds to the end of World
End of World 21 December, 2012 11:11:00
Hire me before that day! | don't bother

Tu sei qui

Defer javascript execution using data

    Due to page performance you should load javascript at the end of page (see http://developer.yahoo.com/performance/rules.html), at least all javascript you can defer to load. There are script that have to load at the very begin of page. But are you sure there is no option?

    Well, actually this is a critical reflection on my own script. When I wrote imagedata drupal example module I replaced to {'imagedata ...} a direct call to jquery plugin.

    There are a lot of situation where javascript function is called
    where a data structure could be defined and a "caller function" can use that
    var to fire up function call.

    Also, often execution could/must be deferred in a given order of execution, in order to make things work (have a look at my formhint plugin and problems with facebook like widget). (so defer attribute in script tag is of no use)

    That said, it could be used some structure like:

    <script>var datastruct = {type:'right',sel:"a > img"};</script>
    

    and at the end of page (at a given time):

    <script>
    function functioncall(data){
    $(data.sel).contextmenu(menu,data.type,data.timeout);
    }
    functioncall(datastruct);</script>
    

    Ok, that's work, and it could be used an array too ...

    <script>var datastruct = datastruct || new Array();datastuct.push({type:'right',sel:"a > img"});</script>
    

    (.. with caller function cycling on array this time)

    But it could be used a more elegant (for me) solution: use script type="text/html"

    <script type="text/html" class="mydatas">{type:'right',sel:"a"}</script>
    

    And a caller function can be

    function caller(class){
      $("."+class).each(function(i,el){
        eval('var m='+$(el).html());
        $(m.sel).contextmenu(menu,m.type,m.timeout);
      });
    }
    

    And the time when call function could be defined.

    Inglese