So here’s my script:
function getRandomArrayIndex(source_array) {
return Math.floor(Math.random() * source_array.length);
}
function getRandomArrayEntry(source_array) {
var random_index = getRandomArrayIndex(source_array);
return source_array[random_index];
}
function getRandomV5test() {
var v5tests = [
["This should tell me I have the following amount of minutes: "],
["This is the number of times I want to jump up and down: "],
["This is a quote."],
]; var random_v5test = getRandomArrayEntry(v5tests);
return random_v5test;
}
function displayV5test(v5test) {
var element = document.getElementById("v5test");
if(element !== null){
var TEXT_ROW = 0;
var LINK_ROW = 1;
var v5test_text = v5test[TEXT_ROW];
var v5test_link = v5test[LINK_ROW];
if (v5test_link != null) {
element.innerHTML = '<a href="' + v5test_link + '">' + v5test_text + '</a>';
} else {
element.innerHTML = v5test_text + '<span id="time"></span>';
}
}
}
function generateRandomV5test(){
var random_v5test = getRandomV5test();
displayV5test(random_v5test);
}
What this does is, in combination with multiple sibling scripts, generates a phrase with a random outcome. It runs before other scripts which insert their random phrase into the span id specified (‘time’), and the end result on a page might be something along the lines of:
This should tell me I have the following amount of minutes: 40
The problem is that this gives me no flexibility – the span I specify after ‘v5test_text’ must always come afterwards. In addition, the third array option would still return a random number regardless of what array option was chosen by the script, looking like this:
This is a quote. 25
What I want to be able to do is put the html code inside different array options, like the following:
var v5tests = [
["This should tell me I have <span id="time"></span> minutes. "],
["I want to jump up and down <span id="time"></span> times. "],
["This is a quote."],
So, really it’s a simple question. What syntax do I use if I want to prevent html code used in this instance being confused by the script itself? Knowing this will give me almost unlimited flexibility in terms of the content of my website.
Any help would be very much appreciated! 🙂
Use single quotes to define your strings…
Or Escape your double quotes inside the strings: