简体   繁体   中英

How to display an alert in ejs file in nodejs

I would like to access a document object and want to use an alert for example.

So I've created a function in the ejs file and I want to show an alert when I have an error. So I am rendering my ejs file and give 2 params, one is the result and the other one is an error. and want to show if there is an error, show an alert.

 res.render('appandfeature',{data: apps, error:error});

and my ejs file

<% if(error!=null) 
showAlert(error.message);

%>  

showAlert = function(err) {

  alert('error: ' + err);
}
%>

As can be seen, I am calling alert and I have an error that is 'alert is not defined'.

I know it is server-side stuff. The question is how can I call an alert in ejs? I do not want to do something from ajax. If I can solve this problem in ejs, it would be great.

you can access EJS variable inside script tag :

<script type="text/javascript">
var error = <%= error %>;          

if(error!=null) 
{  
  showAlert(error.message);
}


showAlert = function(err) {
  alert('error: ' + err);
}
</script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM