JS Getting variables from calling html
I am setting up the following code to allow me decide which JS functions/files to run based on conditional PHP echos to the array given. For some reason I seem unable to retrieve the contents of the array in the Javascript code given:
HTML
<!-- add functions to run at window.onload here -->
<script type="text/javascript">
on_multiload [0] = "message";
on_multiload [1] = "message2";
</script>
<script type="text/javascript" src="http://www.ondesign.org.uk/wp-content/plugins/on-dev-kit/js/pageload.js"></script>
JS
window.onload = function () {
on_mu开发者_开发知识库ltiFunction_load (on_multiload);
}
function on_multiFunction_load (on_multiload) {
for (var i = 0; i< on_multiload.length; i++) {
alert (on_multiload[i]);
}
}
Can anyone see what I've done wrong (first time loading js variables from an external source, so it's likely to be simple)?
You are trying to assign values to properties of on_multiload
while on_multiload
is undefined
.
var on_multiload = [];
on_multiload[0] = "message";
on_multiload[1] = "message2";
or
var on_multiload = [ "message", "message2" ];
精彩评论