views:

60

answers:

4

What would be the best way to have preloaded data in html and then used with javascript. I do not want to use variables in javascript or save an ajax query.

It may be an XML data into a hidden div? .

Example:

1; Europe
2; Latin America

------------------

2, 1, Argentina
2, 2; Brazil
2, 3, Chile
1, 4, France
1, 5, Spain
1, 6; Italy

He would have to load the data into a select depending on the country you select. are much data and need to be fast, I prefer to have it preloaded.

EDIT. Solution, based on the proposed solutions:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
<html xmlns="http://www.w3.org/1999/xhtml"&gt;
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"&gt;&lt;/script&gt; 
<title></title>
<script type="text/javascript">
$(function(){

    $_save_data = $("<div>");

    $("#xselect optgroup").appendTo($_save_data);

    $("#buttonAddA").click(function(){
     $("#xselect").empty();
     $_save_data.find("optgroup[id='a'] option").clone().appendTo("#xselect");
    });

    $("#buttonAddB").click(function(){
     $("#xselect").empty();
     $_save_data.find("optgroup[id='b'] option").clone().appendTo("#xselect");
    });

});
</script>
</head>
<body>
<button id="buttonAddA">Add A</button>
<button id="buttonAddB">Add B</button>
<select id="xselect">
    <optgroup id="a">
     <option>a1</option>
     <option>a2</option>
     <option>a3</option>
     <option>a4</option>
     <option>a5</option>
    </optgroup>
    <optgroup id="b">
     <option>b1</option>
     <option>b2</option>
     <option>b3</option>
     <option>b4</option>
     <option>b5</option>
    </optgroup>
    <optgroup id="c">
     <option>c1</option>
     <option>c2</option>
     <option>c3</option>
     <option>c4</option>
     <option>c5</option>
    </optgroup>
</select>
</body>
</html>
+1  A: 

I've done the opposite thing in my project - I loaded full select, and then saved it's content with JavaScript and only inserted what I wanted. That way users without JavaScript (or users waiting for JavaScript to load) can use my form.

MBO
+1  A: 

I wouldn't recommend doing it this way, but if you have no other options: you could have the following HTML:

<!-- header above this line -->
<div id="data" style="display:none">
  1; Europe
  2; Latin America

  ------------------

  2, 1, Argentina
  2, 2; Brazil
  2, 3, Chile
  1, 4, France
  1, 5, Spain
  1, 6; Italy
</div>
<!-- footer below this line -->

Then use script like the following to retrieve the information within the data div:

var dataDiv = document.getElementById('data');
var dataText = dataDiv.innerHTML;
// do something to parse dataText....

I think a better way to solve the problem would be to use your server-side language/framework (if you're using one) to inject a JavaScript object into a script element in the <head> of the page as it's being rendered before delivery. This way you don't have to do any funky parsing like with my example above.

JasonWyatt
A: 

I'm curious about the prohibition against just declaring a JS variable, but an alternative would be to build a JSON string on the server side and stick it into, say, the value attribute of a hidden form field:

<input type="hidden" id="some-id" value="{ abc: 123, ... }" />

and then...

<script type="text/javascript">
  // assuming you have some kind of JSON parser available
  var values = JSON.eval(document.getElementByID('some-id').value);

  // do something with values
</script>

MBO's solution is viable as well.

Jordan
A: 

I agree with Jordan that JSON is the way to go, but there's a much easier way to use it than an input element with a massive JSON object as it's value: declare the JSON object in script tags:

var continents = {
1 : "Europe",
2 : "Latin America"
};
var countries = {
1 : {
1 : "France",
2 : "Spain"
},
2 : {
1 : "Brazil",
2 : "Argentina"
}
};

I'm not sure that I have the format right, but I think I'm close. To fetch the data, you could simply access the objects:

alert(continents[1]); // Europe
alert(countries[2][1]); // Brazil

Just make sure that the page has loaded before you try to access the data.

Evan Kroske
I agree entirely, but the OP said, "I do not want to use variables in javascript." I took that to mean that he doesn't want to declare a JS variable by generating it on the server side, as you do in your answer, but it's unclear why he wouldn't want that since, as you point out, it's the most straightforward solution.
Jordan