我正在使用Node.js编写应用程序。
我要创建的功能之一是打开默认的Web浏览器并导航到特定的URL。
我希望它具有可移植性,以便它可以在Windows / Mac / Linux上运行。
使用open(以前称为opn
),因为它将处理跨平台问题。安装:
$ npm install open
使用方法:
const open = require('open');
// opens the url in the default browser
open('http://sindresorhus.com');
// specify the app to open in
open('http://sindresorhus.com', {app: 'firefox'});
var url = 'http://localhost';
var start = (process.platform == 'darwin'? 'open': process.platform == 'win32'? 'start': 'xdg-open');
require('child_process').exec(start + ' ' + url);
const opn = require('opn')
opn('http://sindresorhus.com') // Opens the url in the default browser
//opn('http://sindresorhus.com', {app: 'firefox'}) // Specify the app to open in
您可能需要使用...的值实现开关
require('os').type()
然后使用spawn("open")
还是spawn("xdg-open")
取决于平台?
恕我直言,最简单,最整洁的方法是使用一个名为openurl的npm包。做一个npm install openurl
。您可以在Nodejs REPL中快速尝试这种方法
require("openurl").open("http://stackoverflow.com/questions/8500326/how-to-use-nodejs-to-open-default-browser-and-navigate-to-a-specific-url")
如果需要的话,您也可以发送电子邮件。
require("openurl").open("mailto:janedoe@example.com")
本文地址:http://javascript.askforanswer.com/ruheshiyongnode-jsdakaimorenliulanqibingdaohangdaotedingurl.html
文章标签:javascript , node.js
版权声明:本文为原创文章,版权归 javascript 所有,欢迎分享本文,转载请保留出处!
文章标签:javascript , node.js
版权声明:本文为原创文章,版权归 javascript 所有,欢迎分享本文,转载请保留出处!
评论已关闭!