abbc0d97324ce50f7a6c61f83d5cba5b8d810eaae006daf1c9ce12fd72b67eadcc86cb592d650716e947fc650bff343fa747aacae4cf311e2ea1889120c1a1 1.3 KB

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