I am working on a work project that requires a 5*5 table, that will generate random letters in each cell upon button click. I have most of the coding complete with the exception of the letter generation. I am currently generating numbers, I would even be fine with having a RandBetween(and listing the 26 letters of the alphabet) at this point.
Any suggestions?
Code:
<script type="text/javascript">
var random1, random2, random3, random4, random5, random6, random7, random8, random9, random10, random11, random12, random13, random14, random15, random16, random17, random18, random19, random20, random21, random22, random23, random24, random25;
var randomArray = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0];
//generate the random characters
function CreateCellData() {
for (var i=0; i<randomArray.length; i++) {
randomArray[i] = Math.floor(Math.random()*99 + 1);
}
}
jmrker - he asked for the grid to be generated on a button click, so document.write() is sudden death as it erases the page! Suggest the following mods to your script, including mrhoo's suggestion:-
Code:
<html>
<head>
<title> Untitled </title>
</head>
<body onload = "generate()">
<div id = "grid"></div>
<script type="text/javascript">
function generate() {
var str = '<table border="1"><tr>';
for (var i=0; i<25; i++) {
str += '<th>'+randomAlpha()+'</th>';
if ((i % 5) == 4) { str += '</tr><tr>'; }
}
str += '</tr></table>';
document.getElementById("grid").innerHTML = str;
}
function randomAlpha(){
return String.fromCharCode(65+Math.floor(Math.random()*26));
}
</script>
<br><br>
<input type = "button" value = "Generate Another Grid" onclick = "generate()">
</body>
</html>
__________________
All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.
jmrker - he asked for the grid to be generated on a button click, so document.write() is sudden death as it erases the page! Suggest the following mods to your script, including mrhoo's suggestion:-
You're right, he did say button click and I missed that in the initial reading.
You made the exact change I would have suggested if I had read it more closely.
Hmm. I cannot reproduce the example in IE9. </script> terminates the script.
Code:
<div id = "mydiv">xxxx</div>
<script type="text/javascript">
var name = "John";
var el = document.getElementById("mydiv");
el.innerHTML = name; // harmless
// ......
name = "<script>alert('I am John in an annoying alert!')</script>";
el.innerHTML = name; // alerts the text which is annoying and not really what was expected.
</script>
Quote:
If that doesn't bother you, consider that textContent is faster than innerHTML.
To be candid, I do not care a fig if textContent is faster than innerHTML. Any difference would not be discernable.
To quote Douglas Crockford, "IE6 must die! IE7 must die! IE8 must die! IE9 must die!".
Is he a Taliban leader? Perhaps he needs to keep taking his tablets!
As Keynes said, "In the long run we are all dead".
[XP1] - may I very gently and respectfully point out that you are a newcomer to this forum (and we welcome you!), but you will not win friends or influence people if you are too forceful and dogmatic in your opinions, appear to be disagreeable, or are dismissive of long-standing members who have thousands of posts. 'Nuf said, I hope.
__________________
All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.
Hmm. I cannot reproduce the example in IE9. </script> terminates the script.
Result probably depends on the browser.
Quote:
Originally Posted by Philip M
To be candid, I do not care a fig if textContent is faster than innerHTML. Any difference would not be discernable.
It doesn't have to be about performance either.
Using textContent clearly states your intent for you and for other programmers. It is obvious that you are dealing with text. The advantage is in readability.
innerHTML may or may not contain just text. That may be ambiguous to other programmers.
Quote:
Originally Posted by Philip M
To quote Douglas Crockford, "IE6 must die! IE7 must die! IE8 must die! IE9 must die!".
Is he a Taliban leader? Perhaps he needs to keep taking his tablets!
As Keynes said, "In the long run we are all dead".
[XP1] - may I very gently and respectfully point out that you are a newcomer to this forum (and we welcome you!), but you will not win friends or influence people if you are too forceful and dogmatic in your opinions, appear to be disagreeable, or are dismissive of long-standing members who have thousands of posts. 'Nuf said, I hope.
Douglas Crockford is famous, like a "father of JavaScript", and is a big supporter of the language.
If you look at the context of the video, Crockford says that somewhat hesitantly. He wishes it weren't so. In the beginning, he calls IE6 the best browser of its time.
Perhaps, the quotation was much too powerful; in which case, I receive your advice.
"IE6 must die! IE7 must die! IE8 must die! IE9 must die!"
ah, you're one of those. It's a sentiment shared by many here. Personally, I'd prefer that they stopped stubbornly making up their own rules and started adhering to standards, and it appears that that is slowly happening.
But the fact is that IE is not some obscure little browser, and statistics show that IE8 is still more widely used than IE9, so coding things like textContent and DOMContentLoaded will result in somewhere around 15% of users staring at a blank screen, or clicking a button that does nothing.
At which point the question of which is faster, which is more secure, which more clearly states your intent becomes moot because the first job of any code is to render a page, and if it fails to do that it has failed.
You're free to code your pages any way you like, but I think if you are giving out code on a public forum and ignoring something as important as cross-browser compatibility, your code should come with a disclaimer, something like: this code will not work in any version of Internet Explorer, with the possible exception of IE9
(although Philip M's testing puts that last statement in doubt)
At which point the question of which is faster, which is more secure, which more clearly states your intent becomes moot because the first job of any code is to render a page, and if it fails to do that it has failed.
Agreed 100% IE is still the most popular desktop browser in the world. Any page that does not work in IE is useless.
__________________
All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.