JavaScript - Insufficient Explanation Given. Unable To Call The Code.
I am learning javascript from this site, and one of the programs code given is now explained to the point that I can not use it. I don't know how to call it. I would be glad if someone would examine the code to see how it should be applied.
Date and Time Scripts Frank Similar TutorialsHi, Firstly, I would like to introduce myself. I am new to the application framework in sencha and JS. Acutally i have to pop an image on click of the text. I checked some of the same codes and wrote this code in sencha javascript. can any one please check what's wrong with this code and let me know how to call this function from html code. function showEditWindow() { var win = new Ext.Window({ title: 'image', width: 100, height: 100, html: '<img src=\"p1.png\"></img>', }); win.show(); return win; }; Hi , I am inserting an image into a span tag by click on button and resize the image using resize_image_height_weight function. I am using two function to do this task. setImage setImage_w3Validation setImage function resize the image but fail on w3validation on my webpage( As per w3c there is no attribute onload for image tag.) To fix out this issue I am using setImage_w3Validation function which pass on w3validation but unable to call resize_image_height_weight function. My purpose is to resize the image. Kindly suggest me how to do it? <html> <head> <script TYPE="text/javascript" language="javascript"> function resize_image_height_weight(id, hgt, wdth){ //alert(id); Obj=document.getElementById(id); myImage = new Image(); myImage.src = Obj.src; var heights = myImage.height; var widths = myImage.width; // alert("Height=>" + heights + " Width=> " + widths); if(heights > hgt || widths > wdth) { if(heights > widths) { var temp = heights/hgt; var new_width = widths / temp; new_width = parseInt(new_width); heights = hgt; widths = new_width; } else { var temp = widths/wdth; var new_height = heights / temp; new_height = parseInt(new_height); heights = new_height; widths = wdth; } } Obj.height = heights; Obj.width = widths; } function setImage(){ document.getElementById("IM0").innerHTML ="<center><img src=\"http://www.google.co.in/intl/en_com/images/srpr/logo1w.png\" id=\"img1\" alt=\"\" height=\"125\" width=\"125\" onload=\"resize_image_height_weight('img1',125,125);\"><\/center>"; } function setImage_w3Validation(){ document.getElementById("IM1").innerHTML ="<center><img src=\"http://www.google.co.in/intl/en_com/images/srpr/logo1w.png\" id=\"img2\" alt=\"\" height=\"125\" width=\"125\"><script type=\"text/javascript\">document.getElementById('img2').onload = function(){resize_image_height_weight('i12',125,125);};<\/SCRIPT><\/center>"; //I wanna to do the task using this one function to resize img for w3validation } function clear_page(){ document.getElementById("IM0").innerHTML =""; document.getElementById("IM1").innerHTML =""; } </script> </head> <body> <div class="img3"><span id="IM0"></span></div> <div class="img3"><span id="IM1"></span></div> <input type="button" name="btn1" value="Image Onload W3C Fail" onclick="setImage();"> <input type="button" name="btn2" value="Image Onload W3C Pass" onclick="setImage_w3Validation();"> <input type="button" name="btn2" value="Clear" onclick="clear_page();"> </body> </html> Hi, I have some javascript that I am using to get information from a SharePoint Site Options table (name and value) and I am adding this to my sharepoint sites (headers, title bar etc.). the javascript code in the heading of the document is as follows: Code: <script language="'javascript'" type="text/javascript"> //wait until client object model dependencies are loaded before executing our code ExecuteOrDelayUntilScriptLoaded(getWebProperties, "sp.js"); var webProperties; function getvalue(value) { return webProperties.get_item((value)); } function getWebProperties() { var clientContext = new SP.ClientContext.get_current(); webProperties = clientContext.get_web().get_allProperties(); clientContext.load(webProperties); clientContext.executeQueryAsync(Function.createDelegate(this, this.getWebPropertiesSucceeded), Function.createDelegate(this, this.onQueryFailed)); } function getWebPropertiesSucceeded() { var allProps = webProperties.get_fieldValues(); document.getElementById('lblName').innerHTML= webProperties.get_item('ProjectName'); document.getElementById('link').innerHTML= webProperties.get_item('siteURL'); var customProp = ""; //make sure the property is there before using it. if(webProperties.get_fieldValues().CustomSite_Version != undefined) { var customProp = webProperties.get_fieldValues().CustomSite_Version; } } function onQueryFailed(args, sender) { //handle errors here } </script> Could it be because I am trying to call two sets of values in a row? I have the following lines: Code: document.getElementById('lblName').innerHTML= webProperties.get_item('ProjectName'); document.getElementById('link').innerHTML= webProperties.get_item('siteURL'); and maybe it is because I am trying to get more than one set of information? now in the aspx part of the document, I want to call one of the variables in a couple of places and I am using this: Code: <div id="lblName"></div> It works great in the first instance but when I try to use it again, it doesn't show up and I don't know why. If I delete the first one, it shows up in the second location. does anyone who is a programmer see where I may be going wrong? Our SharePoint programmer quit and I am now in the frying pan so to speak. thanks all. Code: <script type="text/javascript"> var K_Thomas = "Kurt Thomas" , E_Turner = "Evan Turner" , P_George = "Paul George" , B_Davis = "Baron Davis"; var G_Vasquez = "Greivis Vasquez" , G_Dragic = "Goran Dragic" , K_Love = "Kevin Love" , V_Carter = "Vince Carter"; var L_Odom ="Lamar Odom" , JasonSmith="Jason Smith" , D_Jordan = "DeAndre Jordan" , J_ONeal = "Jermaine O'Neal"; var playername = prompt("Find Player Salary","First Name Last Name"); if (playername = K_Thomas) { alert("Kurt Thomas - $1.3 M 2012 Bucks"); } else if (playername = E_Turner) { alert("Evan Turner - $4.48 M 2012++ Bucks [$2 M Paid 2013]"); } else if (playername = P_George) { alert("Paul George - $1.71 M 2012++ Bucks"); } else if (playername = B_Davis) { alert("Baron Davis - $13.44 M 2012+ Bucks [$5.44 M Paid 2012]"); } else if (playername = G_Vasquez) { alert("Greivis Vasquez - $1.02 M M 2012++ Bucks"); } else if (playername = G_Dragic) { alert("Goran Dragic - $1.97 M 2012 Bucks"); } else if (playername = K_Love) { alert("Kevin Love - $3.88 M 2012 Bucks"); } else if (playername = V_Carter) { alert("Vince Carter - $17.52 M 2012 Bucks [$2 M Paid 2012]"); } else if (playername = L_Odom) { alert("Lamar Odom - $8.2 M 2013 Bucks [$4 M Paid 2012 & $4 M Paid 2013]"); } else if (playername = JasonSmith) { alert("Jason Smith - $1 M 2012 Bucks"); } else if (playername = D_Jordan) { alert("DeAndre Jordan - $10.1 M 2015 Bucks"); } else if (playername = J_ONeal) { alert("Jermaine O'Neal - $0.3 M 2012 Bucks"); } else { alert("Player Not Found."); } </script> So, whenever I run this code it either always shows up with Kurt Thomas's alert or with all of them. (I've tried using both document.write and alert) I don't know what I'm doing wrong with the conditions, but I'm new to javascript and it could be something simple. The Mozilla error console didn't find anything. Just for clarification I want it to happen so that when I type in a player's first and last name it brings up their salary information. Thanks in advance for your help. Hi I am currently reading through an example in my book and I'm stuck on a function which is passed 'that' as it's parameter. What I'll do is type the code and then my question's so I can explain myself a little better. Code: Code: <head> <script type="text/javascript"> var myImages = new Array("usa.gif","canada.gif","jamaica.gif","mexico.gif"); function changeImg(that) { var newImgNumber = Math.round(Math.random() * 3); while (that.src.indexOf(myImages[newImgNumber]) != -1) { newImgNumber = Math.round(Math.random() * 3); } that.src = myImages[newImgNumber]; return false; } </script> </head> <body> <img name="img0" src="usa.gif" border="0" onclick="return changeImg(this)" /> <img name="img1" src="mexico.gif" border="0" onclick="return changeImg(this)" /> Now what I cannot grasp is how does 'this' relate to 'that'? You return to the onclick handler the function name with the parameter as (this). The function parameter is (that) and I see that 'that' is used within the conditional part of the while statement. What I do not understand is why is 'this' used as the parameter in the onclick event's function, when in the actual function it is 'that'. I hope I have explained myself well enough and I look forward to any responses you may have. Thank-you. LC I don't really understand why a.d() fails in the following, or rather why a.d can't access local vars in a, or how to rewrite this so that it can PHP Code: window.a = (function () { var b = '<br>hello '; var c = function (){ document.write(b+'from c') // in this scope we can access the local vars of a }; c() // this will work and write hello from c document.write(b) // this will work and write hello from b return{c:c} })(); a.d = function () { document.write(b+'from d') // even though as far as I can tell I have added d to the object a, } // this still can't access the local vars of a... why not? how can I change that? a.d() // doesn't work :( a.c() // this works too because we returned c in a's return statement Hey Guys, I have been trying to work out this problem for the last couple of days have having found long way around of doing it but i was wondering why this way isn't working. I have attached to following code but can't seem to get it to show me the average which i am looking for which is 17.2 i have tried a number of things and mostly it is just curiosity as i have also pasted the deadline for this piece of work and was wondering is someone would be able to enlighten me on why its just giving me the output. 'The average height is NaN' (Not a Number) I know that it is probably something stupid like iam not declaring something as numeric or it might be that the whole think is wrong. I would very much appreciate any-help that people can give me but please don't direct me to another post. Code: <HTML> <HEAD> <TITLE> Plant Experiments </TITLE> <SCRIPT LANGUAGE = "JavaScript"> var plantHeights = [15,16,17,18,19]; var plantNumbers = [2,1,5,4,3]; var plant = new Array(5); for (var plantt = 0; plantt < plant.length; plantt = plantt + 1) { plant[plantt] = plantHeights[plantt] * plantNumbers[plantt]; } document.write('The heigh is ' + (plantNumbers[plant] + plantHeights[plant]/plant)); </SCRIPT> </HEAD> <BODY> </BODY> </HTML> Many Thanks, Tom I have devised the following constructor based loosely on the observer pattern Code: Observer = function (ConditionIsTrue , codeToExecute){ var observer = this , ConditionWasMet = false , CheckIfReady = function () { if (ConditionIsTrue()) { if(!ConditionWasMet) codeToExecute(); ConditionWasMet = true; } else { if (ConditionWasMet) ConditionWasMet = false; } }; Loop.call(observer , CheckIfReady) observer.speed(1) }; It works fine, no problems that I know of... But the most interesting thing happened when I attached it to my lib and ran it through the http://closure-compiler.appspot.com/home compiler Code: $['Observer'] = function (//...etc... For the first time the compiler has added something to my global scope Code: var i=!1; window.$=function(){//...etc... ... Observer:function(j,k){var h=i;Loop.call(this,function(){j()?(h||k(),h=!0):h&&(h=i)});this.speed(1)} } })(); Can someone explain to me why the var ConditionWasMet had to be exported to the global scope? Was it already in the global scope? That would confuse me considering the observer can be called from multiple instances without conflict... But I don't want any surprise conflicts jumping out at me later, I appreciate the consideration. hi guys, l am currently studying web technologies on a part time basis, and learning javascript. l got a very useful website, but still l dont understand certain things like this one: Quote: When a function has performed an assignment, it may provide a value that other functions would need, for any reason you judge necessary. When a function produces a value an makes it available to other functions, such a function is said to return a value. To define a function that returns a value, just before the closing curly bracket, type the return keyword followed by the value that the function returns. The value should be a valid and expected type. Because JavaScript is not a type-checking language, meaning it hardly checks anything you are doing, it is your responsibility to make sure that your function returns the right value. Here is a function that returns a value: function rectangleArea(length, height) { var area; area = length * height * 3.14159; return area; } Even if a function returns a value, it can still be called like any other function. If a function returns a value, its name can be assigned to a variable to give a value to, or change the value of, the variable. Here is an example: Code: <Script Language="JavaScript"> function rectangleArea(length, height) { var area; area = length * height * 3.14159; return area; } function displayArea() { var l, h, rectArea; l = 52.05; h = 46.55; rectArea = rectangleArea(l, h); document.write("The area of the is ", rectArea); } </Script> <Script Language="JavaScript"> displayArea(); </Script> what l understand from the code is, l have 2 functions, first function is a return function and has got a value, but where is this value going? l mean what's happing with it? l mean l cant see this return value in the second function, why? l ma also not understand why we need to have 2 functions, why not just 1 ?/?? can someone explain me one by one the lines maybe? thanks Hi, I am hoping I just need to be pointed in the right direction with this. I have Page1. When Page1 body onloads it uses Ajax to call PartA Within PartA I have a message board so members can write messages which will be sent to my database in PartA[1] and immediately posted for view on to PartA[2]. As I want to have my request to the server updating regularly I want to have PartA[2] on a timed loop to refresh - I do not need the content of PartA[1] to refresh. So the order of events would look like this: Page1 | onload call | v PartA / \ V V PartA[1] PartA[2] (loads once) (constantly refreshes) What I am not sure about is that I have <body> and <head> attributes in Page1 only. I have already used my body onload to call PartA (and can't use it to call PartA[2] before PartA has loaded anyway). I do not want the user to have to click a button or do anything to call up PartA[2]. So my question is how would I get PartA[2] to automatically load within PartA? I hope I have made this clear, but if I haven't let me know and I will try again. Hi all! I created a form where users are allowed to delete a specific row of data by clicking the "cancel" button from the drop down menu. However, I have a problem deleting my data using the javascript. Apparently, I only able to delete the first row of the data but not for the rest! The following is the code i have: PHP Code: <html> <body style="background-color:#E0FFFF;"> <script language="javascript" > <!-- hide function submitRequest(val) { document.forms[0].submit(); } // end hide --> </script> <?php $con = mysql_connect("localhost","root",""); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("my_course", $con); $result = mysql_query("SELECT * FROM Courses ORDER BY Year, Sem, CourseCode"); $result1 = mysql_query("SELECT * FROM Courses ORDER BY Year, Sem, CourseCode"); echo "<table border='1' cellpadding='2' cellspacing='0'> <tr> <th>CourseCode</th> <th>CourseName</th> <th>Year</th> <th>Sem</th> <th>Grade</th> <th>Option</th> </tr>"; ?> <?php while($row = mysql_fetch_array($result)) { echo "<tr>"; echo "<td>" . $row['CourseCode'] . "</td>"; echo "<td>" . $row['CourseName'] . "</td>"; echo "<td>" . $row['Year'] . "</td>"; echo "<td>" . $row['Sem'] . "</td>"; echo "<td>" . $row['Grade'] . "</td>"; echo "<td>"; ?> <?php ?> <form name="form1" action="submitDelete.php" method="post"> <select name="cancel" onchange="submitRequest(this.value);"> <option value=> </option> <option value="<?php echo $row['CourseName'];?>">Cancel</option> </select> </form> <?php echo "</td>"; echo "</tr>"; } echo "</table>"; mysql_close($con); ?> <br/> <br/> </body> </html> Thanks in advance! Hi guys, My brain is not working. May be I need a coffee break. I need your expertise. Below is the code holding the textarea. Look for the element with the id : themessage Code: <table width="100%" border="0"> <tr><td rowspan="4"><input type="button" name="send_email" id="send_email" style="height:50px; width:100px;" value="SEND" onClick="return err_check();" /></td><td><span class="que">From : </span></td><td colspan="3"><?php echo $from_email; ?><input type="hidden" name="from_mail" id="from_mail" value="<?php echo $from_email;?>"/><input type="hidden" name="camp_id" id="camp_id" value="<?php echo $camp_id;?>"/></td></tr> <tr><td><span class="que">To : </span></td><td colspan="3"><input name="to_email" id="to_email" style="width:250px;" /></td></tr> <tr><td><span class="que">CC : </span></td><td colspan="3"><input name="cc_email" id="cc_email" style="width:250px;"/></td></tr> <tr><td><span class="que">Subject : </span></td><td colspan="3"><input style="width:300px;" name="subject" id="subject" /></td></tr> <tr><td rowspan="1" colspan="2"> </td><td><input type="checkbox" name="ori_pdf" id="ori_pdf" /> PDF Quotation</td><td> </td><td> </td></tr><tr><td colspan="2"><span class="que">Credit Application</span></td><td><input type="checkbox" name="corporate" id="corporate"/>Corporate</td><td><input type="checkbox" name="individual" id="individual" />Individual</td><td><input type="checkbox" name="cash" id="cash" />Cash Account</td> </tr> <tr> <td colspan="2" rowspan="3"></td><td><input type="checkbox" name="tabloid" id="tabloid" />Tabloid Example</td> <td><input type="checkbox" name="broadsheet" id="broadsheet" />Broadsheet Example</td></tr> <tr><td><input type="checkbox" name="colmt" id="colmt" />Column Sizes Tabloid</td> <td><input type="checkbox" name="colmb" id="colmb" />Column Sizes Broadsheet</td></tr> <tr><td><input type="checkbox" name="maps" id="maps" />Maps / Distribution</td><td colspan="2" align="right">External Attachments <input id="upload_file" name="upload_file" type="file"/> </td></tr> <tr><td colspan="2"><span class="que">Message :</span></td><td colspan="3"> <textarea id="themessage" name="themessage" rows="15" cols="80" style="width: 100%"> </textarea> </td></tr> </table> Now on a button click, I'm trying to pass some values which one of them happens to be what ever that is entered in the textarea. For some reason, the stupid JS is not reading. But I think it is me that is stupid, not JS. Below is the code i'm running to get the value. Code: var message = document.getElementById('themessage').value; alert(message); The alert simply returns blank. There is an alert, but no value even where there is text in the text area. Can someone please shed some light on this. pretty please http://www.codetoad.com/javascript/e...rm_element.asp I tried to use the code in the above link but it is not changing the button to enabled. http://realistichostings.com/b/searc...?cat=Adhesives when i change the value in the select i am wanting the 'add' button to become enabled. i get the following erro in eror console.. Error: missing ; before statement Source File: http://realistichostings.com/b/searc...?cat=Adhesives Line: 1, Column: 14 Source Code: document.form1.3552.disabled=false; I am restarting a thread because as a result of acting on all of the suggestions I have received the nature of the issue has changed. I am trying to create a button and add it to the DOM under IE7. The code works fine under Firefox: Code: var editButton = document.createElement("button"); editButton.name = "editCitation" + rowNum; try { editButton.type = 'button'; } catch(e) { // IE7 throws this exception alert("citTable.js: gotAddCit: unable to set type for <button name='editCitation" + rowNum + "' type=" + editButton.type); } editButton.onclick = editCitation; editButton.appendChild(document.createTextNode('Edit Citation')); td1.appendChild(editButton); I am creating a <button> rather than an <input type=button> because a <button> permits HTML in the button label. Most of my buttons support Alt keyboard shortcuts, so I want to underline the appropriate letter in the label. According to the HTML 4.01 specification, which is as close to a standard as IE7 supports, a <button> can be one of three types: submit, reset, or button, and the default is submit. Since I wish to create a <button type=button> I therefore, at least theoretically, need to be able to set the value of the type attribute. However IE7 throws an exception at that point, although, interestingly, when I interrogate the value of editButton.type in the catch it is set to button. So how do I reliably set the type of a button under IE7? Hello. I'm trying to set up a login formular with PHP and JavaScript/jQuery. Currently it sends an alert box, if some information are wrong. If correct, it refreshes the page, and everyone's happy. However, I noticed upon the first visit, I cannot log in. Lets say I go to www.mysite.com and try to login, it displays a blank alert box, when it's suppose to display the username, that I supplied, and is wrong... however, it's blank. BUT! If I go to another page and login with false informations, an alert box with the wrong username pops up - if I login with correct informations, it works. How come it won't work on first visit? What am I doing wrong here? Pressing F5 won't work either - I have to manually switch to another page for it to work. Any suggestions and ideas are welcome and appreciated. I'll supply the codes necessary for anyone to help me, but at this point I'm completely blank on which of my codes may be the problem. EDIT - IMPORTANT INFORMATION! Now THIS is weird! I just noticed it's because there's not www. in the URL. There has to be www. for it to work, why is this? I forced the client to have www. in the URL with .htaccess, but I'd still like to know, why this problem occurs without it. I'm relatively new to javascript and I'm having some issues in adding linebreaks i.e. working <br> tags via DOM manipulation. An example of where I cant get it to work is included in the function code below. The html file the script is used in contains one div with an id of pfield. If its helpful the full program is available at http://www.templeofmick/wumpus3.htm Code: function printHints() { var vFlags = [0, 0, 0]; //Wumpus, Pit, bat var wFlag = 0; //Use flags to insure each hint isnt printed multiple times var pFlag = 0; var bFlag = 0; var node; var i; var pbreak = document.createElement("br"); var turnP = document.createElement("p"); var idString = ("turn" + turn); turnP.setAttribute("id", idString); document.getElementById("pfield").appendChild(turnP); for (i = 0; i < 3; i++) { vFlags = checkRoom(currentMap[currentRoom][i]); if (vFlags[0] === 1) { wFlag = 1; } if (vFlags[1] === 1) { pFlag = 1; } if (vFlags[2] === 1) { bFlag = 1; } } node = document.createTextNode("You are in room " + currentRoom + "."); document.getElementById(idString).appendChild(node); document.getElementById(idString).appendChild(pbreak); node = document.createTextNode("From here you can travel to room : " + currentMap[currentRoom][0] + "," + currentMap[currentRoom][1] + " and " + currentMap[currentRoom][2] + "."); document.getElementById(idString).appendChild(node); document.getElementById(idString).appendChild(pbreak); if (wFlag === 1) { node = document.createTextNode("The acrid stench of a nearby wumpus fills your delicate nostrils."); document.getElementById(idString).appendChild(pbreak); document.getElementById(idString).appendChild(node); } if (pFlag === 1) { node = document.createTextNode("The whistling of the wind indicates that floors are in short supply nearby."); document.getElementById(idString).appendChild(pbreak); document.getElementById(idString).appendChild(node); } if (bFlag === 1) { node = document.createTextNode("You can hear the mailicious gossiping of giant bats coming from a nearby room."); document.getElementById(idString).appendChild(pbreak); document.getElementById(idString).appendChild(node); } node = document.createTextNode("What now ? (M)ove or (S)hoot"); document.getElementById(idString).appendChild(pbreak); document.getElementById(idString).appendChild(node); } edit: I further identified the problem. Hi, I'm new to JavaScript and I'm having problem with appending a div node returned from a function. Here's where it crashed: Code: var div=document.getElementById("dashboard_secondary_cat_list"); var s=get_secondary_cat_node(attributes); alert("b4 appendin:"+s); div.appendChild(s); alert("done appendin"); alert("done appendin"); never gets called because the previous line crashes. alert("b4 appendin:"+s); shows: b4 appendin:[object HTMLDivElement] http://jsfiddle.net/FZ44M/3/ Thats my jsfiddle. I'm trying to get the gallerynav to sort the thumbnails based on their class. It does not work at all. I've gone over line by line but I can't see the mistake. When I run JSLink i get the error: Error: Problem at line 37 character 14: Cannot set property 'first' of undefined Implied global: $data 13,14, arr 14,15,25, jQuery 28, $ 30 Unused variable: read_button 30 "$", r 32 "read_button" I appreciate any help. for some reason I am unable to read the file contents, or it could be something else, I added in some extra code to see where the script got to and I only get to see '1' on the output. Is there something else I have missed ? I am wanting to ready each line one by one and replace a phrase with another, then output the new line. I visit the 1.txt file in my browser and the file shows fine. var txtFile = new XMLHttpRequest(); txtFile.open("GET", "http://mysite.com/convertJS/1.txt", true); txtFile.onreadystatechange = function() { document.write("1<br>"); if (txtFile.readyState === 4) { // document is ready to parse. document.write("2<br>"); if (txtFile.status === 200) { // file is found document.write("3<br>"); allText = txtFile.responseText; lines = txtFile.responseText.split("\n"); document.write(lines + "<br>"); // I shall be altering the text in each line, search and replace, so only need to be able to read one line at a time. } } } txtFile.send(null); // close file. sorry that i used the quote method to post, but when i used [ code ] it corupted the text as the document . write lines?! I am trying to get a div's content to change using javascript but for some reason it is not working and i get no errors looking at the error console. the code that is waiting for the content... Code: <div style="font-weight: bold;">count = <div id="desc" style="float: left;"></div> descriptions to do.</div><br clear="all" /> the code that should change the content.... Code: <? echo("<script type=\"text/javascript\"> document.getElementById('desc').value='" . $count . "'; </script>"); ?> when i view the source code the javascript lines show like so... Code: <script type="text/javascript"> document.getElementById('desc').value='373'; </script> do i have the javascript correct ? |