jQuery.getScript() fails to load function

前端 未结 2 1476
迷失自我
迷失自我 2021-01-26 18:38

I am trying to load a script with a function like:

$.getScript(\'/js/mymy.js\').done(function(){ 
    if(readCookie(\'my_cookie\', \'yes\')){
        /* do sth h         


        
相关标签:
2条回答
  • 2021-01-26 18:52

    readCookie is undefined because readCookie is not global; it is visible only within the document.ready function scope in mymy.js.

    Make the function global by removing the document.ready wrapper.

    0 讨论(0)
  • 2021-01-26 19:07

    From the docs:

    The callback is fired once the script has been loaded but not necessarily executed.

    In other words, you cannot rely on the success function to be called explicitly after the loaded script is executed, e.g., its functions put into the global scope.


    After your edit (which IMO should have been what you posted first) you actually have two issues; as written it's scoped only inside JQ's DOM-ready function.)

    0 讨论(0)
提交回复
热议问题