0

I have a JavaScript file: "myCode.js"

This file contains some javascript code:

        var user = JSON.parse(localStorage.getItem("m"));
        d.Stacks.Set.Map = user.layer;
        d.Provider = user.Provider;

        var s = user.Stacks;
        if (s instanceof Array){
            d.geo.ref = user.stacks.name;
        } else {
            d.geo.ref = user.stacks.name;
        }

This code is not in any function.

I have multiple files that use this code and instead of copying and pasting this code into every page i want a way to e.g. make a link to this "myCode.js" and call the code. The other files all have functions which require this piece of code.

e.g

    var d = $.getJSON("main.json", function(d) {
           var user = JSON.parse(localStorage.getItem("m"));
           d.Stacks.Set.Map = user.layer;
           d.Provider = user.Provider;

           var s = user.Stacks;
           if (s instanceof Array){
                d.geo.ref = user.stacks.name;
           } else {
                d.geo.ref = user.stacks.name;
           }
         o.load(d);
     });

what i want to have is:

    var d = $.getJSON("main.json", function(d) {
         //HAVE THE CODE FROM "myCode.js" here
         o.load(d);
     });

Is this possible? if yes, how? :) Thanks in advance.

  • the `if` inside your "some javascript code" is useless. It does the same thing in both cases. Therefore, you can safely replace it with only `d.geo.ref=user.stacks.name;` – tao Dec 01 '16 at 22:54
  • Is there a reason why you can't wrap your first piece of code in a function and then call that function? As long as it is defined in the global scope it should work. See [can-we-call..](http://stackoverflow.com/questions/3809862/can-we-call-the-function-written-in-one-javascript-in-another-js-file) or [calling-a-javascript-function...](http://stackoverflow.com/questions/25962958/calling-a-javascript-function-in-another-js-file) – Michael Goodwin Dec 02 '16 at 00:13

1 Answers1

0

You make a function out of it and put it in any of the js files that your include on every page.

myCode.js

function modifyD(d) {
  var user = JSON.parse(localStorage.getItem("m"));
  d.Stacks.Set.Map = user.layer;
  d.Provider = user.Provider;
  d.geo.ref = user.Stacks.name;
}

Since d is an object and passed in function by reference, you don't really need to return anything.

any-file.html

  <script>
  var d = $.getJSON("main.json", function(d) {
    modifyD(d);
    o.load(d);
  });
  </script>

  ...

  <script src="myCode.js"></script>
i--
  • 4,349
  • 2
  • 27
  • 35
  • Hi there! Thank you for your reply. I have done exactly what you have provided. I have 2 html files 1 has a script function with the "global.js" code. the other has what you provided in the "any-other-file.js" i added a script reference like the follwoing in the page but i get error. error: modify(d) not defined. –  Dec 03 '16 at 09:40
  • the link is not working. How can i link the 2 html pages? They are both located in the same folder next to each other. –  Dec 03 '16 at 09:40
  • You can't include html file within script tag, so `` is wrong. See my modified answer, hopefully it makes more sense. – i-- Dec 04 '16 at 12:53