8000 0.3.x Getting Started · LinkedInAttic/inject Wiki · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

0.3.x Getting Started

Jakob Heuser edited this page Jul 20, 2012 · 1 revision

Getting Started With Inject

This page is designed to get you up and running with the latest version of Inject. For greater detail, there is an Advanced Usage section, and a guide to the API.

Download Inject

The latest version of inject is always available via the source repository at https://github.com/linkedin/inject/downloads. The most recent version is usually at the top, and inside is the required JS and optional HTML files. Copy them to your own server.

Adding Inject to Your Page

This walkthrough is assuming you're using a directory called js which contains all of your javascript, possibly even this file. It also assumes inside of the js directory is a modules directory which will contain all of your modules. Your directory layout might look like the following:

|-index.html
|-relay.html (optional)
|-js
    |-inject.js
    |-modules
      |-math.js
      |-increment.js
      |-program.js

The location of the modules directory does not need to be under the inject.js file, but it's common practice to group files of similar types together such as JavaScript.

Starting Your JavaScript

To use inject, place the following script tags into the <head> of your document

<script type="text/javascript" src="inject.js"></script>
<script type="text/javascript">
  require.setModuleRoot("http://example.com/js/modules");
  require.run("program");
</script>
  • require.setModuleRoot is the location of ALL your JS modules. Based on the directory structure above, they are located in the js/modules directory.
  • require.run executes your main entry point, whatever it may be. Given the above directory structure, it will run the program.js file in your module root (from require.setModuleRoot). The .js is added automatically.

Writing Some Modules

When you're ready to write your own modules, have a look at the CommonJS Module Examples to get started.

Next Steps

From here, you can...

  • learn advanced syntax such as require.addRule() for custom module routing
  • do additional asynchronous calls with require.ensure()
  • set up cross-domain functionality with require.setCrossDomain()
  • use AMD-compliant modules with define()

Visit the Advanced Usage Guide for additional API information.

Clone this wiki locally
0