简体   繁体   English

如何从节点js app.post()调用html内的javascript函数?

[英]How to call a javascript function inside html from node js app.post()?

I have to call a javascript function which is present in html file from a nodejs app.post() . 我必须从nodejs app.post()调用html文件中存在的javascript函数。 I have to call openup() as soon as the request goes to app.post(/process-view-dnd-registration) . 请求转到app.post(/process-view-dnd-registration)我必须立即调用openup() app.post(/process-view-dnd-registration) The html file here contains the design for the popup that is to be displayed. 此处的html文件包含要显示的弹出窗口的设计。 I tried calling the function using popup.openup() . 我尝试使用popup.openup()调用该函数。 But it didn't work. 但这没有用。 Please find my code below. 请在下面找到我的代码。

server.js server.js

app.post('/process-view-dnd-registration',function(request,reply){
    popup.openup();
    var json_blob = ({
    "header" : {
    "event_const" : "CNST_PREFERENCE_REGISTRATION_EVENT"
    },
    "jsonblob" : {    
    "MobileNo":request.body.hidMobile,
    "SubscriberPrefCategory":request.body.ServiceProvider,
    "PreferenceDate": today,
    "PreferenceStartTime":request.body.datetimepicker3,
    "PreferenceEndTime":request.body.datetimepicker4
    }
    });
    request.body = json_blob;
    route.invoke(request,reply);
    reply.write('<script>setTimeout(function () { window.location.href = "/view-dnd-registration.html"; }, 3000);</script>');
})

popup.html: popup.html:

<html>
   <head>
   </head>
   <body>
      //contains the design for popup
      <script>
         function open_popup(){
         alert("calling open_popup");
         $("html, body").animate({ scrollTop: 0 }, "slow");
         openpopup(); 
         }
      </script>
      <script>
         $("html, body").animate({ scrollTop: 0 }, "slow"); 
         function openpopup()
         {
         alert("calling open popup function");
         var id = '#dialog';
         //Get the screen height and width
         var maskHeight = $(document).height();
         var maskWidth = $(window).width();
         //Set heigth and width to mask to fill up the whole screen
         $('#mask').css({'width':maskWidth,'height':maskHeight});
         //transition effect
         $('#mask').fadeIn(500);
         $('#mask').fadeTo("slow",0.9);
         //Get the window height and width
         var winH = $(window).height();
         var winW = $(window).width();     
         //Set the popup window to center
         $(id).css('top',  winH/2-$(id).height()/2);
         $(id).css('left', winW/2-$(id).width()/2);
         //transition effect
         $(id).fadeIn(2000);
         }
      </script>
   </body>
</html>

Please help me how to call the function openup(); 请帮我如何调用函数openup(); in node js. 在节点js中。

You can't do this like that. 你不能那样做。 There are two different ways: 有两种不同的方式:

  • Using Ajax: in your HTML page, where you have your form, you could use the Ajax request to call your /process-view-dnd-registration route. 使用Ajax:在具有表单的HTML页面中,可以使用Ajax请求来调用/process-view-dnd-registration路由。 Then, you will handle the response and call the open_popup() function directly in your page. 然后,您将处理响应并直接在页面中调用open_popup()函数。

Something like that: 像这样:

$("#formId").submit(function(e) {
  e.preventDefault();

  var formData = {
     'test': $('input[name=test]').val()
     // All your input values
  };

  $.ajax({
     url: '/process-view-dnd-registration',
     type: 'POST',
     dataType: 'json',
     data: formData
  }).done(function(data) {
     // Call your open_popup() function here
  });
});
  • Handle response in your template: you can send in your app.post method a response when you call your popup.html template, and send it parameters, like a boolean variable which could be openPopup , and then handle it in your template page to determine whether or not to display the popup. 处理模板中的响应:调用popup.html模板时,可以在app.post方法中发送响应,然后将其发送给参数,例如布尔变量(可能为openPopup ,然后在模板页面中进行处理以确定是否显示弹出窗口。 It depends on how you use templates in your project. 这取决于您如何在项目中使用模板。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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