开发者

Javascript callback on blackberry

开发者 https://www.devze.com 2022-12-26 18:42 出处:网络
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 correct

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');
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号