Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 08-19-2008, 01:39 AM   PM User | #1
Deacon Frost
Regular Coder

 
Deacon Frost's Avatar
 
Join Date: Feb 2008
Location: Between the Lines
Posts: 279
Thanks: 31
Thanked 4 Times in 4 Posts
Deacon Frost is on a distinguished road
Lightbulb Percentage timing randomizer

This part is solved, post #17 has the next issue! Please read that !




Alright, I have been searching as hard as I can, and trying to figure out how to compile this all. But I really can't figure it out.

I'll include below what I have, but here's what I need:

I need a script that generates a random number based on a percentage chance. Lets say a person has a 10% chance to get a 100, and in order for the script to return true the number must be 100 or greater. So I need it to generate the number based on that person's 10% chance. Not only that, but if it returns false, I need to add 2% to the person's chance, and redo it until it returns true, or it is stopped. It needs to wait 2 seconds in-between each new roll.

So I have all the scripts I need, I'm positive, they all come from w3schools, or various places. They each do the function separately, but I'm getting a headache trying to figure out how to combine them.


Code:
function generate(){
var randomnumber=Math.floor(Math.random()*101)
}



if (chance => '100')
{
document.write("You successfully got the log.");
}
else
{
+2%
}


setTimeout("document.getElementById('txt').value='2 seconds!'",2000);


<script type="text/javascript">
for (i = 0; i <= 5; i++)
{
document.write("The number is " + i);
document.write("<br />");
}
</script>



<script type="text/javascript">
var i=0;
while (i<=10)
{
document.write("The number is " + i);
document.write("<br />");
i=i+1;
}
</script>



function() {
   if(number == 100) {
      return true
   } else {
      return false +2 chance
   }

});
The bottom one is basically what is gonna output, the others are just the randomizing/checking, etc.

I'm going to be putting this on a form of sorts, and it'll just output text, which the return true will do. But it'll basically be called upon through php, if that helps.


. My head hurts.


Thanks if anyone can help, ask questions, and I'll answer to the best I can.

Last edited by Deacon Frost; 08-20-2008 at 03:40 AM..
Deacon Frost is offline   Reply With Quote
Old 08-19-2008, 05:54 AM   PM User | #2
Trinithis
Regular Coder

 
Join Date: Jun 2007
Location: USA
Posts: 527
Thanks: 26
Thanked 74 Times in 72 Posts
Trinithis will become famous soon enough
What is the valid range of random numbers? Got an an equation on how to generate them as well?
Trinithis is offline   Reply With Quote
Old 08-19-2008, 06:10 AM   PM User | #3
Trinithis
Regular Coder

 
Join Date: Jun 2007
Location: USA
Posts: 527
Thanks: 26
Thanked 74 Times in 72 Posts
Trinithis will become famous soon enough
Something like this?

Code:
function genRand(chance) {
  var rand = chance >= Math.random() * 100
    ? Math.random() * @@@ + 100 // change @@@ to any non-negative number that suits you
    : Math.random() * 101
    ;
  if(rand >= 100)
    alert("Your number is " + rand);
  else
    setTimeout(function() {
      genRand(chance + 2);
    }, 2000);
}
Trinithis is offline   Reply With Quote
Old 08-19-2008, 06:11 AM   PM User | #4
Deacon Frost
Regular Coder

 
Deacon Frost's Avatar
 
Join Date: Feb 2008
Location: Between the Lines
Posts: 279
Thanks: 31
Thanked 4 Times in 4 Posts
Deacon Frost is on a distinguished road
Quote:
Originally Posted by Trinithis View Post
What is the valid range of random numbers? Got an an equation on how to generate them as well?
0 - 100

And not really, I think that's what's mainly confusing me. Generally I take other scripts to cut em up, combine em, splice em, etc... ... so actually doing from scratch isn't my best ability.


I keep wanting to go about it in php:


$chance * randomnumber
if(this <= 100) { fail } if(this >= 100) { WIN }


Or summat...

But basically the problem is I need it to randomly generate a number based on the percentage chance. So say again that i have a 10% chance at getting 100, 90% of the time I will get =<99.

Then for every time it returns false, it adds 2% chance to the 10%.

So:

1st Try: 10%
2nd Try: 12%
3rd Try: 14%


And so on. But in-between each new try needs to be a 2 second display :P.
Deacon Frost is offline   Reply With Quote
Old 08-19-2008, 06:42 AM   PM User | #5
Arbitrator
Senior Coder

 
Arbitrator's Avatar
 
Join Date: Mar 2006
Location: Splendora, Texas, United States of America
Posts: 2,895
Thanks: 5
Thanked 187 Times in 184 Posts
Arbitrator is on a distinguished road
Quote:
Originally Posted by Deacon Frost View Post
I need a script that generates a random number based on a percentage chance. Lets say a person has a 10% chance to get a 100, and in order for the script to return true the number must be 100 or greater. So I need it to generate the number based on that person's 10% chance. Not only that, but if it returns false, I need to add 2% to the person's chance, and redo it until it returns true, or it is stopped. It needs to wait 2 seconds in-between each new roll.
I’ve created a demo that does what I believe that you requested (though the output isn’t identical).

Edit: It looks like there was an error in the code. The lines reading var delay = window.setInterval(draw_number, 2000); should read:

Code:
if (r !== 100) {
	var delay = window.setInterval(draw_number, 2000);
}
I’ve rectified this issue in the posted and live code.

Live Example:
HTML with PHP:
PHP Code:
<?php
    ob_start
();
    
header("Content-Type: text/html; charset=UTF-8");
    
header("Content-Script-Type: application/ecmascript");
    
header("Content-Style-Type: text/css");
?>
<!doctype html public "-//W3C//DTD HTML 4.01//EN">

<html lang="en-Latn">
    <head>

        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <meta http-equiv="Content-Script-Type" content="application/ecmascript">
        <meta http-equiv="Content-Style-Type" content="text/css">
        <title>Demo for CodingForums.com Thread 146939 (HTML)</title>
        <meta name="Author" content="Patrick Garies">
        <meta name="Created" content="2008-08-18">
        <meta name="Revised" content="2008-08-19">
        <style type="text/css" media="all">
            * { margin: 0; padding: 0; }
            html, h1, h2 { font: 16px/1.2 sans-serif; }
            html { background: #ded url("bg_c.png"); color: black; }
            h1 { opacity: 0.8; padding: 2em; background: white; font-weight: bolder; }
            a { color: black; }
            h2, ul { border: 0.5em solid; }
            h2 { margin: 0 1em; border-width: 0 0 0.5em 0.5em; padding: 1em; font-weight: bolder; }
            ul, li { padding: 0.5em; }
            ul { margin: 0 1em; border-width: 0 0.5em 0.5em 0; }
            li { list-style: inside decimal; }
            li p { display: inline; }
            *#no_script { margin: 1em; padding: 1em; background: red; color: palegoldenrod; font-weight: bolder; }
            *#no_script a { color: palegoldenrod; }
        </style>
        <!--[if IE]>
            <style type="text/css" media="all">
                h1 { zoom: 1; filter: alpha(opacity=80); }
            </style>
        <![endif]-->

    </head>
    <body>

        <h1>Demo for <a href="http://www.codingforums.com/showthread.php?t=146939">CodingForums.com Thread 146939</a></h1>
        <p id="no_script">ECMAScript must be enabled in your browser for this document to work properly. Please enable it or obtain an ECMAScript&#x2010;capable browser such as <a href="http://www.mozilla.com/firefox/">Mozilla Firefox</a>.</p>

        <!--[if !IE]>-->
        <script type="application/ecmascript" defer="defer">
            var d = document;
            var r = 0; // random number
            var percent_chance_of_100 = 10; // initial 10% chance of 100 and 90% chance of 1 through 99
            var p_element = d.getElementById("no_script");
            var h2_element = d.createElement("h2");
            var ul_element = d.createElement("ul"); // appended later to maintain HTML 4.01 validity
            h2_element.appendChild(d.createTextNode("The Results"));
            p_element.parentNode.replaceChild(h2_element, p_element);
            function output(result) {
                var li_element = d.createElement("li");
                var p_element = d.createElement("p");
                var percent_chance = "";
                if (result === 100) {
                    percent_chance = percent_chance_of_100.toString();
                }
                else {
                    percent_chance = ((100 - percent_chance_of_100) / 99).toFixed(2);
                }
                var message = "The result is " + result.toString() + ". There was a " + percent_chance + " percent chance of receiving this result.";
                p_element.appendChild(d.createTextNode(message));
                li_element.appendChild(p_element);
                ul_element.appendChild(li_element);
                if (ul_element.childNodes.length === 1) {
                    d.getElementsByTagName("body").item(0).insertBefore(ul_element, d.getElementsByTagName("h2").item(0).nextSibling);
                }
                
            }
            function draw_number() {
                r = Math.floor(Math.random() * (100 + 1));
                if (r <= percent_chance_of_100) {
                    r = 100;
                    output(r);
                    if (typeof delay !== "undefined") {
                        window.clearInterval(delay);
                    }
                }
                else {
                    r = Math.floor(Math.random() * (99 + 1));
                    output(r);
                    percent_chance_of_100 += 2;
                }
            }
            if (typeof window !== "undefined" && window.setInterval !== undefined && window.clearInterval !== undefined) {
                draw_number();
                if (r !== 100) {
                    var delay = window.setInterval(draw_number, 2000);
                }
            }
            else {
                do {
                    draw_number();
                }
                while (r !== 100);
            }
        </script>
        <!--<![endif]-->
        <!--[if IE]>
            <script type="text/ecmascript" defer="defer">
                var d = document;
                var r = 0; // random number
                var percent_chance_of_100 = 10; // 10% chance of 100; 90% chance of 1 through 99
                var p_element = d.getElementById("no_script");
                var h2_element = d.createElement("h2");
                var ul_element = d.createElement("ul"); // appended later to maintain HTML 4.01 validity
                h2_element.appendChild(d.createTextNode("The Results"));
                p_element.parentNode.replaceChild(h2_element, p_element);
                function output(result) {
                    var li_element = d.createElement("li");
                    var p_element = d.createElement("p");
                    var percent_chance = "";
                    if (result === 100) {
                        percent_chance = percent_chance_of_100.toString();
                    }
                    else {
                        percent_chance = ((100 - percent_chance_of_100) / 99).toFixed(2);
                    }
                    var message = "The result is " + result.toString() + ". There was a " + percent_chance + " percent chance of receiving this result.";
                    p_element.appendChild(d.createTextNode(message));
                    li_element.appendChild(p_element);
                    ul_element.appendChild(li_element);
                    if (ul_element.childNodes.length === 1) {
                        d.getElementsByTagName("body").item(0).insertBefore(ul_element, d.getElementsByTagName("h2").item(0).nextSibling);
                    }
                    
                }
                function draw_number() {
                    r = Math.floor(Math.random() * (100 + 1));
                    if (r <= percent_chance_of_100) {
                        r = 100;
                        output(r);
                        if (typeof delay !== "undefined") {
                            window.clearInterval(delay);
                        }
                    }
                    else {
                        r = Math.floor(Math.random() * (99 + 1));
                        output(r);
                        percent_chance_of_100 += 2;
                    }
                }
                if (typeof window !== "undefined" && window.setInterval !== undefined && window.clearInterval !== undefined) {
                    draw_number();
                    if (r !== 100) {
                        var delay = window.setInterval(draw_number, 2000);
                    }
                }
                else {
                    do {
                        draw_number();
                    }
                    while (r !== 100);
                }
            </script>
        <![endif]-->

    </body>
</html>
XHTML with PHP:
PHP Code:
<?php
    ob_start
();
    
header("Content-Type: application/xhtml+xml; charset=UTF-8");
    
header("Content-Script-Type: application/ecmascript");
    
header("Content-Style-Type: text/css");
    echo 
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
?>

<html xml:lang="en-Latn" xmlns="http://www.w3.org/1999/xhtml">
    <head>

        <meta http-equiv="Content-Type" content="application/xhtml+xml; charset=UTF-8"></meta>
        <meta http-equiv="Content-Script-Type" content="application/ecmascript"></meta>
        <meta http-equiv="Content-Style-Type" content="text/css"></meta>
        <title>Demo for CodingForums.com Thread 146939 (XHTML)</title>
        <meta name="Author" content="Patrick Garies"></meta>
        <meta name="Created" content="2008-08-18"></meta>
        <meta name="Revised" content="2008-08-19"></meta>
        <style type="text/css" media="all"><![CDATA[
            * { margin: 0; padding: 0; }
            html, h1, h2 { font: 16px/1.2 sans-serif; }
            html { background: #ded url("bg_c.png"); color: black; }
            h1 { opacity: 0.8; padding: 2em; background: white; font-weight: bolder; }
            a { color: black; }
            h2, ul { border: 0.5em solid; }
            h2 { margin: 0 1em; border-width: 0 0 0.5em 0.5em; padding: 1em; font-weight: bolder; }
            ul, li { padding: 0.5em; }
            ul { margin: 0 1em; border-width: 0 0.5em 0.5em 0; }
            li { list-style: inside decimal; }
            li p { display: inline; }
            *#no_script { margin: 1em; padding: 1em; background: red; color: palegoldenrod; font-weight: bolder; }
            *#no_script a { color: inherit; }
        ]]></style>

    </head>
    <body>

        <h1>Demo for <a href="http://www.codingforums.com/showthread.php?t=146939">CodingForums.com Thread 146939</a></h1>
        <p id="no_script">ECMAScript must be enabled in your browser for this document to work properly. Please enable it or obtain an ECMAScript&#x2010;capable browser such as <a href="http://www.mozilla.com/firefox/">Mozilla Firefox</a>.</p>

        <script type="application/ecmascript" defer="defer"><![CDATA[
            var d = document;
            var XHTML_NS = "http://www.w3.org/1999/xhtml";
            var r = 0; // random number
            var percent_chance_of_100 = 10; // initial 10% chance of 100 and 90% chance of 1 through 99
            var p_element = d.getElementById("no_script");
            var h2_element = d.createElementNS(XHTML_NS, "h2");
            var ul_element = d.createElementNS(XHTML_NS, "ul");
            h2_element.appendChild(d.createTextNode("The Results"));
            p_element.parentNode.replaceChild(h2_element, p_element);
            d.getElementsByTagNameNS(XHTML_NS, "body").item(0).insertBefore(ul_element, d.getElementsByTagNameNS(XHTML_NS, "h2").item(0).nextSibling);
            function output(result) {
                var li_element = d.createElementNS(XHTML_NS, "li");
                var p_element = d.createElementNS(XHTML_NS, "p");
                var percent_chance = "";
                if (result === 100) {
                    percent_chance = percent_chance_of_100.toString();
                }
                else {
                    percent_chance = ((100 - percent_chance_of_100) / 99).toFixed(2);
                }
                var message = "The result is " + result.toString() + ". There was a " + percent_chance + " percent chance of receiving this result.";
                p_element.appendChild(d.createTextNode(message));
                li_element.appendChild(p_element);
                ul_element.appendChild(li_element);
                
            }
            function draw_number() {
                r = Math.floor(Math.random() * (100 + 1));
                if (r <= percent_chance_of_100) {
                    r = 100;
                    output(r);
                    if (typeof delay !== "undefined") {
                        window.clearInterval(delay);
                    }
                }
                else {
                    r = Math.floor(Math.random() * (99 + 1));
                    output(r);
                    percent_chance_of_100 += 2;
                }
            }
            if (typeof window !== "undefined" && window.setInterval !== undefined && window.clearInterval !== undefined) {
                draw_number();
                if (r !== 100) {
                    var delay = window.setInterval(draw_number, 2000);
                }
            }
            else {
                do {
                    draw_number();
                }
                while (r !== 100);
            }
        ]]></script>

    </body>
</html>
__________________
Please for the love of god stop making IE. You current "browser"s cause me to cry every day. —Phil *

Last edited by Arbitrator; 08-19-2008 at 06:56 AM.. Reason: See the post.
Arbitrator is offline   Reply With Quote
Users who have thanked Arbitrator for this post:
Deacon Frost (08-19-2008)
Old 08-19-2008, 06:54 AM   PM User | #6
Deacon Frost
Regular Coder

 
Deacon Frost's Avatar
 
Join Date: Feb 2008
Location: Between the Lines
Posts: 279
Thanks: 31
Thanked 4 Times in 4 Posts
Deacon Frost is on a distinguished road
That is EXACTLY what I need.

Care to explain it?

Thank you so much for taking the time to put it all together !
Deacon Frost is offline   Reply With Quote
Old 08-19-2008, 07:23 AM   PM User | #7
Arbitrator
Senior Coder

 
Arbitrator's Avatar
 
Join Date: Mar 2006
Location: Splendora, Texas, United States of America
Posts: 2,895
Thanks: 5
Thanked 187 Times in 184 Posts
Arbitrator is on a distinguished road
Quote:
Originally Posted by Deacon Frost View Post
That is EXACTLY what I need.
Note that I corrected an error in the code. The previous post has been edited.

I found another error while writing this post. The ul element should be an ol element for semantic’s sake. The live versions (only) have been updated with this change.

Quote:
Originally Posted by Deacon Frost View Post
Care to explain it?
At the start, I declare necessary variables and remove the message for users without scripting enabled. I also create “The Results” header.

The function, draw_number, picks randomly between 1 and 100. 1 through 10 is the 10 percent range needed for 100. If the number is 1 through 10, the script ends (with output). Otherwise, a new random draw occurs for 1 through 99, the percent range is increased by two percent, and the function repeats (after output).

Note that there are two code tracks that lead to the draw. The first track is used if setInterval and its friends are supported; this should, generally, be always. The second track is used otherwise; the results will appear instantly in the second track without the two second delay between the creation of new list items. (The second track was what I had originally started coding and didn’t want to get rid of it since it’s the first time that I’ve found a use for a do while loop. I also have separate tracks since I always test for support of features like setInterval that aren’t part of a formal Web standard.)

The function, output, outputs the result into the document as an HTML/XHTML ordered list with paragraphs.

The Internet Explorer version of the script is the same in this particular demo with the exception of the MIME type. (So, you can get away with using a single script for this demo by, say, using a single script element with one of the older MIME types.)

The XHTML version of the script changes some methods into namespace‐aware methods and appends the ol element earlier since there’s no need for validity (i.e., there’s no document type declaration to validate against).

If you have trouble understanding how I edited the document (i.e., createElement, getElementsByTagName, appendChild, etc.), you may want to look up information on the W3C DOM. I figure that this might be the case since you’re still using outdated things like document.write.
__________________
Please for the love of god stop making IE. You current "browser"s cause me to cry every day. —Phil *
Arbitrator is offline   Reply With Quote
Users who have thanked Arbitrator for this post:
Deacon Frost (08-19-2008)
Old 08-19-2008, 07:30 AM   PM User | #8
Trinithis
Regular Coder

 
Join Date: Jun 2007
Location: USA
Posts: 527
Thanks: 26
Thanked 74 Times in 72 Posts
Trinithis will become famous soon enough
Here's a demo using the code I wrote above:
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> 
<html>
<head>
<title>Test</title>
</head>
<body><div>

<div id="output"></div>

<script type="text/javascript">

function output(str) {
  var out = document.getElementById("output");
  out.appendChild(document.createTextNode(str));
  out.appendChild(document.createElement("br"));
}

function genRand(chance) {
  var rand = Math.floor( chance >= Math.random() * 100
    ? Math.random() + 100
    : Math.random() * 101
    );
  var str = "Your number is " + rand + ". You had a " + (100 - chance) + "% chance of losing.";
  if(rand >= 100)
    output("Success: " + str);
  else {
    output("Failure: " + str);
    setTimeout(function() {
      genRand(chance + 2);
    }, 2000);
  }
}

genRand(10);

</script>
</div></body>
</html>

Last edited by Trinithis; 08-19-2008 at 07:41 AM..
Trinithis is offline   Reply With Quote
Users who have thanked Trinithis for this post:
Deacon Frost (08-19-2008)
Old 08-19-2008, 07:47 AM   PM User | #9
Deacon Frost
Regular Coder

 
Deacon Frost's Avatar
 
Join Date: Feb 2008
Location: Between the Lines
Posts: 279
Thanks: 31
Thanked 4 Times in 4 Posts
Deacon Frost is on a distinguished road
Wow!

Thanks for the explanation. I will probably end up using a combination of both of those.

I figured it'd be much more difficult than those. I can sort of read them, but I'm still very new to javascript, so yeah, it still confuses me.

Most of the knowledge I have of JS stems from w3schools, and other random browsing, so the other functions required were beyond what I had learned.

Now, how would I put either of those into a singular function? Would it simply be easy:

function fullscript(){
<script>
}

Or would I need to break it up?

Basically so I can put it onto an onclick...
Deacon Frost is offline   Reply With Quote
Old 08-19-2008, 07:48 AM   PM User | #10
Arbitrator
Senior Coder

 
Arbitrator's Avatar
 
Join Date: Mar 2006
Location: Splendora, Texas, United States of America
Posts: 2,895
Thanks: 5
Thanked 187 Times in 184 Posts
Arbitrator is on a distinguished road
Quote:
Originally Posted by Trinithis View Post
Arbitrator: What's with the PHP code
Deacon Frost is, apparently, familiar with PHP, so I decided to just paste in the raw source. The PHP portion of the code is easy enough to remove, if desired, since it’s all in one place.

Quote:
Originally Posted by Trinithis View Post
duplicate JS code (the IE/!IE) in your html file?
I create separate script elements due to Internet Explorer’s lack of support for the newer application/* MIME types.

When writing demos, I embed scripts (and style sheets) for ease of reading and maintenance (i.e., I maintain one file). When scripts happen to be identical between the sets of conditional comments (which is not that often) you get demos like this one that appear to be very redundant. In a “real” Web page, I probably would have referenced a single external script like in the following code:

Code:
<!--[if !IE>-->
<script type="application/ecmascript" src="script.es"></script>
<!--<![endif]-->
<!--[if IE]>
	<script type="text/ecmascript" src="script.es"></script>
<![endif]-->
Of course, if the scripts didn’t contain the same content, then I would create and reference two separate external files instead. Each browser would only download one, so there would likely be a decrease in bandwidth usage.
__________________
Please for the love of god stop making IE. You current "browser"s cause me to cry every day. —Phil *
Arbitrator is offline   Reply With Quote
Old 08-19-2008, 07:54 AM   PM User | #11
Trinithis
Regular Coder

 
Join Date: Jun 2007
Location: USA
Posts: 527
Thanks: 26
Thanked 74 Times in 72 Posts
Trinithis will become famous soon enough
Quote:
Originally Posted by Deacon Frost View Post
Basically so I can put it onto an onclick
Try this:
Code:
var el = ...

el.onclick = function() {
  genRand(10);
};
Trinithis is offline   Reply With Quote
Old 08-19-2008, 08:03 AM   PM User | #12
Arbitrator
Senior Coder

 
Arbitrator's Avatar
 
Join Date: Mar 2006
Location: Splendora, Texas, United States of America
Posts: 2,895
Thanks: 5
Thanked 187 Times in 184 Posts
Arbitrator is on a distinguished road
Quote:
Originally Posted by Deacon Frost View Post
Now, how would I put either of those into a singular function? Would it simply be easy:

function fullscript(){
<script>
}

Or would I need to break it up?

Basically so I can put it onto an onclick...
In my code, you would nest the following code in a function:

Code:
if (typeof window !== "undefined" && window.setInterval !== undefined && window.clearInterval !== undefined) {
	draw_number();
	if (r !== 100) {
		var delay = window.setInterval(draw_number, 2000);
	}
}
else {
	do {
		draw_number();
	}
	while (r !== 100);
}
The new code would look like the following:

Code:
function start_computation() {
	if (typeof window !== "undefined" && window.setInterval !== undefined && window.clearInterval !== undefined) {
		draw_number();
		if (r !== 100) {
			var delay = window.setInterval(draw_number, 2000);
		}
	}
	else {
		do {
			draw_number();
		}
		while (r !== 100);
	}
}
(Actually, that’s a bit simplified. You’d probably want to throw all of the DOM stuff at the beginning (like the “The Results” header creation) into the function too, but what you’d do there depends on your document’s structure.)

To call your function, you might use one of the following:

HTML/XHTML
Code:
<element onclick="start_computation();"></element>
W3C DOM2 Events (for everything except Internet Explorer)
Code:
element.addEventListener("click", start_computation, false);
Microsoft attachEvent Method (for Internet Explorer; non‐standard)
Code:
element.attachEvent("onclick", start_computation);
(Or, as I just noticed Trinithis used:)

element.onclick (non‐standard)
Code:
element.onclick = start_computation;
__________________
Please for the love of god stop making IE. You current "browser"s cause me to cry every day. —Phil *
Arbitrator is offline   Reply With Quote
Old 08-19-2008, 08:19 AM   PM User | #13
Trinithis
Regular Coder

 
Join Date: Jun 2007
Location: USA
Posts: 527
Thanks: 26
Thanked 74 Times in 72 Posts
Trinithis will become famous soon enough
Is element.onclick really non‐standard? I know it comes from way back when, but I thought it was still valid.

Normally I would use the DOM methods, but I opted for simplicity.
Trinithis is offline   Reply With Quote
Old 08-19-2008, 08:33 AM   PM User | #14
Arbitrator
Senior Coder

 
Arbitrator's Avatar
 
Join Date: Mar 2006
Location: Splendora, Texas, United States of America
Posts: 2,895
Thanks: 5
Thanked 187 Times in 184 Posts
Arbitrator is on a distinguished road
Quote:
Originally Posted by Trinithis View Post
Is element.onclick really non‐standard?
Yes, it is non‐standard. DOM2 HTML doesn’t have an onclick property; you have to use setAttribute("onclick", value); (via DOM Core) which, of course, doesn’t work correctly in Internet Explorer. <element onclick="value"></element> is more standard than element.onclick.

Quote:
Originally Posted by Trinithis View Post
I know it comes from way back when, but I thought it was still valid.
There’s really no such thing as validity in the scripting world.

Quote:
Originally Posted by Trinithis View Post
Normally I would use the DOM methods, but I opted for simplicity.
Yeah, I realize that they can be verbose when browser support is factored into the equation.

If I were to use element.onclick, I would test it first since it’s a non‐standard feature:

Code:
if (element.onclick !== undefined) {
	element.onclick = do_something;
}
Of course, that’s just my preference.
__________________
Please for the love of god stop making IE. You current "browser"s cause me to cry every day. —Phil *
Arbitrator is offline   Reply With Quote
Old 08-19-2008, 08:57 AM   PM User | #15
Trinithis
Regular Coder

 
Join Date: Jun 2007
Location: USA
Posts: 527
Thanks: 26
Thanked 74 Times in 72 Posts
Trinithis will become famous soon enough
Thanks for the info. (For some reason my thanks button isn't working.)
Trinithis is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 02:22 PM.


Advertisement
Log in to turn off these ads.