0

I have 2 js files in my project

first.js:

(function () {
    "use strict";

    function konumBul() {

        if (navigator.geolocation) {
            navigator.geolocation.getCurrentPosition(konumGoster, konumHata, {enableHighAccuracy: true});
        } else {
            alert("Geolocation is not supported by this browser.");
        }
    }

    function konumGoster(position) {

        //merkez koordinatları
        window.merkez = L.latLng(position.coords.latitude, position.coords.longitude);
    }
    konumBul();
}());

second.js:

(function () {
    "use strict";

    alert(window.merkez); //undefined
    alert(merkez) //undefned
}());

How can i reach "merkez" variable in second.js? Why the way i used isn't working?

EDIT: I wrote missing sorry about that. I called someFunction() in first.js file

1
  • Where do you execute the inner function in the first file? Commented Dec 26, 2014 at 22:33

3 Answers 3

1

According to your example, someFunction() is never called, therefore window.center won't ever be calculated. Here are two possibilities to fix it by changing first.js to:

(function () {
    "use strict";
    window.center = L.latLng(position.coords.latitude, position.coords.longitude);
}());

or:

(function () {
    "use strict";
    function someFunction() {
        window.center = L.latLng(position.coords.latitude, position.coords.longitude);
    }
    someFunction();
}());

EDIT:

Your issue is that getCurrentPosition() is an asynchronous function. This means that your callback may not execute before the second script is loaded. There are a couple of ways to fix this issue. You can use a Promise implementation or just use a simple timeout loop in your second file such as:

(function () {
    "use strict";
    function doWorkWithResult() {
        if (window.merkez == undefined) {
            setTimeout(doWorkWithResult, 100); //Callback hasn't been called yet, wait 100 ms
        } else {
            alert(window.merkez);
        }
    }

    doWorkWithResult();
}());
Sign up to request clarification or add additional context in comments.

3 Comments

i did it. same results :(
@user3802409 can you provide more information about your code? With what you have provided, there isn't much else I can point you towards.
@user3802409 added more to my answer
0

"someFunction" is never executed. I'm not sure why your code is wrapped in another function though. This will execute immediately

(function(){
     window.center = L.latLng(position.coords.latitude, position.coords.longitude);
})();

Comments

0

Your code looks good and you should be able to access the global center reference from your second file. If your code is still not running correctly, then the only thing that you want to check is the order of importing your references and whether any of your code is async (which doesn't look to be the case). Posting an example of your code woudl help.

With that said, what I would highly recommend you to look into is using an AMD module loader such as RequireJS to organise your code and its dependencies in small modules. It would also get you to avoid polluting the global scope to pass information between self encapsulated modules.

Your code would then be refactored to something as follows:

coord.js

require(['longitude'], function(L){
   var position = ...;

   return {
      center: L.latLng(position.coords.latitude, position.coords.longitude);
   };
});

main.js

require(['coord'], function(Coord){
   var center = Coord.center;
   console.log('center', center);
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.