在inApp浏览器中插入代码,并获取应用程序中的返回值 [英] Inject code in inApp browser and get it's return value in the app
问题描述
我正在写一个phonegap应用程序,它在inAppBrowser中启动一个Web应用程序。
我想从这个网络应用程序获得一些反馈,在我的phonegap应用程序中使用它。
I am writing a phonegap app, that's starting a web app inside an inAppBrowser. I would like to get certain feedback from this web app to use it further in my phonegap app.
因此,用户启动网络应用程序,做一些事情点击一个按钮,web应用程序返回一个值到phonegap应用程序。
So the user starts the web app, does some things there and upon clicking a button, the web app returns a value to the phonegap app.
我想我可以使用inAppBrowser的executeScript方法注入一个函数,将使用某些事件在Web应用程序内部调用,当调用该函数时,在Web应用程序中评估其返回值。
所有我发现是不是很完整的文档的phonegap和这个问题stackoverflow:
使用本机功能扩充webapp - 使用Rails网络应用程序应用程序桥接PhoneGap的InAppBrowser javascript
I thought I could use the executeScript method of the inAppBrowser to inject a function, that would be called inside the web app using some event, and when that function is called, evaluate its return value inside the web app. All I found was the not very complete documentation of phonegap and this question on stackoverflow: Augmenting a webapp with native capabilities - Bridging PhoneGap's InAppBrowser with Rails web app application javascript
这是我的移动应用程序代码
Here is my mobile app code
<!DOCTYPE html>
<html>
<head>
<title>InAppBrowser.executeScript Example</title>
<script type="text/javascript" charset="utf-8" src="cordova-2.7.0.js"></script>
<script type="text/javascript" charset="utf-8">
// Wait for Cordova to load
//
document.addEventListener("deviceready", onDeviceReady, false);
// Global InAppBrowser reference
var iabRef = null;
// Inject our custom JavaScript into the InAppBrowser window
//
function addFeebackFunction() {
iabRef.executeScript(
{code: "var evaluateFeedback = function(){return 'Done';};"},
function(data) {
alert(data);
}
);
//iabRef.close();
}
function iabClose(event) {
iabRef.removeEventListener('loadstop', addFeebackFunction);
iabRef.removeEventListener('exit', iabClose);
}
// Cordova is ready
//
function onDeviceReady() {
iabRef = window.open('http://{ipaddress}/test/', '_blank', 'location=no');
iabRef.addEventListener('loadstop', addFeebackFunction);
iabRef.addEventListener('exit', iabClose);
}
</script>
</head>
<body>
<h1>Mobile App</h1>
</body>
</html>
她是我的网络应用程序代码
And her is my web app code
<!DOCTYPE HTML>
<html>
<head>
<title>
Test
</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<script type="text/javascript" src="./js/jquery-1.8.3.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$('#get-feedback').click(function() {
var feedback = $('#feedback').val();
evaluateFeedback(feedback);
});
});
</script>
</head>
<body>
<div data-role="page">
<article data-role="content">
<h1>Web app</h1>
<input type="text" id="feedback" /><br />
<button type="button" id="get-feedback">Feedback</button>
</article>
</div>
</body>
</html>
推荐答案
只是一个猜测, this function ...
Just a wild guess, but you're not executing this function...
{code:var evaluateFeedback = function(){return'Done';}; evaluateFeedback();}
{code: "var evaluateFeedback = function(){return 'Done';};evaluateFeedback();"}
或更好:
{code:'Done';}
or better: {code: "'Done';"}
这篇关于在inApp浏览器中插入代码,并获取应用程序中的返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!