views:

150

answers:

2

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?

A: 

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');
DShultz
Loading a big librairy within a mobile phone is not always a good idea.
Michael B.
And only the most advanced devices can handle javascript as sophisticated as jquery.
Kevin
I like jQuery, but it scares me how fast people default to it for the simplest of things. And on a limited platform no less.
Matt
A: 

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.

Kevin