3

Here is the simplified of my code:

// script1.js
$(document).ready(function(){
    .
    .
    function myfunc(){ ... }
    .
    .
});


// script2.js
$(document).ready(function(){
    .
    .
    //doesn't work
    $.getScript("./script1.js");
    // anyway I need to call myfunc() here ... how can I access it?
    .
    .
});

As you can see, I've used $.getScript() to require sccript1.js file, but still myfunc() is not defined there. How can I handle that?

Zakaria Acharki
  • 66,747
  • 15
  • 75
  • 101
stack
  • 10,280
  • 19
  • 65
  • 117
  • define `myfunc` globally by putting `window.myfunc = myfunc;` right after `myfunc` definition. – gurvinder372 Dec 25 '17 at 10:29
  • put my function in a module and import it in both scripts. – Jonas Wilms Dec 25 '17 at 10:30
  • Possible duplicate of [Can we call the function written in one JavaScript in another JS file?](https://stackoverflow.com/questions/3809862/can-we-call-the-function-written-in-one-javascript-in-another-js-file) – Arya Aghaei Dec 25 '17 at 10:31
  • @gurvinder372 Your approach worked .. if you write an answer, I will accept it as accepted one. – stack Dec 25 '17 at 11:02

4 Answers4

1

As you can see, I've used $.getScript() to require sccript1.js file, but still myfunc() is not defined there. How can I handle that?

myfunc is defined inside a document.ready event and is not exposed globally, hence you are not able to use the same in another function which is not inside this document ready event.

You need to either export this function globally or define it outside document.ready event.

Export function globally

$(document).ready(function(){
    .
    .
    function myfunc(){ ... }
    window.myfunc = myfunc; //this line added here
    .
    .
});

Or define it outside

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

    .
    .
});
function myfunc(){ ... }
gurvinder372
  • 66,980
  • 10
  • 72
  • 94
  • Also as a consult with you *(js expert)*, there is a library named *nighmare.js*, do you suggest me to learn it? Is it a great thing compared to react.js ? – stack Dec 25 '17 at 11:14
  • 1
    @stack Sorry, I haven't used either of them before. However, I have heard good things about *nightmarejs*, and if you are in **automation testing domain** then hands-on with this library should be useful for you. – gurvinder372 Dec 25 '17 at 11:20
0

try:

var myfunc=null;
// script1.js
$(document).ready(function(){
    .
    .
    myfunc = function (){ ... }
    .
    .
});


// script2.js
$(document).ready(function(){
    .
    .
myfunc();
.
});
  • 1
    While this code snippet may solve the question, [including an explanation](http://meta.stackexchange.com/questions/114762/explaining-entirely-‌​code-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. – Rosário Pereira Fernandes Dec 25 '17 at 11:06
0

//util.js

var test = (function (){
return {
myfunc : function (){ ... }
}
})();

// script1.js

$(document).ready(function(){
  test.myfunc();
});

// script2.js

$(document).ready(function(){
   // $.getScript("./script1.js");
   test.myfunc()
});

in your html you should put this order

<script src="util.js"></script>
<script src="script1.js"></script>
<script src="script2.js"></script>
zabusa
  • 2,520
  • 21
  • 25
-1

Try to using absolute path. For example https://example.com/js/script1.js instead ./script1.js

Ilya
  • 16
  • 1