Javascript callback on blackberry
I'm working on a mobile project, and I have a script that is dynamically generated on the backend and then appended to the document (the html page). Desktop browsers seem to append the script correctly and call any functions in the script, however, I can't get this to happen on Blackberry phones. I know that the script is being appending because I can alert it after I append it. I开发者_开发知识库 just can get it to call the function in the script.
For example, if I have code like this:
var scriptText = document.createElement('script');
scriptText.type = 'text/javascript';
scriptText.id = 'thisScript';
scriptText.innerHTML = 'alert("hello");';
document.getElementById('idName').appendChild(scriptText);
alert(document.getElementById('thisScript')); //Alerts the script element.
This will alert 'hello' in desktop browsers and even the iPhone/iPodTouch, but not BlackBerry's. Anyone have any idea why? Or if there's a fix/hack?
Javascript support on BB devices is not comprehensive. You can perform basic functions, but advanced features such as DOM manipulation do not have great support. This is true of most mobile devices, Android, iPhone and WebOS excepted. Newer versions of BB devices will address this.
Have you looked into using the jQuery library for javascript? You would merely need to include the jQuery script library:
<script type="text/javascript" src="jquery.js" ></script>
...then dynamically add your alert however you needed to within jQuery's ready() function:
$(document).ready(function() {
$('#header').append('<div class="testButton" onclick="alert(123)">Menu</div>');
//or just like this:
alert('hello 123');
精彩评论