UNPKG

1.3 kBMarkdownView Raw
1# vm-browserify
2
3emulate node's vm module for the browser
4
5[![Build Status](https://travis-ci.org/browserify/vm-browserify.svg?branch=master)](https://travis-ci.org/browserify/vm-browserify)
6
7# example
8
9Just write some client-side javascript:
10
11``` js
12var vm = require('vm');
13
14window.addEventListener('load', function () {
15 var res = vm.runInNewContext('a + 5', { a : 100 });
16 document.querySelector('#res').textContent = res;
17});
18```
19
20compile it with [browserify](http://github.com/substack/node-browserify):
21
22```
23browserify entry.js -o bundle.js
24```
25
26then whip up some html:
27
28``` html
29<html>
30 <head>
31 <script src="/bundle.js"></script>
32 </head>
33 <body>
34 result = <span id="res"></span>
35 </body>
36</html>
37```
38
39and when you load the page you should see:
40
41```
42result = 105
43```
44
45# methods
46
47## vm.runInNewContext(code, context={})
48
49Evaluate some `code` in a new iframe with a `context`.
50
51Contexts are like wrapping your code in a `with()` except slightly less terrible
52because the code is sandboxed into a new iframe.
53
54# install
55
56This module is depended upon by browserify, so you should just be able to
57`require('vm')` and it will just work. However if you want to use this module
58directly you can install it with [npm](http://npmjs.org):
59
60```
61npm install vm-browserify
62```
63
64# license
65
66MIT