如何从node.js命令行脚本启动浏览器 [英] How would you launch a browser from the a node.js command line script
本文介绍了如何从node.js命令行脚本启动浏览器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我不知道是否重要,但我在OSX上。
I don't know if it matters, but I am on OSX.
我知道你可以通过输入命令行本身启动浏览器:
I know you can launch a browser from the command line itself by typing:
open http://www.stackoverflow.com
但是有没有办法从nodejs命令行脚本中打开浏览器?
But is there a way to open a browser from inside a nodejs command line script?
推荐答案
var spawn = require('child_process').spawn
spawn('open', ['http://www.stackoverflow.com']);
将打开您当地的网页浏览器到该网站。
Will open your local web browser to that website.
打开节点现在存在,使用它。 :)
Node Open exists now, use that. :)
安装:
$ npm install --save open
使用于:
var open = require('open');
open('http://www.google.com');
您也可以选择特定的浏览器:
You can also select a specific browser:
open('http://www.google.com', 'firefox');
或处理错误回调:
open('http://www.google.com', function (err) {
if (err) throw err;
console.log('The user closed the browser');
});
感谢 @ ForbesLindesay 谁的回答被管理员删除。
Thanks to @ForbesLindesay who's answer got deleted by an admin.
这篇关于如何从node.js命令行脚本启动浏览器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文