![](/img/trans.png)
[英]How to call a function from index.html to app.js file in node js application javascript
[英]How to invoke app.js function from html page in expressjs?
我想從HTML頁面調用app.js中存在的function init() { // }
<a href='javascript:init();'> invoke init </a>
上面的代碼的問題在於,它僅在客戶端尋找function init()
,而我想在快遞服務器app.js上調用function init()
。
盡管使用相同的語言,但是客戶端和服務器之間沒有直接的聯系。 他們仍然必須與HTTP或WebSockets通信。
因此,您需要建立一個在請求時調用init()
的路由:
app.get('/init', function (req, res) {
init();
// res.send(...), etc.
});
然后讓客戶端導航到它:
<a href="/init"> invoke init </a>
或者,通過Ajax請求:
function init() {
var xhr = new XMLHttpRequest();
xhr.open('GET', '/init');
xhr.send();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.