JavaScript - Is This Legal?
Hi all,
I just tried this: (value == true) ? if_true_func() : if_false_func(); The idea being if "value" is true, then the "if_true" function is executed otherwise it execs the "if_false" function. I tried it on a hunch and it worked! But is it legal code or am I fooling myself? Thanks. -- Roger Similar TutorialsHi all, I've noticed that setTimeout seems to return a simple integer as it's "id" which can be used to clear the timeout ahead of time. So, in order to have multiple setTimeouts called (which requires the previous one to be cleared before the next one is called), I simply did this: Code: clearTimeout(setTimeout(function() { /* some code */ }, 1000) -1); Note the "-1"... each time this code is called, it starts a new setTimeout and clears "instance-1" which is supposed to be the previous instance. The idea is that this block can be called hundreds of times, and when the calling finally stops, the inner code is executed 1 second later. This SEEMS to be working (yes, even in MSIE!). Question is, am I fooling myself? Is this wrong? If so, what's the right way to do it? Thanks... -- Roger (btw, the actual use of this is dynamic resizing of a DIV by dragging the corner with the mouse.... I want the actual PX size to display live while resizing, then after resizing stops, the size display goes away 1 second later... in case you were wondering). I have been using the following html & have tested it cross-browser with no malfunction Code: <a href="#quicklinks" name="modal">Quick Links</a> <a href="#Login" name="modal">Login</a> js part Code: //select all the a tag with name equal to modal $('a[name=modal]').click(function(e) { //Cancel the link behavior e.preventDefault(); //Get the A tag var id = $(this).attr('href'); //using href as id (already defined in CSS) $(id).fadeIn(2000); Now a senior guy says that what I am doing with selectors is essentially illegal, I quote him Quote: Another potential problem is your use of the name attribute. When used with "A" tags, the name attribute is equivalent to the ID attribute, meaning that each name on a page should be unique. It's possible that the javascript engine is only setting the onclick() on the first element with the "modal" name. Plz guide me Hi all, I wrote a little live clock in JS (using Steve Levithan's "date format" code) that formats the time display according to a format string. I then find all elements by classname and search for a classname of "clock" and write the clock string to the innerHTML of every element I find. But recently I added another "feature". Rather than hard code a format string, I instead pass it in ID. Example: <span class="clock" id="hh:mm:mm tt"></span> ..and the JS: Code: var doClock = function () { var now = new Date(); var e = getElementsByClassName('clock'); var i = e.length; while (i--) { e[i].innerHTML = dateFormat(now, e[i].id, false); } } Note that I am passing the clock FORMAT STRING by using the span's ID. It works great... question is, is it HTML LEGAL to do this? Thanks! -- Roger |