0

Is it possible to reference a JavaScript file inside of a JavaScript function?

Therefore i am wanting to convert this:

<script type="text/javascript" src="http://crypto-js.googlecode.com/files/2.5.3-crypto-sha1-hmac.js"></script>
<script type="text/javascript">

var hmacString = Crypto.HMAC(Crypto.SHA1, "Message", "Secret Passphrase", { asString: true });

</script>

In to:

function hmac (input){

  var hmacString = Crypto.HMAC(Crypto.SHA1, "Message", "KEY", { asString: true });

  return hmacString;

}

I am using a tool called Cast Iron, which therefore restricts JavaScript down to only a function, but i need to call an external file, to load the needed functionality.

Is this even possible?

MrBoJangles
  • 12,127
  • 17
  • 61
  • 79
mmkd
  • 880
  • 1
  • 14
  • 26
  • 1
    Confused me... what are you trying to do now ? – jAndy Mar 22 '12 at 16:11
  • @jAndy: I think he's trying to consolidate all his code into one function, so he needs that function to "include" the external JS file, so he can call `Crypto.HMAC`. – gen_Eric Mar 22 '12 at 16:15
  • 1
    Do you mean something [like this](http://stackoverflow.com/questions/5415886/load-js-from-external-site-on-fly)? – Shadow The GPT Wizard Mar 22 '12 at 16:17
  • Cast Iron is a IBM product that allows for connection between cloud and on-premise applications. Basically im trying to get this `src="http://crypto-js.googlecode.com/files/2.5.3-crypto-sha1-hmac.js"` inside of here `function hmac (input){ }`. Can it be done? – mmkd Mar 22 '12 at 16:17
  • 1
    Oh, I see now. My answer below may be invalid then. This is a possible duplicate of [This Post](http://stackoverflow.com/questions/3248384/document-createelementscript-synchronously) – augustknight Mar 22 '12 at 16:18
  • If you look at this image you will understand what i mean. [Image](http://www.uploadimage.co.uk/images/321586Untitled.png) – mmkd Mar 22 '12 at 16:23

3 Answers3

0

If I understand correctly, yes you can access functions and classes from one JS file as long as the other class was loaded before you attempt to access it.

So, if some-javascript-file.js has a function named getThings(), then you can do this:

<script type="text/javascript" src="http://cdn.example.com/js/some-javascript-file.js"></script>
<script type="text/javascript">
    var things = getThings(); // getThings is a publicly accessible function in an external class.
</script>
augustknight
  • 448
  • 2
  • 7
0

OK the screenshot kind of helps. It seems you want something from an external JS file, and to manipulate it inside this function.

So you could have one javascript file with:

var foo = 'foo'; //this is in the global scope

and then your other file has:

function hmac(key){
    alert(document.foo);
}

should access what you want

Chris Sobolewski
  • 12,819
  • 12
  • 63
  • 96
  • That looks great but this is where it gets hard. Cast Iron hard codes the `function hmac (input){}` so it looks like this [Image](http://www.uploadimage.co.uk/images/321586Untitled.png). Which means i cant do what your asking. Does that make sense? – mmkd Mar 22 '12 at 16:22
  • I think I get what you're saying now, and you probably want the `document` keyword, and keep the variables you need in the global scope. – Chris Sobolewski Mar 22 '12 at 16:33
0

Yes, you can load other js files with javascript. Depending on the load state where your function is executed, you may either use

document.write('<script type="text/javascript" src="http://crypto-js.googlecode.com/files/2.5.3-crypto-sha1-hmac.js"'+'><'+'/script>"');
// loads synchronouly and executes
Crypto.HMAC(...); // is available here

Watch out that document.write breaks the whole page once your DOM is loaded. You can also use:

var s = document.createElement("script");
s.type = "text/javascript";
s.src = "http://crypto-js.googlecode.com/files/2.5.3-crypto-sha1-hmac.js";
s.onload = function() {
    // the file should be executed now so
    Crypto.HMAC(...); // is available here
};
document.head.appendChild(s); // load asychronously

See also Load js from external site on fly

Community
  • 1
  • 1
Bergi
  • 630,263
  • 148
  • 957
  • 1,375