开发者

Safari generates "Can't find variable" error although it is already defined in another Javascript page

开发者 https://www.devze.com 2023-01-23 09:48 出处:网络
The JavaScript script works with Chrome and Firefox but not in Safari. The code is: $(document).ready(function(){

The JavaScript script works with Chrome and Firefox but not in Safari. The code is:

  $(document).ready(function(){
     $(window).load(function() {
        myVariable.start();
     });

This generates:

  ReferenceError: can't find variable: myVariable  

The variable is defined in another JavaScript page that is included in this p开发者_开发知识库age, but for some reason Safari doesn't see the definition in the other page. Is Safari executing this script without loading the page that the variable is defined in?

How can I fix this?

Thanks for any help


Try to avoid setting global variables. Maybe try assigning your variable to the window object, on top of the page:

window.myVariable = { start: function() {} };

Then when you need it:

$(window).load(function() {
    window.myVariable.start();
 });
0

精彩评论

暂无评论...
验证码 换一张
取 消