最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to close window using node.js - Stack Overflow

programmeradmin0浏览0评论

i created node.js script which takes query string from URL and store some variables in the database after inserting the data I need to close the window or current tab using node.js

i created node.js script which takes query string from URL and store some variables in the database after inserting the data I need to close the window or current tab using node.js

Share Improve this question asked Mar 12, 2018 at 12:40 user3768498user3768498 411 silver badge5 bronze badges 4
  • 3 "...using node.js." No, you need to do that using JavaScript on the page that sent the request to your Node.js server. You can't reach out from the server and close the browser window on the client. – T.J. Crowder Commented Mar 12, 2018 at 12:44
  • process.exit() ? (assuming you're talking about a console window sticking around) – Ovidiu Dolha Commented Mar 12, 2018 at 12:44
  • I think you know in the client side when the result of the insert is successful, when it occurs close the window – ZiTAL Commented Mar 12, 2018 at 12:45
  • @OvidiuDolha: From the "query string from URL" part, I assume the Node.js part of this is a server process. – T.J. Crowder Commented Mar 12, 2018 at 12:45
Add a ment  | 

2 Answers 2

Reset to default 7

As NodeJs is a server side language it won't work on client side. So you can rather use following code to close the window. Using express routing:

router.get('/close',(req, res) => {
    res.send("<script>window.close();</script > ")})

Here we are send a script to the client side which runs on the browser and closes the window succesfully.

The HTML page that is being served to the client by your NodeJS server can be closed by using window.close()

You can have this behavior triggered manually by including a button on such HTML page, as well as a function that will execute window.close when the button is pressed.

If you would rather like the NodeJS server to dictate when the window is ready to be closed, you have a couple options:

  • Use a websocket connection for the NodeJS server to instruct the HTML page on the client to execute window.close()

  • Have the HTML page periodically check (perhaps with a GET request) the server to see if the page is ready to execute window.close()

发布评论

评论列表(0)

  1. 暂无评论