Page 1 of 1

Failed to using jQuery in loaded script

Posted: 22 Jun 2015
by Anton Vityaz
I've add code to load script on the SPForm button OnClick:

$.getScript("somescript.js", function(,,)

{

callMethodFromTheScript();

}

I've use callMethodFromTheScript() from the script library.

Calling this way I have got and error inside callMethodFromTheScript() - "$ is undefined".

How I can correctly import custom script function wich also uses jQuery $ functions?

Re: Failed to using jQuery in loaded script

Posted: 24 Jun 2015
by rostislav
You'll need to define $ and JQuery variables globally, like so:

Code: Select all

window.$ = $;
window.JQuery = $;

//then you can do your stuff
$.getScript("/script.js", function(data, textStatus, jqxhr)
{
    callMethodFromTheScript();
})

Re: Failed to using jQuery in loaded script

Posted: 24 Jun 2015
by Anton Vityaz
Thank you! Works perfect!

Re: Failed to using jQuery in loaded script

Posted: 10 Jul 2015
by Евгений Минчев
Hi. It works perfect only for javascript library.

For JQuery library functions like:

(function($) {

$.fn.testFunc = function(options) {

});

}(jQuery));

It's not working.

Could you help me with this issue.

Tnx.

Re: Failed to using jQuery in loaded script

Posted: 13 Jul 2015
by rostislav
The reason it doesn't work (two reasons, actually):
-you have a problem with brackets
-above we've defined JQuery, not jQuery (as we should have)

Correct version of your code:

Code: Select all

...
window.jQuery = $;
...

//in your script:
(function($) {
  $.fn.testFunc = function(options) {
  }
})(jQuery);

Re: Failed to using jQuery in loaded script

Posted: 20 Jul 2015
by Евгений Минчев
Unfortunately it's noy working.

For example i tried to use datatable grid plugin for prettefy standart grid in form.

But it has be shown for me like TypeError: $(...).dataTable is not a function

my code is


window.$ = $;

window.JQuery = $;

$.getScript("js/jquery.dataTables.min.js", function(data, textStatus, jqxhr){});

$('table').dataTable();


Any ideas?

Re: Failed to using jQuery in loaded script

Posted: 20 Jul 2015
by Евгений Минчев
for mor information - DataTable plugin

Re: Failed to using jQuery in loaded script

Posted: 20 Jul 2015
by rostislav
So, as I've posted above, you need to change JQuery to jQuery. Also, your function needs to be inside the success function. Try this code:

Code: Select all

window.$ = $;
window.jQuery = $;
$.getScript("js/jquery.dataTables.min.js", function(data, textStatus, jqxhr){
    $('table').DataTable();
});