You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

66 lines
1.3 KiB

5 years ago
  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