0

I am trying to assign variables out of global scope and allow a included file with access to these vars. This is what I have:

<script type="text/javascript">
    (function(){
        var cm_url = "http://test.com/mikelbring/1313-myproducthere";
        var cm_text = "Buy";
        var z = document.createElement("script");
        z.src = "button.js";
        var s = document.getElementsByTagName("script")[0];
        s.parentNode.insertBefore(z, s);
    })();
</script>

The script doesn't have access to the vars. What can I do to give it access to the vars but keep them out of the global scope? Thanks!

mikelbring
  • 1,492
  • 2
  • 13
  • 24

1 Answers1

0

You're using a self-executing function to limit the scope of those variables. At some point, you've got to expose something in the global scope so that the other scripts can access them.

Might I suggest a simple namespace or module pattern:

var MIKEL = {
    cm_url: "http://test.com/mikelbring/1313-myproducthere",
    cm_text: "Buy",
    // etc...
};

...or even something more standard/library-ish, like RequireJS or CommonJS.

Community
  • 1
  • 1
Matt Ball
  • 354,903
  • 100
  • 647
  • 710