如何從 C++ 呼叫 JavaScript 函式?
要從 C++ 呼叫 JavaScript 函式,生成一個呼叫該函式的 js 檔案。網頁將載入 JS 並執行此函式 −
int callId = 0;
void callFunction() {
// the js file
ofstream fout("generate.js");
fout << "caller(" << callId ++ << ", display);";
}
// codeHTML
<html>
<body>
<script>
var callId = 0;
function caller(i, func) {
if (callId < i ) {
callId = i;
func();
}
}
function display() {
alert("my Function called");
}
// load js script
function loadScript(file) {
var myId = "gh";
var e = document.getElementById(myId);
if ( e != null )
e.parentNode.removeChild(e);
var script = document.createElement("script");
script.myId = myId;
script.src = file;
document.body.appendChild(script);
}
window.setInterval(function() {
//path to the js file
loadScript("file:///path/generate.js");
}, 2000);
</script>
</body>
</html>
廣告
資料結構
網路
關係型資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP