JavaScript - Return Keyword Applied On Object Returns Reference Or Value?
I am confused about what the return keyword is actually returning when returning an object, a primitive, or a function.
My confusion is compounded by the fact that I'm not sure if a function is an object or not. According to the book JavaScript Programmer Reference it is: "All functions in JavaScript are first class objects , meaning they can be passed around like any other object reference. In fact, regardless of how they are created, functions are instances of a global object named (aptly) Function." However, someone else states that a function is not an object. An object is a set of primitives and references. A function is executable code. Functions become objects through the use of the new operator. Yet, in the book I mentioned, it says you don't need the new keyword to execute the function as an object, as it already inherits from the object Function when the function keyword is used: Code: function functionName([argname1 [, ...[, argnameN]]]) { statements; } So there's one source of contradiction. Now the bigger issue is what is going on when the return keyword is used. Notice the Validation() function returns an object as its last expression. This technique is common, where you return an object which contains functions in form of object notation. I believe this is done so that we create a closure so that when the intepreter exits the Validation() method, since we created a closure by returning an object, which contains the inner functions addRule and getRule, the local variables of Validation() are not destroyed, given that we can reference them through the two inner functions that make use of the local variables of the outer function. So when we use the return keyword on an object literal, and then exit the function, when we call one of the inner functions as we do later: Code: var rule = $.Validation.getRule(types[type]); essentially getRule() is called, passes an argument, which is received by the inner function as parameter we call name: Code: getRule : function(name) { return rules[name]; } First, note that the return {} is written in object notation, therefore making getRule a local variable and, thus, private function only accessible through the namespace of Validation(). Validation() declares the rules local variable and because of the closure, we can access the rules local variable through the getRule() inner function. *****Here's the part that really thows me off. We return rules[name]. So let's say name is equal to email. This is an associative array so email (held in name) is a property of rules. So here we return the object's property: Code: return rules[name]; And then assign it to a local variable called rule: Code: var rule = $.Validation.getRule(types[type]); So when we return an object rules[name], do we return a reference to an object or a value? In other words, by returning rules[name], where name is equal to email, are we then returning a reference to the following object literal: Code: email : { check: function(value) { if(value) return testPattern(value,".+@.+\..+"); return true; }, msg : "Enter a valid e-mail address." } And if we are returning a reference, by returning a reference, are we essentially pointing to this object when we assign it to rule? In other words, the variable rule is just pointing to the object literal? And is that the reason we can then access the check function or msg local variable through rule using dot notation, because rule points to the email object literal? Now the ultimate brain twist for me is that if a function is an object, then why when return a function, it returns a value, such as a boolean, if an object only returns a reference and not the value? Code: //Validation is a local variable as it is in a self-executing anonymous function. The purpose of the said anonymous function is to pass the jQuery object as a parameter $ so the $() function will be in scope of the anonymous function and not interfere with other libraries that make use of the same function technique - in the global scope. (function($) { var rules = { email : { check: function(value) { if(value) return testPattern(value,".+@.+\..+"); return true; }, msg : "Enter a valid e-mail address." }, url : { check : function(value) { if(value) return testPattern(value,"https?://(.+\.)+.{2,4}(/.*)?"); return true; }, msg : "Enter a valid URL." }, required : { check: function(value) { if(value) return true; else return false; }, msg : "This field is required." } } var testPattern = function(value, pattern) { var regExp = new RegExp("^"+pattern+"$",""); return regExp.test(value); //The test() method is built into javascript } return { addRule : function(name, rule) { rules[name] = rule; }, getRule : function(name) { return rules[name]; } } } /* Form factory */ var Form = function(form) { var fields = []; $(form[0].elements).each(function() { var field = $(this); if(field.attr('validation') !== undefined) { fields.push(new Field(field)); } }); this.fields = fields; } Form.prototype = { validate : function() { for(field in this.fields) { this.fields[field].validate(); } }, isValid : function() { for(field in this.fields) { if(!this.fields[field].valid) { this.fields[field].field.focus(); return false; } } return true; } } /* Field factory */ var Field = function(field) { this.field = field; this.valid = false; this.attach("change"); } Field.prototype = { attach : function(event) { var obj = this; if(event == "change") { obj.field.bind("change",function() { return obj.validate(); }); } if(event == "keyup") { obj.field.bind("keyup",function(e) { return obj.validate(); }); } }, validate : function() { var obj = this, field = obj.field, errorClass = "errorlist", errorlist = $(document.createElement("ul")).addClass(errorClass), types = field.attr("validation").split(" "), container = field.parent(), errors = []; field.next(".errorlist").remove(); for (var type in types) { var rule = $.Validation.getRule(types[type]); if(!rule.check(field.val())) { container.addClass("error"); errors.push(rule.msg); } } if(errors.length) { obj.field.unbind("keyup") obj.attach("keyup"); field.after(errorlist.empty()); for(error in errors) { errorlist.append("<li>"+ errors[error] +"</li>"); } obj.valid = false; } else { errorlist.remove(); container.removeClass("error"); obj.valid = true; } } } /* Validation extends jQuery prototype */ $.extend($.fn, { validation : function() { var validator = new Form($(this)); $.data($(this)[0], 'validator', validator); $(this).bind("submit", function(e) { validator.validate(); if(!validator.isValid()) { e.preventDefault(); } }); }, validate : function() { var validator = $.data($(this)[0], 'validator'); validator.validate(); return validator.isValid(); } }); $.Validation = new Validation(); })(jQuery); Thanks for any response. Similar TutorialsThe goal is for selectedDay to be assigned the value of the system variable Mainpackage.subclass0.subclass1.firstDay and then incremented by two days. Mainpackage.subclass0.subclass1.firstDay needs to be unchanged. Important system variable in red. To be 'manipulated and used' variable in green. Even with an intermediary, third, dummy variable it doesn't work: Code: //Declarations and instantiations var systemDate = new Date(); var selectedDay = new Date(); Mainpackage.subclass0.subclass1.firstDay ; //Assignments and manipulations systemDate = Mainpackage.subclass0.subclass1.firstDay ; selectedDay = systemDate ; selectedDay .setDate( selectedDay .getDate()+2); //Logging console.log('Mainpackage.subclass0.subclass1.firstDay: ' +Mainpackage.subclass0.subclass1.firstDay +'\n' +'systemDate: ' +systemDate +'\n' +'selectedDay :' +selectedDay +'\n'); Console log is: Code: Mainpackage.subclass0.subclass1.firstDay: Tue Aug 24 2010 00:00:00 GMT-0400 (Eastern Daylight Time) systemDate: Tue Aug 24 2010 00:00:00 GMT-0400 (Eastern Daylight Time) selectedDay: Tue Aug 24 2010 00:00:00 GMT-0400 (Eastern Daylight Time) It doesn't work in my webapp : All variables change at the same time. My question is commented in the script: Code: function Love(){ var req = { ring : 'gold', money : 'a lot', commitment : 'long-term' } this.loveReqs = function(){ return req; } } var you = new Love(); var details = you.loveReqs(); details.ring = "silver"; console.log(details.ring); var me = new Love(); var details = me.loveReqs(); console.log(details.ring); // why does this return gold and not silver if req is passed by reference and we already changed its value above and so it's pointing to the same memory position? Hi, I have this ajax routine... Code: function xhr_get(target_str,async){ var sync=true; if(async){sync=false;} var xhr = new XMLHttpRequest(); xhr.open('GET', target_str, sync); if(sync==false){ xhr.onreadystatechange = function () { if (xhr.status === 200) { try{ var ii =JSON.parse(xhr.responseText);} catch (exception) { } if(ii){ return ii; }else{ alert(xhr.responseText); return false; } } else { alert(xhr.status+' '+target_str);return false; } }; xhr.send(null); }else{ xhr.send(null); try{ var ii =JSON.parse(xhr.responseText);} catch (exception) { } if(ii){ return ii; }else{ alert(xhr.responseText); return false; } } } now if I alert(ii) on success the data I am looking for (specifically ii.content) shows up in the alert as expected however when calling from another javascript function, ii is false ???? please explain, Code: function call_change_val(fld,vm_id){ var str='?_f=load_edit_fld&_ctl_fld='+fld+'&vm_id='+vm_id+'&fld='+fld+'&width=80'; i = xhr_get('ajax/val_main_loader.php'+str,true); ////////////////////// alert(i); or alert(i.content) here both give be a blank popup///////////////// showdiv('c_change_'+fld); setih('c_change_'+fld,i.content); } I expected alert(i) to say Object or similar, getting nothing Hello, how can I make the following object work properly so when I use 'layers.photo1.layer' a jQuery element is returned rather than the text of the function as it is now. I also need to be able to find the element by the photo1 object its contained in not have to input a string there so that I can also reuse it in the background layer. Code: var layers = { photo1 : { layer: function() { return $(element).find($('photo1'))} }, background : { layer: function() { return $(element).find($('background'))} } } Thx Very Much! Hi, I'm trying to use a form which is existent on one of my sites and try re-creating a similar form on another site for the exact same purpose. Here is the URL for the form on our website Cast Iron Cookware Depot. I have everything duplicated but running into form validation errors. Right now without event entering any data into the form and also the verification code the form still gets submitted but ofcourse runs into "object expected" error at onsubmit="return validate(this);"> by IE Debugger. Below is the total code and would appreciate if any of you gurus point out where the mistake is and also why the exact same code is working on one site is not working on this site. Thanks much in advance. Please help me! ------------------------------------------------------------------------ Code: <style> .TableBG { background-color: #83a338; color: #ffffff; font-family: Tahoma, Verdana, Arial, Helvetica, sans-serif; font-size: 12px; font-weight: bold; } .no { font-family: Tahoma, Verdana, Arial, Helvetica, sans-serif; font-size: 12px; font-weight: bold; color: #333333; width: 35px; text-align:right; } input, textarea {border: 1px inset #cccccc; font-family: Tahoma, Verdana, Arial, Helvetica, sans-serif; font-size: 12px;} .input01 {width: 150px;} .input02 {width: 250px;} .button { background-color: #83a338; color: #000000; border: 1px outset #83a338; font-family: Tahoma, Verdana, Arial, Helvetica, sans-serif; font-weight: bold; font-size: 12px; } </style><br /> <br /> <table width="600" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#FFFFFF" class="TableBG"> <tr> <td bgcolor="#FFFFFF"> <FORM name="form1" method="POST" action="http://s.p8.hostingprod.com/@castironcookwaredepot.com/php/tellafriend.php" onsubmit="return validate(this);"> <FORM name="form1" method="POST" action="http://s.p4.hostingprod.com/@bestsafetyapparel.com/php/tellafriend.php" onsubmit="return validate(this);"> <table width="100%" border="0" cellpadding="5" cellspacing="0"> <tr> <td class="TableBG"> </td> <td class="TableBG"><strong>Your Name: </strong></td> <td class="TableBG"><strong>Your Email: </strong></td> </tr> <tr> <td colspan="3" height="5"></td> </tr> <tr> <td> </td> <td> <input type="text" name="sName" class="input01" style="font-weight: bold;" /> </td> <td> <input type="text" name="sEmail" class="input02" size="40" style="font-weight: bold;" /> </td> </tr> <tr> <td colspan="3" height="5"></td> </tr> <tr> <td width="4%" class="TableBG"> </td> <td width="36%" class="TableBG"> Your Friend's Name :</td> <td width="60%" class="TableBG">Your Friend's Email:</td> </tr> <tr> <td colspan="3" height="5"></td> </tr> <tr> <td class="no"><strong>1.</strong></td> <td> <input type="text" name="name1" class="input01" /> </td> <td> <input type="text" name="email1" class="input02" size="40" /> </td> </tr> <tr> <td class="no"><strong>2.</strong></td> <td> <input type="text" name="name2" class="input01" /> </td> <td> <input type="text" name="email2" class="input02" size="40" /> </td> </tr> <tr> <td class="no"><strong>3.</strong></td> <td> <input type="text" name="name3" class="input01" /> </td> <td> <input type="text" name="email3" class="input02" size="40" /> </td> </tr> <tr> <td class="no"><strong>4.</strong></td> <td> <input type="text" name="name4" class="input01" /> </td> <td> <input type="text" name="email4" class="input02" size="40" /> </td> </tr> <tr> <td class="no"><strong>5.</strong></td> <td> <input type="text" name="name5" class="input01" /> </td> <td> <input type="text" name="email5" class="input02" size="40" /> </td> </tr> <tr> <td class="TableBG"> </td> <td colspan="2" class="TableBG">Your Message </td> </tr> <tr> <td colspan="3" height="5"></td> </tr> <tr> <td colspan="3" align="center"> <textarea name="comments" cols="65" rows="5" id="comments" style="width: 420px;"></textarea> </td> </tr> <tr> <td class="TableBG"> </td> <td colspan="3" class="TableBG">Enter Verification Code</td> </tr> <tr> <td colspan="3" height="5"></td> </tr> <tr> <td colspan="3" align="center" valign="absmiddle"><img src="http://s.p8.hostingprod.com/@castironcookwaredepot.com/php/captcha.php" align="absmiddle"> <input type="text" name="vercode" value="Enter Verification Code" onFocus="if(this.value=='Enter Verification Code') this.value='';" onBlur="if(this.value=='') this.value='Enter Verification Code';" size="25"/></td> </tr> <tr> <td colspan="3" align="center"> <input type="submit" name="Submit" value=" Send Email " class="button" /> </td> </tr> </table> </form> </td> </tr> </table> <script> function validate(frm) { name = frm.sName; email = frm.sEmail; name1=frm.name1; email1=frm.email1; err_flag = 0; if (name.value == "" || !removeSpaces(name.value)) { alert ("Please enter proper Name!"); name.value=""; name.focus(); return false; } else if (email.value == "" || !validate_email(email.value)) { alert ("Please enter proper Email!"); email.value=""; email.focus(); return false; } else if (name1.value == "" || !removeSpaces(name1.value)) { alert ("Please enter proper Friend\'s Name!"); name1.value=""; name1.focus(); return false; } else if (email1.value == "" || !validate_email(email1.value)) { alert ("Please enter proper Friend\'s Email!"); email1.value=""; email1.focus(); return false; } } function validate_email(e) { var str=e; var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i if (!filter.test(str)) return false; else return true; } function removeSpaces(string) { var tstring = ""; string = '' + string; splitstring = string.split(" "); for(i = 0; i < splitstring.length; i++) tstring += splitstring[i]; return tstring; } </script> <br /> <br /> Regards Learner I don't understand the logic of Break, Return False, Return True. It was never really covered in our college class, and I see everyone using it. I got an A in the class, if that 'proves' that I really tried to apply myself. NOTE: I understand what the function is doing. I just don't understand WHEN to use break, return false or return true if the the translator can determine the conditional statements. PHP Code: function submitForm(){ var ageSelected = false; for (var i=0; i<5; ++1){ if (document.forms[0].ageGroup[i].checked == true) { ageSelected = true; break; } } if (ageSelected == false){ window.alert("You must select your age group"); return false; } else return false; } if the the translator can determine the conditional statements, why not write it like this: PHP Code: function submitForm(){ var ageSelected = false; for (var i=0; i<5; ++1){ if (document.forms[0].ageGroup[i].checked == true) { ageSelected = true; break; // what's the point for the 'break'? Won't the rest of the code be ignored since it passed the first condition? } } if (ageSelected == false){ window.alert("You must select your age group"); return false; } // why not leave the last else out? is it just a 'safety' catch, in case something other than true or false is inputted? else return false; // what's the point? } Questions: Why use return true, if the translator knows it's ture? Why use "return false" if the translator knows it's false and the alert window has already gone up? why not use "break" to stop the code? Why use the "return false" at the end "else" statement? Hi room, Hey, I opened up the source code for this page in google chrome and since i'm learning javascript, i wanted see if i could "read" it and figure out what was going on. I'm am having the hardest time understanding "return false" and "return true". Could someone step me through this via interpreting this code (in bold typeface): Code: var DefaultValue = 'Search'; function clearSearch() { if (document.searchForm.q.value == DefaultValue) { document.searchForm.q.value = ''; } } function validateSearchHeader() { if ( document.searchForm.q.value == '' || document.searchForm.q.value.toLocaleLowerCase() == DefaultValue.toLocaleLowerCase() ) { alert('Please enter at least one keyword in the Search box.'); document.searchForm.q.focus(); return false; } return true; } Thanks! i am making a payslip but i do not know how to correct the error : =grosspay(int,int) in payslip3 cannot be applied to (int) =netpay(int,int) in payslip3 cannot be applied to (int) =totaldeduction(int,int) in payslip3 cannot be applied to (int) also whenever I type the value and push the button in order to calculate the answers, the inputs are being cleared...... here is the code...... Code: import javax.swing.*; import java.awt.*; import java.awt.event.*; public class payslip3 extends JFrame implements ActionListener { private static int width=600; private static int height=300; private JLabel[] labelJL = new JLabel[18]; private JTextField[] textJT = new JTextField[16]; private JButton[] AddClearExit = new JButton[5]; private JPanel panel = new JPanel(new GridLayout()); public payslip3() { Container pane = getContentPane(); pane.setBackground (Color.black); setTitle("USING BUTTON EVENT"); setSize(width,height); labelJL[0] = new JLabel("Payslip",0); labelJL[0].setForeground(Color.white); labelJL[0].setFont(new Font("Comic Sans MS",Font.BOLD,50)); labelJL[0].setLocation(560,10); labelJL[0].setSize(225,125); pane.add(labelJL[0]); labelJL[0] = new JLabel("RUN DATE:",0); labelJL[0].setForeground(Color.white); labelJL[0].setFont(new Font("Verdana",Font.BOLD,13)); labelJL[0].setLocation(250,150); labelJL[0].setSize(100,20); pane.add(labelJL[0]); textJT[0] = new JTextField(); textJT[0].setBackground(Color.white); textJT[0].setLocation(350,150); textJT[0].setSize(200,20); pane.add(textJT[0]); labelJL[1] = new JLabel("DAYS WORKED:",0); labelJL[1].setForeground(Color.white); labelJL[1].setFont(new Font("Verdana",Font.BOLD,13)); labelJL[1].setLocation(200,200); labelJL[1].setSize(150,20); pane.add(labelJL[1]); textJT[1] = new JTextField(); textJT[1].setBackground(Color.white); textJT[1].setLocation(350,200); textJT[1].setSize(200,20); pane.add(textJT[1]); labelJL[2] = new JLabel("BASIC PAY:",0); labelJL[2].setForeground(Color.white); labelJL[2].setFont(new Font("Verdana",Font.BOLD,13)); labelJL[2].setLocation(225,250); labelJL[2].setSize(100,20); pane.add(labelJL[2]); textJT[2] = new JTextField(); textJT[2].setBackground(Color.white); textJT[2].setLocation(350,250); textJT[2].setSize(200,20); pane.add(textJT[2]); labelJL[3] = new JLabel("OVERTIME WORKED:",0); labelJL[3].setForeground(Color.white); labelJL[3].setFont(new Font("Verdana",Font.BOLD,13)); labelJL[3].setLocation(180,300); labelJL[3].setSize(170,20); pane.add(labelJL[3]); textJT[3] = new JTextField(); textJT[3].setBackground(Color.white); textJT[3].setLocation(350,300); textJT[3].setSize(200,20); pane.add(textJT[3]); labelJL[4] = new JLabel("OVERTIME RATE:",0); labelJL[4].setForeground(Color.white); labelJL[4].setFont(new Font("Verdana",Font.BOLD,13)); labelJL[4].setLocation(180,350); labelJL[4].setSize(200,20); pane.add(labelJL[4]); textJT[4] = new JTextField(); textJT[4].setBackground(Color.white); textJT[4].setLocation(350,350); textJT[4].setSize(200,20); pane.add(textJT[4]); labelJL[5] = new JLabel("GROSS PAY:",0); labelJL[5].setForeground(Color.white); labelJL[5].setFont(new Font("Verdana",Font.BOLD,13)); labelJL[5].setLocation(250,450); labelJL[5].setSize(100,20); pane.add(labelJL[5]); textJT[5] = new JTextField(); textJT[5].setBackground(Color.white); textJT[5].setLocation(350,450); textJT[5].setSize(200,20); pane.add(textJT[5]); labelJL[6] = new JLabel("NET PAY:",0); labelJL[6].setForeground(Color.white); labelJL[6].setFont(new Font("Verdana",Font.BOLD,13)); labelJL[6].setLocation(250,500); labelJL[6].setSize(100,20); pane.add(labelJL[6]); textJT[6] = new JTextField(); textJT[6].setBackground(Color.white); textJT[6].setLocation(350,500); textJT[6].setSize(200,20); pane.add(textJT[6]); labelJL[7] = new JLabel("CUT-OFF DATE:",0); labelJL[7].setForeground(Color.white); labelJL[7].setFont(new Font("Verdana",Font.BOLD,13)); labelJL[7].setLocation(680,150); labelJL[7].setSize(210,20); pane.add(labelJL[7]); textJT[7] = new JTextField(); textJT[7].setBackground(Color.white); textJT[7].setLocation(850,150); textJT[7].setSize(200,20); pane.add(textJT[7]); labelJL[8] = new JLabel("NAME:",0); labelJL[8].setForeground(Color.white); labelJL[8].setFont(new Font("Verdana",Font.BOLD,13)); labelJL[8].setLocation(750,190); labelJL[8].setSize(100,20); pane.add(labelJL[8]); textJT[8] = new JTextField(); textJT[8].setBackground(Color.white); textJT[8].setLocation(850,190); textJT[8].setSize(200,20); pane.add(textJT[8]); labelJL[9] = new JLabel("TAX WITH HELD:",0); labelJL[9].setForeground(Color.white); labelJL[9].setFont(new Font("Verdana",Font.BOLD,13)); labelJL[9].setLocation(680,300); labelJL[9].setSize(200,20); pane.add(labelJL[9]); textJT[9] = new JTextField(); textJT[9].setBackground(Color.white); textJT[9].setLocation(850,300); textJT[9].setSize(200,20); pane.add(textJT[9]); labelJL[10] = new JLabel("SSS:",0); labelJL[10].setForeground(Color.white); labelJL[10].setFont(new Font("Verdana",Font.BOLD,13)); labelJL[10].setLocation(760,350); labelJL[10].setSize(100,20); pane.add(labelJL[10]); textJT[10] = new JTextField(); textJT[10].setBackground(Color.white); textJT[10].setLocation(850,350); textJT[10].setSize(200,20); pane.add(textJT[10]); labelJL[11] = new JLabel("MEDICAL:",0); labelJL[11].setForeground(Color.white); labelJL[11].setFont(new Font("Verdana",Font.BOLD,13)); labelJL[11].setLocation(750,400); labelJL[11].setSize(100,20); pane.add(labelJL[11]); textJT[11] = new JTextField(); textJT[11].setBackground(Color.white); textJT[11].setLocation(850,400); textJT[11].setSize(200,20); pane.add(textJT[11]); labelJL[12] = new JLabel("PAG-IBIG:",0); labelJL[12].setForeground(Color.white); labelJL[12].setFont(new Font("Verdana",Font.BOLD,13)); labelJL[12].setLocation(750,450); labelJL[12].setSize(100,20); pane.add(labelJL[12]); textJT[12] = new JTextField(); textJT[12].setBackground(Color.white); textJT[12].setLocation(850,450); textJT[12].setSize(200,20); pane.add(textJT[12]); labelJL[13] = new JLabel("SSS LOAN:",0); labelJL[13].setForeground(Color.white); labelJL[13].setFont(new Font("Verdana",Font.BOLD,13)); labelJL[13].setLocation(750,500); labelJL[13].setSize(100,20); pane.add(labelJL[13]); textJT[13] = new JTextField(); textJT[13].setBackground(Color.white); textJT[13].setLocation(850,500); textJT[13].setSize(200,20); pane.add(textJT[13]); labelJL[14] = new JLabel("PAG-IBIG LOAN:",0); labelJL[14].setForeground(Color.white); labelJL[14].setFont(new Font("Verdana",Font.BOLD,13)); labelJL[14].setLocation(690,550); labelJL[14].setSize(200,20); pane.add(labelJL[14]); textJT[14] = new JTextField(); textJT[14].setBackground(Color.white); textJT[14].setLocation(850,550); textJT[14].setSize(200,20); pane.add(textJT[14]); labelJL[15] = new JLabel("TOTAL DEDUCTION:",0); labelJL[15].setForeground(Color.white); labelJL[15].setFont(new Font("Verdana",Font.BOLD,13)); labelJL[15].setLocation(675,620); labelJL[15].setSize(200,20); pane.add(labelJL[15]); textJT[15] = new JTextField(); textJT[15].setBackground(Color.white); textJT[15].setLocation(850,620); textJT[15].setSize(200,20); pane.add(textJT[15]); labelJL[16] = new JLabel("DEDUCTION",0); labelJL[16].setForeground(Color.white); labelJL[16].setFont(new Font("Gill Sans Ultra Bold",Font.BOLD,10)); labelJL[16].setLocation(870,250); labelJL[16].setSize(100,20); pane.add(labelJL[16]); AddClearExit[0] = new JButton("Gross Pay"); AddClearExit[0].setLocation(225,750); AddClearExit[0].setSize(150,20); AddClearExit[0].addActionListener(this); pane.add(AddClearExit[0]); AddClearExit[0] = new JButton("Net Pay"); AddClearExit[0].setLocation(825,750); AddClearExit[0].setSize(150,20); AddClearExit[0].addActionListener(this); pane.add(AddClearExit[0]); AddClearExit[0] = new JButton("Total Deduction"); AddClearExit[0].setLocation(1025,750); AddClearExit[0].setSize(200,20); AddClearExit[0].addActionListener(this); pane.add(AddClearExit[0]); AddClearExit[1] = new JButton("Clear"); AddClearExit[1].setLocation(425,750); AddClearExit[1]. setSize(150,20); AddClearExit[1].addActionListener(this); pane.add(AddClearExit[1]); AddClearExit[2] = new JButton("Exit"); AddClearExit[2].setLocation(625,750); AddClearExit[2].setSize(150,20); AddClearExit[2].addActionListener(this); pane.add(AddClearExit[2]); setVisible(true); setDefaultCloseOperation(EXIT_ON_CLOSE); } public void actionPerformed(ActionEvent ae) { String temp = ""; int bp=0,otr=0,grosspay=0,d=0,tw=0,sss=0,m=0,p=0,sssl=0, pl=0,totaldeduction=0,netpay=0; try { bp=Integer.parseInt(textJT[2].getText()); otr=Integer.parseInt(textJT[4].getText()); grosspay=Integer.parseInt(textJT[15].getText()); tw=Integer.parseInt(textJT[8].getText()); sss=Integer.parseInt(textJT[9].getText()); m=Integer.parseInt(textJT[10].getText()); p=Integer.parseInt(textJT[11].getText()); sssl=Integer.parseInt(textJT[12].getText()); pl=Integer.parseInt(textJT[13].getText()); totaldeduction=Integer.parseInt(textJT[14].getText()); netpay=Integer.parseInt(textJT[6].getText()); } catch (Exception ne) { for(int x=0;x<16;textJT[x].setText(temp),x++); } if(ae.getActionCommand().equals("GrossPay")) {temp+=grosspay (bp*otr); textJT[5].setText(temp);} else if(ae.getActionCommand().equals("NetPay")) {temp+=netpay (grosspay-totaldeduction); textJT[6].setText(temp);} else if(ae.getActionCommand().equals("TotalDeduction")) {temp+=totaldeduction (tw+sss+m+p+sssl+pl); textJT[15].setText(temp);} if(ae.getActionCommand().equals("Clear")) for(int x=0;x<16;textJT[x].setText(temp),x++); if(ae.getActionCommand().equals("Exit")) dispose(); } public int grosspay(int x,int y) { return x*y; } public int netpay(int x,int y) { return x-y; } public int totaldeduction(int x,int y) { return x+y; } public static void main (String args[]) { new Button(); } } Code: function Hero() { this.occupation = 'Ninja'; } don't understand the meaning of this line ,this.occupation = 'Ninja'; why it use this keyword. thank you. Hi, I've been searching google endlessly and I've come to the conclusion I'm not using the proper search keywords. I'm trying to create something similar to the banner/button combo you can find he http://www.nba.com and also here, http://home.worldvision.org/?page=nonflash I assume these type of slide show buttons combos use java script and possibly php. If anyone knows the proper name for this type of slide show or can point me to some scripting I would appreciate it. Thanks I'm not sure if this is the right forum, but I'm looking for help with adding a search box to my website. I want to create a keyword search box - one where entering the right keyword(s) will take the visitor DIRECTLY to the page they're looking for, without a results page. And if they don't enter a keyword (in a keyword database?) they're taken to an error page. So, in essence, it's not really a search engine, it's more of a keywords box. Oh, also, some of the keywords will have non alphabetical or numerical characters (like dashes and periods) if that's a concern. Can someone help me with this or point me in the right direction to someone/someplace that can? Thanks.
Hi, I'm Jon. First of all excuse me if I posted this thread in the wrong forum, I really have no clue where to put this but I've had some minor experience with js so I hope it can be solved this way. I check websites and/or keywords in googles Keyword Tool External as a free service for some of my clients. The thing is that more and more clients like this service and it's becoming very time-consuming for me to do this for all of them. Is there a way to let my clients fill in the keywords or url they like researched in a form on my website, then let those words be researched and return a specified number of results in a pdf or something? My biggest concern is the captcha code. If I can't find a way to partially or fully automate this process, I'll be forced to either charge for or completely give up this service. Can anyone help me with some kind of solution? Thanks in advance. Jon Hi everybody, I have an search form and I'm handling it with .htaccess like this: RewriteRule ^(.*)/search/(.*) /search.php?lang=$1&keyword=$2 The problem, i can't call the right URL from front-end. I have an search form with get and if i search something the url going to like this: domain.com/en/search/?keyword=test 404.. But if i type the URL manually like this: domain.com/en/search/test Than it's working well. So, I need some js code to put search keyword in the right form so i can cancel the <form>. Can somebody help me to write this code? Or any other ideas are welcome. tested this only on FF. Code: target = this.parentNode.parentNode.getElementsByClassName('help')[0]; target.style.display = (target.style.display=='none')?'inline':'none'; -->Error: target.style is undefined In next two codes what is red is different, all the rest as above. Code: var target = this.parentNode.parentNode.getElementsByClassName('help')[0]; target.style.display = (target.style.display=='none')?'inline':'none'; --> works. Code: xxx = this.parentNode.parentNode.getElementsByClassName('help')[0]; xxx .style.display = ( xxx .style.display=='none')?'inline':'none'; --> works. Why is that ? Hi, every time I try and alert: [ { number:0, secondnumber:0 }, { number:2, secondnumber:1 }, { number:1, secondnumber:2 } ] it just shows [object object], [object object], [object object]. Why is this and what can I do to make the record be shown as it is above in an alert? Thanks. Hello all, Sorry if this may seem like a silly question, I have searched but not really to sure how to word what I am searching for as I don't know if I am going the right way around it! Basically, I am looking to insert a keyword in to a javascript alert box when someone visits my website, so say they came from codingforums.com, it would say "Welcome, CodingForums.com Visitor". My keyword will be passed from the ad platform I am working with and shows up correctly in the tracking, so I'd imagine it's just a case of having the snippet of code for it to show in the alert, correct? If there is no keyword, I would just like it to say "Welcome Visitor" or something. How do I go about this? Thank you in advance for any help. I'm looking at building an interactive desktop gadget for our company computers that functions similar to Clippy (less annoying though). The idea is for users to be able to type in keywords or questions and have the gadget present links to pages on our intranet. So similar to a search engine but more focused on answering questions as opposed to just returning a hundred results. I've got a moderate understanding of java so I know I could do something like IF "Textbox" EQUALS "Keyword" THEN DO "THAT". Need a little more sophistication though, it should be able to accept multiple keywords and weigh the search results accordingly. I'm sort of picturing an updatable XML table like this: KEYWORD This is the result KEYWORD This is the result Or an array in a separate .js file. Maybe there's a better way than that actually but it's what I know. Has anyone got some advice on this? Thanks. Code: this.delete = function(obj) { .. Is that it ? I can't have delete ? Or can this be written in some other way, including delete ? I can't get any info from Firebug except that one line, uncaught exception [object Object]. The code fully worked, then I needed to make it dynamically create Sortables from the scriptaculous library based on how many X were in a table in my database, which I've done, and I'm thinking it may be a simple slight parse error of some type, I'm not too good with Javascript, because now my script barely works. I've double checked the script's source code, the PHP variables are exactly what they should be. Code: print<<<HERE Sortable.create('sortlist$box', { tag: 'img', overlap:'horizontal',constraint:false, containment: $list, dropOnEmpty: true, onChange: function(item) { var list = Sortable.options(item).element; if(changeEffect) changeEffect.cancel(); changeEffect = new Effect.Highlight('changeNotification', {restoreColor:"transparent" }); }, onDrop: function(item) { var thing=Sortable.options(item).element.identify(); var anchors = document.getElementById(thing).childNodes.length-2; if(anchors > 20){ alert('This box had 20 creatures in it already, your last action has not been saved.'); window.location.reload(); } else{ new Ajax.Request("saveImageOrder.php", { method: "post", parameters: { data: Sortable.serialize("sortlist$box") } }); } } }); HERE; $box++; } ?> }); </script> if you solve this I'll send ya $10 via paypal I created a method for displaying an object's properties: Code: renderfunction = false; function showProperty (object, property) { document.write ('<td class="type">' + (typeof object[property]) + '</td>' + '<td class="name">' + property + '</td>'); document.writeln('<td class="value">' + ( (typeof object[property] != 'function') ? object[property] :( (property != 'showProperties') ? ( renderfunction ? object[property]() : ('<span class="self">NOT RENDERED</span>') ) : ('<span class="self">THIS</span>') ) ) + '</td>'); document.writeln('<td class="hasOwnProperty" >' + ( object.hasOwnProperty(property) ? "Local" : "Inherited" ) + '</td>'); if (typeof object[property] == 'function') { document.writeln ('<td class="function">' + object[property] + '</td>'); } else { document.writeln ('<td class="function"> </td>'); } } As long as renderfunction = false, the object is fine coming out of this function. However, if I change renderfunction to true, all my properties become undefined. Why isn't this working as I expect it to? How should I fix it? Thanks in advance, -Brian. |