jquery - Use one javascript variable into another javascript file

This is common.jsjQuery(function ($) {var commonJs = {init: function () {},showAlert: function () {},}});This is add.jsj

This is common.js

jQuery(function ($) {

    var commonJs = {
        init: function () {
        },
        showAlert: function () {
        },

    }

});

This is add.js

jQuery(function ($) {

    var addJs = {
        init: function () {
            console.log(commonJs.showAlert);
        },
    }

});

I want to use common js function commonJs.showAlert in add.js

How can i access that?

This is common.js

jQuery(function ($) {

    var commonJs = {
        init: function () {
        },
        showAlert: function () {
        },

    }

});

This is add.js

jQuery(function ($) {

    var addJs = {
        init: function () {
            console.log(commonJs.showAlert);
        },
    }

});

I want to use common js function commonJs.showAlert in add.js

How can i access that?

Share Improve this question asked Mar 17, 2020 at 7:01 Shweta DanejShweta Danej 154 bronze badges
Add a comment  | 

2 Answers 2

Reset to default -1

Please simply place commonJs var outside the jQuery function and try again. It will work for you.

var commonJs;
jQuery(function ($) {

    commonJs = {
        init: function () {
        },
        showAlert: function () {
        },

    }

});

I just had to put this variable outside jQuery(function ($) and it worked.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744657869a4586289.html

相关推荐

  • jquery - Use one javascript variable into another javascript file

    This is common.jsjQuery(function ($) {var commonJs = {init: function () {},showAlert: function () {},}});This is add.jsj

    1天前
    50

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信