Skip to content

Instantly share code, notes, and snippets.

@brikis98
Forked from jakobo/increment.js
Created February 11, 2012 01:04
Show Gist options
  • Save brikis98/1794839 to your computer and use it in GitHub Desktop.
Save brikis98/1794839 to your computer and use it in GitHub Desktop.
var getName = require('name').getName;
exports.hello = function() {
return "Hello " + getName();
};
<script type="text/javascript" src="/js/inject.js"></script>
<script type="text/javascript">
require.setModuleRoot("http://example.com/js/modules");
require.run("program");
</script>
<script type="text/javascript" src="/js/modules/name.js"></script>
<script type="text/javascript" src="/js/modules/hello.js"></script>
<script type="text/javascript" src="/js/modules/program.js"></script>
exports.getName = function() {
return prompt("What's your name?");
};
var hello = require('hello').hello;
alert(hello());
@brikis98
Copy link
Author

@danmilon: fixed, thanks!

@AgostinoSturaro
Copy link

Interesting indeed, but I cannot get it to work.

I downloaded all the files here, plus the inject.js lib from the link in
the getting started guide
https://github.com/linkedin/inject/downloads

I changed require.setModuleRoot("http://example.com/js/modules");
to require.setModuleRoot("/js/modules");
and set up the remaining html for the index page (head and body).

I organized them like this:

|-index.html
|-js
_|-inject.js
_|-modules
___|-program.js
___|-hello.js
___|-name.js

At start I get no message at all.
It would be nice to have an all working .zip
to download and run.

Thanks.

@brikis98
Copy link
Author

@Serpenthelm: open up the console in your browser and see if there are any JS errors. My guess is that your module root isn't being set correctly. For example, if you are testing this code by opening up http://localhost:3000/index.html in your browser, you'll need to update the code to require.setModuleRoot("http://localhost:3000/js/modules");.

@AgostinoSturaro
Copy link

Ok, now it works. I had to rename a folder.

However, I can't get this lib to work offline (without a server).
I'm using Firefox+Firebug, I get no error, just this line
Porthole: Using built-in browser support

Then nothing. No error, no alert.

@brikis98
Copy link
Author

@Serpenthelm: I believe Inject uses XHR to fetch JS files, so you need a valid domain. This means running locally, without a server, won't work, since a file:/// path is probably not going to be recognized as a valid domain. @jakobo could comment more, but I think you'll need to fire up some sort of server (e.g. apache) to test so you can use localhost as a domain.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment