0

I need a solution for managing js files in my project.

Is there any way that i can reference a js file like this in my html?

<script>
   var someName = use("somejsfile.js")
</script>

And to use it like this

<input onclick="someName.saveThisInput()">

Currently the js file in my project is made like this:

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

var abc = "some global var";
// a lot of other globals

function saveThisInput(){}
function saveThisInput1(){}
function saveThisInput2(){}

EDIT

I can't make any changes to the preexisting js files so require.js won't work. My goal is to merge two or more such js files having name conflicts

  • Possible duplicate of [How to manage client-side JavaScript dependencies?](http://stackoverflow.com/questions/12893046/how-to-manage-client-side-javascript-dependencies) – Aprillion Feb 07 '16 at 14:29
  • What's the ultimate purpose? In what environment/context are you trying to load individual files? What's the problem you're trying to solve? – Dave Newton Feb 07 '16 at 14:29
  • The js files i am supposed to work with are a mess. They are giant ugly beasts that i have to work with. I want namespaces so that my current module will use the namespace object instead of the global variables coming from those files. – Anshu Srivastava Feb 07 '16 at 14:35
  • @AnshuSrivastava fix those "ugly beasts" files so they do everything in their own closure and explicitly set things needed to be externally referencable `;(function () { /* do stuff */ ; if(!window.fileNameSpace) window.fileNameSpace = someGeneratedObject;}());` – Paul S. Feb 07 '16 at 14:47
  • I simply don't have that kind of time and that would confuse other people. – Anshu Srivastava Feb 07 '16 at 14:56
  • if you want to load a script after then try [jQuery.getScript](https://api.jquery.com/jquery.getscript/), otherways I'm sorry. – Sainan Feb 07 '16 at 15:04

1 Answers1

0

you can use namespace in js edit the js file to look like:

var hello = {

  world: function() {
      alert("hello world!");
  },

  foo: function() {
      alert("foo");
  }
};

and use it like:

hello.world();
Abdo
  • 322
  • 6
  • 15