PDA

View Full Version : u won't call it a problem ..



Black Label
07-02-2007, 10:51 AM
hi all,
at first ,, i dont speak english ,, and i don't think that it's a problem :)
and my english is weak ..
........ :
i have a small problem ..
the problem is ....................
hmm i think u'll understand by the code below :) ..

<script>
var mmm = Math.round(Math.random()*27);
var ar = new Array("2", "3", "5", "7", "8", "20", "24", "25", "15", "16",
"17", "18", "10", "13", "26", "28", "30", "31", "36", "37", "39", "40", "41", "42",
"44", "46", "50", "51");
document.write(ar[mmm]);
</script>
this script works 100% .. as u see ..
when u refresh the page u will get a new number from the array ..ok ?

but when we write this :

<form name="dynamicdrive">
<input type="text" name="num"></form>
<script>
var mmm = Math.round(Math.random()*27);
var ar = new Array("2", "3", "5", "7", "8", "20", "24", "25", "15", "16",
"17", "18", "10", "13", "26", "28", "30", "31", "36", "37", "39", "40", "41", "42",
"44", "46", "50", "51");
document.dynamicdrive.num.value = ar[mmm];
</script>
:( it gives a fixed number ..
i don't know whyyy ?????
someone helps me ?
((( i wrote the code in the simplest way :) just to let u understand it )))
....
&&&Black Label&&&

codeexploiter
07-03-2007, 06:12 AM
Try the following code



<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
<style type="text/css">

</style>
<script type="text/javascript">
var mmm = Math.round(Math.random()*27);
var ar = new Array("2", "3", "5", "7", "8", "20", "24", "25", "15", "16", "17", "18", "10", "13", "26", "28", "30", "31", "36", "37", "39", "40", "41", "42", "44", "46", "50", "51");
window.onload = function() {
document.dynamicdrive.num.value = ar[mmm];
}
</script>
</head>
<body>
<form name="dynamicdrive">
<input type="text" name="num" /></form>
</body>
</html>

tech_support
07-03-2007, 07:02 AM
/>
That's for XHTML.
HTML =
>

codeexploiter
07-03-2007, 10:13 AM
I forgot to insert the closing tag, mistake from my part and thanks for pointing that man

Twey
07-03-2007, 05:56 PM
That wasn't the point tech_support was trying to make. You've used an HTML DOCTYPE and even declared Content-Type: text/html using <meta> elements, but at several places in your code you've used XML-style ending tags, such as
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />These are invalid in HTML.*

* well, technically they're valid, although they have a vastly different meaning from that they possess in HTML, but no browser today implements the NET feature of SGML that these require to work.

jscheuer1
07-03-2007, 08:41 PM
The script as written should work, and does in Opera and FF, just not in IE. Using the defer attribute in the script tag, like so:


<form action="#" name="dynamicdrive">
<input type="text" name="num">
</form>
<script type="text/javascript" defer>
var mmm = Math.round(Math.random()*27);
var ar = new Array("2", "3", "5", "7", "8", "20", "24", "25", "15", "16",
"17", "18", "10", "13", "26", "28", "30", "31", "36", "37", "39", "40", "41", "42",
"44", "46", "50", "51");
document.dynamicdrive.num.value = ar[mmm];
</script>

makes it work in IE as well. This really seems to me like a bug in IE though.

djr33
07-04-2007, 02:40 AM
What is "defer"?
(I could look it up, but I'm assuming it might help others too.)

tech_support
07-04-2007, 02:41 AM
I think it makes the script load when the page is loaded.

djr33
07-04-2007, 02:49 AM
Load after the page has loaded?
reload each time the page loads?
something else?

jscheuer1
07-04-2007, 03:38 AM
W3Schools:

defer - Indicates that the script is not going to generate any document content. The browser can continue parsing and drawing the page.

W3C:

defer - When set, this boolean attribute provides a hint to the user agent that the script is not going to generate any document content (e.g., no "document.write" in javascript) and thus, the user agent can continue parsing and rendering.

So, basically it is telling the browser that it can continue drawing the page. However, this should be immaterial in this case, and is in Opera and FF, as all the browser needs to execute the script has already been parsed.

Twey
07-04-2007, 11:28 AM
What actually happens with the defer attribute is that the script is executed when the page, and only the page, not all the images as happens with onload, has loaded. This is the basis of all the DOMContentLoaded workarounds for IE.

Black Label
07-04-2007, 03:07 PM
codeexploiter, tech_support, Twey and jscheuer1 :
thanx alot ..
i knew that dynamic drive is the right place to learn and teach ..
:) :) ..

special thanx to ( Defer Guy :D )
thank u .. your code is the closest and the simlpest ..
and that's wat i want ..