android webview 同一JS代码,在eclipse里编译运行没有问题,而在android studio上编译就会有问题

丶一意孤行 发布于 2015/06/19 09:39
阅读 1K+
收藏 0

android webview 加载同一JS代码,用eclipse里编译没有问题,而用android studio编译运行就会出问题。

webview加载JS代码时的错误日志:"Uncaught SyntaxError: Unexpected token var"。

这是怎么回事?如何解决?求大神解答,谢谢

加载中
0
新一jun
新一jun
明显提示js语法错误了,都不把你的代码贴出来,神仙才知道你哪里错啊,这年头的问问题方式呵呵`
0
丶一意孤行
丶一意孤行

引用来自“act262”的评论

明显提示js语法错误了,都不把你的代码贴出来,神仙才知道你哪里错啊,这年头的问问题方式呵呵`

谢谢。JS代码如下,麻烦帮忙看下了,非常感谢。

javascript:;(function() {
	if (window.WebViewJavascriptBridge) { return }

	var messageHandlers = {}
	var responseCallbacks = {}
	var uniqueId = 1

	function init(messageHandler) {
		if (WebViewJavascriptBridge._messageHandler) { throw new Error('WebViewJavascriptBridge.init called twice') }
		WebViewJavascriptBridge._messageHandler = messageHandler
	}

	function send(data, responseCallback) {
		_doSend({ data:data }, responseCallback)
	}
	
	function registerHandler(handlerName, handler) {
		messageHandlers[handlerName] = handler
	}
	
	function callHandler(handlerName, data, responseCallback) {
		_doSend({ handlerName:handlerName, data:data }, responseCallback)
	}
	
	function _doSend(message, responseCallback) {
	console.log("responseCallback:"+responseCallback);
		if (responseCallback) {
			var callbackId = 'cb_'+(uniqueId++)+'_'+new Date().getTime()
			responseCallbacks[callbackId] = responseCallback
			message['callbackId'] = callbackId
			}
				console.log("sending:"+JSON.stringify(message));
		_WebViewJavascriptBridge._handleMessageFromJs(encodeURIComponent(JSON.stringify(message.data))||null,message.responseId||null,
		    message.responseData||null,message.callbackId||null,message.handlerName||null);

	}

	function _dispatchMessageFromJava(messageJSON) {
			var message = JSON.parse(messageJSON)
			var messageHandler
			
			if (message.responseId) {
				var responseCallback = responseCallbacks[message.responseId]
				if (!responseCallback) { return; }
				responseCallback(message.responseData)
				delete responseCallbacks[message.responseId]
			} else {
				var responseCallback
				if (message.callbackId) {
					var callbackResponseId = message.callbackId
					responseCallback = function(responseData) {
						_doSend({ responseId:callbackResponseId, responseData:responseData })
					}
				}
				
				var handler = WebViewJavascriptBridge._messageHandler
				if (message.handlerName) {
					handler = messageHandlers[message.handlerName]
				}
				try {
					handler(message.data, responseCallback)
				} catch(exception) {
					if (typeof console != 'undefined') {
						console.log("WebViewJavascriptBridge: WARNING: javascript handler threw.", message, exception)
					}
				}
			}
	}
			

	function _handleMessageFromJava(messageJSON) {
		_dispatchMessageFromJava(messageJSON)
	}

	//export
	window.WebViewJavascriptBridge = {
		init: init,
		send: send,
		registerHandler: registerHandler,
		callHandler: callHandler,
		_handleMessageFromJava: _handleMessageFromJava
	}

	//dispatch event
	var doc = document;
    var readyEvent = doc.createEvent('Events');
    readyEvent.initEvent('WebViewJavascriptBridgeReady');
    readyEvent.bridge = WebViewJavascriptBridge;
    doc.dispatchEvent(readyEvent);
})();



返回顶部
顶部