Failed to using jQuery in loaded script
- Anton Vityaz
- Posts: 7
- Joined: Mon Jun 22, 2015
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?
$.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?
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();
})
- Евгений Минчев
- Posts: 6
- Joined: Fri Jul 10, 2015
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.
For JQuery library functions like:
(function($) {
$.fn.testFunc = function(options) {
});
}(jQuery));
It's not working.
Could you help me with this issue.
Tnx.
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:
-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);
- Евгений Минчев
- Posts: 6
- Joined: Fri Jul 10, 2015
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?
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?
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();
});
-
- Information
-
Who is online
Users browsing this forum: No registered users and 31 guests