JavaScript - Not Working Online, But Works Well Offline
hi,
I have to dynamically add images into a div. I am able to do this and perform action on the image locally. If i host it to the server iam not able to see the image it only displays x. But i am able to perform some action on the newly added image. Following is the code i have tested offline and online Code: <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>JavaScript: Moving an Image</title> <script language="javascript" type="text/javascript"> var currImage=null; var maxWidth=100; var maxHeight=100; var fileTypes=["bmp","gif","png","jpg","jpeg"]; function preview(what) { var source=what.value; var imgW,imgH, path; var ext=source.substring(source.lastIndexOf(".")+1,source.length).toLowerCase(); for (var i=0; i<fileTypes.length; i++) if (fileTypes[i]==ext) break; if (i<fileTypes.length) { var ni = document.getElementById('myDiv'); var numi = document.getElementById('theValue'); var num = (document.getElementById('theValue').value -1)+ 2; numi.value = num; var newImg = document.createElement('img'); path = source.replace(/\\/g, '/'); // Fix Windows paths path = 'file://localhost/'+ path; path = path.toLowerCase(); newImg.src=path; alert(path); newImg.id="imgID"+num; ni.appendChild(newImg); var x=parseInt(newImg.width); var y=parseInt(newImg.height); if (x>maxWidth) { y*=maxWidth/x; x=maxWidth; } if (y>maxHeight) { x*=maxHeight/y; y=maxHeight; } newImg.width=x; newImg.height=y; } else { alert("THAT IS NOT A VALID IMAGE\nPlease load an image with an extention of one of the following:\n\n"+fileTypes.join(", ")); } } function removeImage() { var ni = document.getElementById('myDiv'); if (currImage==null) { alert("No image to remove"); } else if (currImage==0) { alert("Click the image to remove / No image to remove"); } else { alert(currImage.id); ni.removeChild(currImage); currImage=0; } } </script> <style> #myDiv { width: 200px; height: 240px; position: relative; margin: 20px auto 0px auto; border: 5px outset #000; overflow: hidden; background-color:pink; } </style> </head> <body > <div id="myDiv" > </div> <p> <input type="hidden" value="0" id="theValue" /> <input type="button" id="delete" value="Remove" onClick="removeImage()"> <input type="file" id="picField" onChange="preview(this)"> </p> </body> </html> Similar TutorialsHi everyone, My page with my slideshow (with lightbox integrated) is up and running fine on browsers like Firefox, IE, and Google Chrome... But Safari doesn't even show the slideshow. www.bobbivanstudio.com/work.php Any ideas why? Thanks in advance! I'm trying to create a navigation bar with four elements. If the element is currently selected it will have a "red" background image, if it is one of the other 3, it will have a "black" background image. my four tabs are 'timetable, homework, notifications and sport' I tried making 8 functions like the 2 below Code: function setTimeRed() { document.getElementById("time").style.ClassName = 'timetable_r'; } function setTimeBlack() { document.getElementById("time").style.ClassName = 'time_r'; } And then four blocks like this: Code: function changeTimeButton() { var timePath = new String(); timePath = document.getElementById("timetable").style.backgroundImage; if(timePath == "url(assets/img/tabs/time_black.png)" || timePath == "") { setTimeRed(); setHomeBlack(); setNotiBlack(); setSportBlack(); } else { } } finally, my html has this: Code: <div id="tabbar"> <ul id="tabs"> <a href"#" onclick="changeTimeButton()"> <li id="timetable" class="time_b"> <p>Timetable</p> </li> </a> <a href"#" onclick="changeHomeButton()"> <li id="homework" class="home_b"> <p>Homework</p> </li> </a> <a href"#" onclick="changeNotiButton()"> <li id="notifications" class="noti_b"> <p>Notifications</p> </li> </a> <a href"#" onclick="changeSportButton()"> <li id="sport" class="sport_b"> <p>Sport</p> </li> </a> </ul> </div> It works once then does nothing. Why, and how would I go about fixing this?? Please help! Title explains it really. The button I am using working perfecting in FireFox and Chrome but not in Internet explorer. (Yes I know someone that actually uses IE that pointed this out to me) The rest of the code works fine but just not the reset button I have. Something I have been needing to do is make the code call my images differently by putting them in an array so I don't mess this the sources of my images like I sloppily am here, but I am not sure if that will actually fix the problem I am have. The checkbox should both uncheck the box(if checked) AND change the picture but it only unchecks the box. Here is the relevant code let me know if you want me to post the whole thing. Code: function reset(){ cb1.checked=false; document.goodAfternoon.src=goodAfternoonFinal.src; } Code: "Check" me out;) <input type="checkbox" id="cb1" onClick="validate()" /> <img src="Good Afternoon.gif" name="goodAfternoon"> <input type="button" id="cb2" value="RESET" onClick="reset()"/> http://www.dyn-web.com/tutorials/iframes/refs.php I dont get it Hey guys, so I'm trying to make a single function that will check to make sure the e-mails in both fields match AND to make sure the e-mail is in proper format. Strangely, with both if statements included the form will return no messages at all. If I comment out the if statement checking for proper format, the if statement to check for matching e-mail works just fine. I want this all to be in one function because I'm wanting it to be executed onSubmit with the form. Let me know what you guys think, any help is greatly appreciated: Code: <html> <head> <script type="text/javascript"> function verifyEmail() { var x = document.forms["emailForm"]["enterEmail"].value; var y = document.forms["emailForm"]["confirmation"].value; var atpos = x.indexOf("@"); var dotpos = x.lastIndexOf("."); if (x != y) { var msg = "The email addresses entered do not match, please enter matching email addresses"; document.getElementById("error").innerHTML = msg; return false; } if (atpos < 1 || dotpos < atpos + 2 || dotpos + 2 > = x.length) { var msg = "Improper e-mail format"; document.getElementById("error").innerHTML = msg; return false; } else { return; } } </script> </head> <body> <form name="emailForm" onsubmit="return verifyEmail();" method="post"> E-Mail Address: <input type="text" name="enterEmail"> Confirm E-Mail Address: <input type="text" name="confirmation"> <input type="submit" value="Submit"> <span id="error"></span> </form> </body> </html> I am using javascript to change the buttons on my website so that they load up a different image when hot spots on the buttons are hovered over. Everything has been working great, but this morning I tried checking out my site and for some reason the javascript wasn't working in Chrome, or on my iphone using Safari. I tried it in IE and everything still works. Any ideas on why it would stop working for those two browsers? Here is the javascript: Code: function init(){ document.getElementsByTagName('area')[0].onmouseover=function(){ document.getElementById('crocodileImg').src='Images/crocodileHover.png'; this.onmouseout=function() { document.getElementById('crocodileImg').src='Images/crocodile.png'; } } document.getElementsByTagName('area')[1].onmouseover=function(){ document.getElementById('historyImg').src='Images/historyHover.jpg'; this.onmouseout=function() { document.getElementById('historyImg').src='Images/history.jpg'; } } document.getElementsByTagName('area')[2].onmouseover=function(){ document.getElementById('applyImg').src='Images/applyHover.jpg'; this.onmouseout=function() { document.getElementById('applyImg').src='Images/apply.jpg'; } } document.getElementsByTagName('area')[3].onmouseover=function(){ document.getElementById('applyImg').src='Images/membersHover.jpg'; this.onmouseout=function() { document.getElementById('applyImg').src='Images/apply.jpg'; } } document.getElementsByTagName('area')[4].onmouseover=function(){ document.getElementById('mediaImg').src='Images/mediaHover.jpg'; this.onmouseout=function() { document.getElementById('mediaImg').src='Images/media.jpg'; } } } if(window.addEventListener){ window.addEventListener('load',init,false); } else { if(window.attachEvent){ window.attachEvent('onload',init); } } I'm not sure if seeing the html the js is referencing will help at all, but here it is just in case: Code: <div id="crocodile"> <img id="crocodileImg" src="Images/crocodile.png" usemap="#crocodileImg" border="0" width="436" height="147" alt="Home" /> <map id="crocodileImg" name="crocodileImg"> <area id="crocArea" shape="poly" coords="41,9,23,21,20,58,40,78,72,78,82,86,88,112,117,129,140,126,257,126,274,107,274,79,401,78,420,66,419,26,404,6,369,7,366,0,294,0,291,7," href="index.html" alt="Home" title="Home" /> </map> </div> <div id="history"> <img id="historyImg" src="Images/history.jpg" usemap="#historyImg" border="0" width="350" height="256" alt="Home" /> <map id="historyImg" name="historyImg"> <area id="historyArea" shape="poly" coords="18,77,18,167,48,170,79,208,124,233,174,238,228,218,257,188,266,176,280,184,306,182,338,102,331,93,271,92,254,54,226,27,196,11,169,4,137,7,109,17,82,33,65,51,52,74," href="history2.html" alt="History" title="History" /> </map> </div> <div id="apply"> <img id="applyImg" src="Images/apply.jpg" usemap="#applyImg" border="0" width="280" height="223" alt="Apply" /> <map id="applyImg" name="applyImg"> <area id="applyArea" shape="poly" coords="17,43,17,57,15,72,20,84,39,83,43,79,56,76,62,62,65,49,77,42,76,36,70,22,65,14,52,14,45,21,38,24,31,32," href="apply.html" alt="Apply" title="Apply" /> <area id="membersImg" shape="poly" coords="112,49,97,55,77,71,64,89,61,99,47,104,44,123,44,149,46,164,57,168,70,172,82,186,102,197,125,206,146,208,168,201,188,189,201,177,208,166,231,165,254,165,260,150,262,133,262,113,254,97,238,95,221,97,213,97,205,85,197,74,183,63,172,55,159,51,145,48,134,47,"href="members.html" alt="Members" title="Members" /> </map> </div> <div id="media"> <img id="mediaImg" src="Images/media.jpg" usemap="#mediaImg" border="0" width="182" height="149" alt="Media" /> <map id="mediaImg" name="mediaImg"> <area id="mediaArea" shape="poly" coords="0,102,16,103,26,117,44,129,67,133,92,128,108,115,118,102,165,102,170,88,173,65,172,52,161,49,141,49,132,42,121,42,110,30,96,22,76,16,60,14,44,18,30,29,19,36,13,48,0,51," href="media.html" alt="Media" title="Media" /> </map> </div> Thank you for any replies. hi I am making a multimedia Cd presentation in Html and I was wondering if there is any way for the index.html to autorun not in a browser, but only in window without any buttons - like a popup widow ? I really need help, i am looking for solution for 3 days now, and I need this to be done tomorrow.. thanx I have a simple function to display an image when called, but if I try to rewrite the function to take the image as an argument, it stops working. I've looked at other function examples on the web but can't figure out what I am missing. Can anyone help? works: Code: <html> <script language="JavaScript"> var ImagePlusSign = '<img src="plus.jpg" name="slide" width="65" height="50"/>'; function FlashImagesThenWait() { document.getElementById("first").innerHTML = ImagePlusSign } setTimeout(FlashImagesThenWait, 1500); </script> <body>Hello. <div id="first"> </div> </body> </html> does NOT work : Code: <html> <script language="JavaScript"> var ImagePlusSign = '<img src="plus.jpg" name="slide" width="65" height="50"/>'; function FlashImagesThenWait(z) { document.getElementById("first").innerHTML = z } setTimeout(FlashImagesThenWait(ImagePlusSign), 1500); </script> <body>Hello. <div id="first"> </div> </body> </html> I am hoping someone can help me with a tweak to my current online quiz code. I know very little javascript but was thinking this might be an easy change. I need a funtion to get all the quiz answers ( I'm using radio buttons with values assigned) Values of each question answered will be added together Results are determined by 3 score ranges and displayed on the appropriate html page I have this code from a different quiz but not able to figure out how to change it to get the results I need. Please be gently with with!!! I am just learning. Code: function finish() { var ext = '.html'; var results = new Array("none","results1","results2","results3"); var nums = new Array(4); for(var i = 0; i < nums.length; i++) nums[i] = 0; for(var i = 1; i <= 9; i++) { var q = document.forms['quiz'].elements['question_'+i]; if(q[0].type=='checkbox') { var n = 0; } for(var j = 0; j < q.length; j++) { if(q[j].checked) { var a = q[j].value.split(','); for(var k = 0; k < a.length; k++) { nums[a[k]]++; } if(q[j].type=='radio') break; else n++; } if(j == q.length-1&&q[j].type=='radio') {nums[0]++;} } if(q[0].type=='checkbox'&&((document.forms['quiz'].elements['question_'+i+'_min']&&n<document.forms['quiz'].elements['question_'+i+'_min'].value)||(document.forms['quiz'].elements['question_'+i+'_max']&&n>document.forms['quiz'].elements['question_'+i+'_max'].value))) nums[0]++; } var j = new Array('0'); for (i in nums) if(nums[i]>nums[j[0]]){j=new Array(''+i);} else if(nums[i]==nums[j[0]])j[j.length] = i; //var o = '';for(var i in results)o+=results[i]+'='+nums[i]+'\n'; //alert(o); if(nums[0]!=0) { alert('You missed or incorrectly answered '+nums[0]+' questions!'); } else if(j[0]==0) { alert('No result could be determined.'); } else { location = results[j[0]]+ext; } } Please let me know if I am way off base or if there is a better way to attach the project. Most Grateful, TechPam hi guys, am trying to create online quiz with javascript,but am having problems,the code compiles but when i run nothing appears.i need help pliz. below is my code.please show where edited. <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"/> <title>Driving Theory Test</title> <script language="javaScript" type="text/javascript"> //Questions and answers var questions = new Array(); var answers = new Array(); var questionsAsked; var numberOfQuestionsAsked = 0; var numberofQuestionCorrect = 0; var currentQNumber = -1; //define question 1 questions[0] = new Array(); questions[0][0] = "For which purpose is a horn allowed to be used outside built up areas"; questions[0][1] = "As a warning signal"; questions[0][2] = "As an overtaking signal"; // give correct answer Answers[0] = "A"; //define question 2 questions[1] = new Array(); questions[0][0] = "What can be the effects of even small quantities of alcohol"; questions[0][1] = "Delayed reactions"; questions[0][2] = "Reckless driving"; questions[0][3] = "Impairment of hearing and vision"; // give correct answer Answers[1] = "A"; //define question 3 questions[2] = new Array(); questions[0][0] = "What can cause a dangerous situation"; questions[0][1] = "Cutting corners"; questions[0][2] = "Dipping your headlihgts too late"; // give correct answer Answers[2] = "B"; fuction reset quiz() { var indexCounter; currentQNumber = -1; questionsAsked = New Array(); for (indexCounter = 0; indexCounter < questions.length;indexCounter++) { questionsAsked[indexCounter] = false; } numberOfQuestionsAsked = 0; numberOfQuestionsCorrect = 0; } function answerCorrect(questionNumber, answer) { // declare a variable to hold return value var correct = false; //if ansa provided is same as ansa then correct ansa is true if (answer == answers[questionNumber]) { numberOfQuestionsCorrect++; correct = true; } // return whether the ansa was correct (true or false) return correct; } function getQuestion() { if (questions.length != numberOfQuestionsAsked) { var questionNumber = Math.floor(Math.random() * questions.length) while (questionsAsked[questionNumber] == true) { questionNumber = Math.floor(Math.random() * questions.length); } var questionLength = questions[questionNumber].length; var questionChoice; numberOfQuestionsAsked++; var questionHTML = "<h4>Question " + numberofQuestionAsked + "</h4>"; questionHTML = questionHTML + "<p>" + questions[questionNumber][0]; questionHTML = questionHTML + "</p>; for (questionChoice = 1;questionChoice < questionLength;questionChoice++) { questionHTML = questionHTML + "<input type=radio " questionHTML = questionHTML + " name=radQuestionChoice" if (questionChoice == 1) { questionHTML = questionHTML + " checked"; } questionHTML = questionHTML + ">" + questions[questionNumber][questionChoice]; questionHTML = questionHTML + "<br>" } questionHTML = questionHTML + "<br><input type= 'button'" questionHTML = questionHTML + " value='Answer Question'"; questionHTML = questionHTML + "name=buttonNextQ "; questionHTML = questionHTML + "onclick='return buttonCheckQ_onclick()'>"; currentQNumber = questionNumber; questionsAsked[questionNumber] = true; } else { var questionHTML = "<h3>Quiz Complete</h3>"; questionHTML = questionHTML + "You got " + numberOfQuestionsCorrect; questionHTML = questionHTML + " questions correct out of " questionHTML = questionHTML + " numberOfQuestionsAsked; questionHTML = questionHTML + "<br><br>Your rating is " switch(Math.round(((numberOfQuestionsCorrect / numberOfQuestionsAsked) * 10))) { case 0: case 1: case 2: case 3: questionHTML = questionHTML + "Try next time"; break; case 4: case 5: case 6: case 7: questionHTML = questionHTML + "Average"; break; default: questionHTML = questionHTML + "Excellent" } questionHTML = questionHTML + "<br><br><A " questionHTML = questionHTML + "href= 'Exampage.htm'><Strong>" questionHTML = questionHTML + "Start again</strong></A>" } return questionHTML; } </script> </head> <body> </body> </html> thnks in advance I have been playing with two options but just dont seem to get the coding right. Can someone please assist. Using ie9 but same problem with all browsers. first snippets. HTML: Code: <!-- Row 3, Col 3 purchase boxes --> <td colspan="1" height="120" align="left"> <input style="margin-left: 60px" type="text" name="bed_359" size="3" maxlength="3" onchange="calculateValue(this.form)" /> R359</td></tr> <!-- Row 10, Col 2 Order Value Box--> <td colspan="1" align="left"><input style="margin-left: 60px" type="text" name="total" size="10" onfocus="this.form.elements[0].focus()" /> </td></tr> javaScript: Code: // Function to calculate order value function calculateValue(orders) { var orderValue = 0; var value = 0; var itemPrice = 0; var itemQuantity = 0; // Run through all the product fields for(var i = 0; i < orders.elements.length; ++i) { // Get the current field formField = orders.elements[i]; // Get the fields name formName = formField.name; // Items price extracted from name itemPrice = parseFloat(formName.substring(formName.lastIndexOf("_") + 1)); // Get the Quantity itemQuantity = parseInt(formField.value); // Update the OrderValue if(itemQuantity >= 0) { value = itemQuantity * itemPrice; orderValue += value; } } // Display the total orders.total.value = orderValue; } Second code: Code: <!-- Row 9, Col 3 purchase boxes --> <td colspan="1" height="120" align="left"> <select style="margin-left: 60px" name="shirt" value="215" onchange="calculateValue(this.form)" /> <option value="0">0</option> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option></select> R215</td></tr> Java Code: // Function to calculate order value function calculateValue(orders) { var orderValue = 0; var value = 0; var itemPrice = 0; var itemQuantity = 0; // Run through all the product fields for(var i = 0; i < orders.elements.length; ++i) { // Get the current field formField = orders.elements[i]; // Get the fields name formValue = formField; // Items price extracted from name itemPrice = formField; // Get the Quantity itemQuantity = formField.selectedIndex; // Update the OrderValue if(itemQuantity > 0) { orderValue += itemQuantity * itemPrice } } // Display the total orders.total.value = orderValue.toLocaleString();; } Please help its probably something simple. Hi, I am trying to create a small art project where I want to track real-time visitors on my site. I am not very good at coding at all and am really struggling to know how to go about this To give you an idea of what I want to achieve; a basic example would be for every live visitor on my site, a circle would appear on the index page. I am aiming for the end product to be a very basic visualisation of live visitors on the site. So just a group of circles representing how many people are on the site. I am really struggling to get to grips with the coding needed and would hugely appreciate your help. Thank you very much for your time, Hi, I would like to display total online viewers on my website, how can i do this? thanks Hello Guys, I have been grabbing good ideas and code from everywhere to build an online order form. I have no skill programming, but i have managed to almost finish it. However i still have two thing I cant done. Hope you can help me. 1. If any product of the "Product #2 drop-down menu" is selected, then "shipping drop-down menu" should only have "Post office option" enabled. 2. If "Shipping drop-down menu" is 7-11 or Family Mart, then "Convenience store text box" should be enabled. You can check the whole code he http://dl.dropbox.com/u/21610873/code.txt Thanks I want to write a code. in which I select the AC Temperature By Radio Buttons. then I observe the Saving energy by Bar Graph with out submitting the Form. As I change the Input, automatically, Output should change what should be code in HTML or Javascript so that I may observe effect of the change of Temperature on saving energy. thanks. Since it is not clear to me where this post should fit in the forum, I'm just posting it here. Is it possible to create a tracking code that would allow me to: (1) a) Find out the identity of visitors to a certain website (a website unrelated to me and the visitors did not visit my website) b) The identity of those who recently searched for a certain keywords and/or... (2) Find out what websites certain individuals have recently visited (or visiting in real-time)? Hi, My project simulation takes lot of time so I cant monitor that continuously,. I want to send a SMS whenever some simulation is done/ progress / throws an error to my mobile through SMS when I'm away from the PC.. I dont know how to start with this.. pls tell me how to do this I want to use free online SMS service to do this.. Hello, I am trying to figure out how to write a quiz that has one question per html page, then once the choice has been selected, it will go to another page and tally a total at the end. Can this be done? And if so, can you point me in the right direction to research? Thanks in advance, Tanya I'm almost positive this question will make you groan "neewwwbie!".... I have a simple javascript that decides which 'div' to make visible, based on another element within the html. What i want now is to create a page where someone can view all the possible divs in that javascript that might appear and edit them (including href's, ital, bold and image tags.) Much like a CMS system, but the information is within a javascript, not a database. I would assume it's possible. What's a good way to approach this? It sounds like Javascript cannot edit .txt files. Is there a way to have the user access and edit an .xml file and just pull that into the div tag, instead of text? |