prem prem - 3 months ago 35
jQuery Question

Dynamically adding a script/function block in jquery


Just wondering if is it possible to add a function (or static script) block or a variable to body of html.

Something like this,





$('body').fn.myFunc = function(){}

Is it correct to use .extend instead?

One example is if I use ExternalInterface, my callbacks have to be statically defined in body/html/js but not in $().ready function unless I have a var defined globally and refer it in $().ready function.

I overlooked this requirement, and thats where I wanted to add dynamic callback functions.

  • To create a script tag in your markup

    $('<script/>', {
        type:    'text/javascript',
        src:     'http://...'
  • To extend jQuery use

    $.fn.yourmethodname = function(){