问题:如果我使用带有多行的java String对象,我似乎无法找到javafx webengine触发javascript函数的方法。
如果我在html中输入以下示例:“asd”在输入textArea
中输入“qwe”,然后单击CLICK发送按钮。以下java代码不会运行:
webEngine.executeScript("onRecieveMsg('" + msg + "')");
。
但如果我不在输入textArea
中使用ENTER,它将会起作用。
请帮帮我
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Chat</title>
</head>
<script type="text/javascript">
function onSendMsg(){
var msg = document.getElementById("inputTextArea").value;
document.getElementById("inputTextArea").value = "";
java.onSendMsg(msg);
}
function onRecieveMsg(msg){
var msg = msg;
msg = document.getElementById("outputTextArea").value + msg;
document.getElementById("outputTextArea").value = msg + "\n";
}
</script>
<body>
<h1>Output</h1>
<textarea id="outputTextArea" rows="10" cols="50" readonly></textarea>
<h1>Input</h1>
<textarea id="inputTextArea" rows="4" cols="50"></textarea>
<button id="sendBtn" onclick="onSendMsg()">Send</button>
</body>
</html>
import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.concurrent.Worker.State;
import javafx.scene.Scene;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.stage.Stage;
import netscape.javascript.JSObject;
public class ViewFx extends Application {
//VARIABLES
private WebView webView;
private WebEngine webEngine;
private WebEngineListener webEngineListener;
private JsBridge jsBridge;
//CONTRUCTOR
//METHODS
@Override
public void start(Stage stage) {
webView = new WebView();
webEngine = webView.getEngine();
webEngine.load(getClass().getResource("view.html").toExternalForm());
jsBridge = new JsBridge();
webEngineListener = new WebEngineListener(jsBridge);
webEngine.getLoadWorker().stateProperty().addListener(webEngineListener);
stage.setScene(new Scene(webView));
stage.setWidth(600);
stage.setHeight(700);
stage.show();
}
//INNER CLASS
private class WebEngineListener implements ChangeListener<State> {
//VARIABLES
private JsBridge jsBridge;
private JSObject jsobj;
//CONSTRUCTOR
private WebEngineListener(JsBridge jsBridge) {
this.jsBridge = jsBridge;
}
//METHODS
@Override
public void changed(ObservableValue<? extends State> observable, State oldState, State newState) {
if(newState == State.SUCCEEDED) {
jsobj = (JSObject) webEngine.executeScript("window");
jsobj.setMember("java", jsBridge);
}
}
}
public class JsBridge {
//VARIABLES
//CONTRUCTOR
//METHODS
public void onSendMsg(String msg) {
webEngine.executeScript("onRecieveMsg('" + msg + "')");
}
}
public static void main(String[] args) {
launch(args);
}
}
答案 0 :(得分:0)
多行字符串文字在JavaScript和java中都不起作用。如果收到多行消息,您只需执行无效的JavaScript代码:
收到asd
qwe
执行javascript
onRecieveMsg('asd
qwe')
您需要转义换行符,而不是将它们包含在脚本中。还有一些字符需要引用。您可以使用这样的方法来获取正确引用的字符串:
public static String toJavaScriptString(String value) {
value = value.replace("\u0000", "\\0")
.replace("'", "\\'")
.replace("\\", "\\\\")
.replace("\"", "\\\"")
.replace("\n", "\\n")
.replace("\r", "\\r")
.replace("\t", "\\t");
return "\""+ value+"\"";
}