How to download jqtouch on demand

I am trying to load jqtouch on request like this:

<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript">
    $(function() {
        $.getScript("js/jqtouch.min.js", function() {
             $.jQTouch();
        });
    });     
</script>

Firebug outputs: $ (_ 3c.selector) .tap is not a function

If I included jqtouch.min.js in the script, as I did for jquery.js and called $ .jQtouch, everything will work correctly. However, I would like to download jqtouch only when I need it, however I cannot get it to work. I also tried to make ajax post in jqtouch.min.js and got the same error.

+3
source share
1 answer

When you load a jqtouch script upon request, the $ (document) .ready event fires before the script loads, so the jqtouch script initialization is never executed.

.... jqtouch script

$(document).ready(function(){...})

$(document).bind('ready',function(){...})

... , script

<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript">
    $(function() {
        $.getScript("js/jqtouch.min.js", function() {
             $(document).trigger('ready');
             $.jQTouch();
        });
    });     
</script>
+2

Source: https://habr.com/ru/post/1739104/


All Articles