I have a multipage design with jquery mobile. On the first site i call a function with onklick() to fill a global array with values. Now i want to display this values on the second site, but i can't manage to display them with document.write(array)
Asked
Active
Viewed 3,156 times
0
-
when you say "site" are you referring to a page (data-role="page")? – Jon Wells Apr 12 '12 at 16:24
-
yes. i think i have to start the javascript somehow when the page (data-role="page") is loaded. Because the global array should be visible because the javascript file is linked to the hole html file ?! – AdrianoCelentano Apr 12 '12 at 16:35
1 Answers
0
To append data to the DOM you first need to select an element to append it to, then in your case iterate through the array, adding each index's value to the DOM:
//here is the array of values
var myArr = [...];
//wait for out page to initialize
$(document).delegate('#my-page-id', 'pageinit', function () {
//create an array to buffer the output
var output = [];
//use a fast loop to add the value at each index to an array,
//in this case I'm adding some HTMl markup to it as well
for (var i = 0, len = myArr.length; i < len; i++) {
output.push('<li>' + myArr[i] + '</li>');
}
//check to see if there were any indexes in the array
if (output.length) {
//append a list-view widget with the info from myArr to the content section of the current page
$(this).children('.ui-content').append('<ul data-role="listview">' + output.join('') + '</ul>').trigger('create');
}
});
Documentation for document.write
: https://developer.mozilla.org/en/document.write
Here is a JSPerf to show the performance difference between some loops: http://jsperf.com/jquery-each-vs-for-loops/2

Jasper
- 75,717
- 14
- 151
- 146