Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to use NodeJS pop up a alert window in browser

Tags:

node.js

I'm totally new to Javascript and I'm wondering how to use node js to pop up a alert window in browser, after sever(Nodejs) received post message from front end? Do I need to use Ajax?

like image 292
phaneven Avatar asked Jul 05 '17 01:07

phaneven


People also ask

How to handle JavaScript alerts/Popus?

In this post, we see how to handle javascript alerts/popus. Alerts are basically popup boxes that take your focus away from the current browser and forces you to read the alert message. You need to do some action such as accept or dismiss the alert box to resume your task on the browser.

How to handle browser based alerts (web based alert popups)?

To handle Browser based Alerts (Web based alert popups), we use Alert Interface. The Alert Interface provides some methods to handle the popups. While running the WebDriver script, the driver control will be on the browser even after the alert generated which means the driver control will be behind the alert pop up.

What does the alert() method do in JavaScript?

The alert () method displays an alert box with a specified message and an OK button. An alert box is often used if you want to make sure information comes through to the user. Note: The alert box takes the focus away from the current window, and forces the browser to read the message.

How to open a popup in JavaScript?

The syntax to open a popup is: window.open (url, name, params): An URL to load into the new window. A name of the new window. Each window has a window.name, and here we can specify which window to use for the popup. If there’s already a window with such name – the given URL opens in it, otherwise a new window is opened.


1 Answers

"after sever(Nodejs) received post message from front end?" show a pop up in the browser. This can not be done. I assume you want to show a popup in if the post request is success. Because you mention about Ajax, This is how it is done.

in your post router definition in the server do it as follows

router.post('/path', function(req, res){
   //do something
   res.jsonp({success : true})
});

something like this. finally you want to send something form the server to the client. after in the client side javascript file send the post request as follows.

$.ajax({
    url:"/url/is/here",
    method: "POST",
    data : {
        data : "what you want to send",
        put : "them here"
    },
    cache : false,
    success : function (data) {
        // data is the object that you send form the server by 
        // res.jsonp();
        // here data = {success : true}
        // validate it
        if(data['success']){
            alert("message you want to show");
        }
    },
    error : function () {
        // some error handling part
        alert("Oops! Something went wrong.");
    }
});
like image 158
Viran Malaka Avatar answered Oct 20 '22 23:10

Viran Malaka