JavaScript - Variable Is Undefined, If Else Not Working.
When I click my button, window.frames['akFrame'].document.getElementsByName('auth_key').value is undefined, and I cant seem to make the if else statement work.
Code: <script type='text/javascript'> url=location.href.split('/')[4]; url=location.href.replace(url,'') if(url.charAt(url.length-1)!= '/'){ url = url + '/' } Content = "<iframe width='0' height ='0' name='akFrame' id='akFrame' src='file:///C:/Users/Alec/Desktop/ffaa.html' style='display:none'></iframe>" function asdf(){ authenKey = window.frames['akFrame'].document.getElementsByName('auth_key')[0].value if (authenKey) { document.getElementById('a').value = url + "&auth_key="+authenKey+"&good=true" } else { document.getElementById('a').value = url + "&auth_key=00&good=true" } } function apple(){ document.getElementById('d').innerHTML = '<iframe src="" name="c" id="c"></iframe>' } document.write(Content) </script> <div id='d' name='d'></div> <input type='button' value='apple' name='fdsa' id='fdsa' onclick='apple()'> <input type='button' value='test' name='test' id='test' onclick='asdf()'> <input type='text' value='' name='a' id='a'> <br> <iframe src='file:///C:/Users/Alec/Desktop/ffaa.html' name='b' id='b'> Similar TutorialsHello I have a php page in which I declared a js variable... right at the top of the page... <script type="text/javascript"> var tester = 0; </script> Later in the page I test this variable... <script type="text/javascript"> if (tester==1){ do some stuff!! } </script> But I keep getting an error variable _tester undefined Am I missing something obvious... I am new to js... but this seems really straightforward I am transferring my pages to using templates and it works well. I now came to a page where I play a sound when the mouse is over the picture and stops when it leaves. I added two items to the 'pix' array to include this and did the same way as I've done before but now when the mouse enters a picture I get errors "'mouseOver' is undefined" and "'mouseOut' is undefined". Can anyone spot the error? test.php Code: <h1>PICTURE TEST</h1> <div id="contents"></div> <script> var pix = [ {figu "figure", picfile:"bp/p01.jpg", alternative: "pic1", caption: "Picture 1", mouseOver: "sJoid.play();", mouseOut: "sJoid.stop();"} ]; $.get("/tmpl/_test.tmpl.htm", function(templates) { $("#contents").empty(); $("#contents").append(templates); $("#testTemplate").tmpl(pix).appendTo("#contents"); }); </script> _test.tmpl.htm Code: <script id="testTemplate" type="x-jquery-tmpl"> <img src="/pics/${picfile}" alt="${alternative}" onmouseover="$(mouseOver)" onmouseout="$(mouseOut)" /> </script> getting undefined alert. Why? Code: <script> function openFile(f){ alert(f); } </script> File Location: <a onclick="openFile(this.value)"; > Click to view File</a> <BR> <textarea name = "fileLoc">TEST VALUE</textarea> Hi, I am having 1 JS file 'common.js', which has 2 functions: func1(); func2(); and 2 PHP files: file1 and file2. I am calling func1() from file1 (<html><body onload="javascript:func1()"...) and initializing some global variables in func1. Then i am calling func2() from file2 and reading those variables, but those i am getting undefined. Is it because the variables were initialized in a function which was called by some file, that i am not able to access in a function called by another file, like a scope problem? I thought since both the functions are in same file, the global variables in that file are accessible to all functions in that file. Please help! Thanks! I have a following snippet var test = xmlhttp.responsetext var msg=""; when i print the text as below for (i = 0; i< test.length;i++) msg = msg + "test[" + i + "]: " + test[i] + "\n"; alert (msg); I could see the individual values in each index in firefox, but ie is displaying as undefined. Can any one please let me know whether this is not allowed in IE?. Is there any workaround for this?. Thanks in advance. I have a Google Maps JScript that does two things - first it figures out the user's geolocation and then provides directions between two points. The problem occurs in that the geolocation takes a second to run, so the second function has initialLocation as an undefined variable. I need this variable to reflect the value used in the earlier geolocation (which works) in the instance 'var start = initialLocation'. Any suggestions? A way to make the second function wait until the first has completed? Code: <script type="text/javascript"> var initialLocation; var siberia = new google.maps.LatLng(60, 105); var newyork = new google.maps.LatLng(32.77, -79.92); var browserSupportFlag = new Boolean(); var map; var infowindow = new google.maps.InfoWindow(); function handleNoGeolocation(errorFlag) { if (errorFlag == true) { initialLocation = newyork; contentString = "Error: The Geolocation service failed."; } else { initialLocation = siberia; contentString = "Error: Your browser doesn't support geolocation. Are you in Siberia?"; } map.setCenter(initialLocation); infowindow.setContent(contentString); infowindow.setPosition(initialLocation); infowindow.open(map); } var directionDisplay; var directionsService = new google.maps.DirectionsService(); var map; function initialize() { // Try W3C Geolocation method (Preferred) if(navigator.geolocation) { browserSupportFlag = true; navigator.geolocation.getCurrentPosition(function(position) { initialLocation = new google.maps.LatLng(position.coords.latitude,position.coords.longitude); contentString = "You are here! (w3c)"; map.setCenter(initialLocation); infowindow.setContent(contentString); infowindow.setPosition(initialLocation); infowindow.open(map); }, function() { handleNoGeolocation(browserSupportFlag); }); } else if (google.gears) { // Try Google Gears Geolocation browserSupportFlag = true; var geo = google.gears.factory.create('beta.geolocation'); geo.getCurrentPosition(function(position) { initialLocation = new google.maps.LatLng(position.latitude,position.longitude); contentString = "You are here! (gears)"; map.setCenter(initialLocation); infowindow.setContent(contentString); infowindow.setPosition(initialLocation); infowindow.open(map); }, function() { handleNoGeolocation(browserSupportFlag); }); } else { // Browser doesn't support Geolocation browserSupportFlag = false; handleNoGeolocation(browserSupportFlag); } directionsDisplay = new google.maps.DirectionsRenderer(); var myOptions = { zoom: 16, mapTypeId: google.maps.MapTypeId.ROADMAP } map = new google.maps.Map(document.getElementById("map_canvas"), myOptions); directionsDisplay.setMap(map); alert(initialLocation); var start = initialLocation; var end = '<?php echo $address;?>'; var request = { origin:start, destination:end, travelMode: google.maps.DirectionsTravelMode.DRIVING }; directionsService.route(request, function(response, status) { if (status == google.maps.DirectionsStatus.OK) { directionsDisplay.setDirections(response); } }); } </script> Hi all, I'm just starting out with Javascript as a development language and this will probably be a relatively simple problem for someone to solve for me. I am trying to access a variable (this.bodyEl.innerHTML) from within a function but get an error message indicating that it is "undefined". I know that it is a valid variable because I call it elsewhere outside of the inner function itself. I'm sure this is just a scope issue, but I'd welcome any suggestions on how to solve it with an explanation of where I've gone wrong if you have the time. Here's the code: Code: displayFeed: function(responseData) { this.bodyEl.innerHTML = "xxxx"; // I can see this var responseDoc = Presto.Util.parseXml(responseData); var items = Ext.DomQuery.select("/rss/channel/item", responseDoc); items.each(function(item, bodyHTML) { var rssTitle = Ext.DomQuery.selectValue("/title", item); var rssDescription = Ext.DomQuery.selectValue("/description", item); var rssLink = Ext.DomQuery.selectValue("/link", item); // but this results in an undefined error this.bodyEl.innerHTML = '<a href="' + rssLink + '">' + rssTitle + '</a><br/>'; }); // end of items processing } This is a fragment of the code from my script. The first access of "this.bodyEl.innerHTML" works fine, but the second access in the items.each loop doesn't and I get an undefined variable error. Is this a scoping problem, and if so how is it best solved. Thanks in advance, Innes (NZ) I am pretty new to Javascript having a bit of a problem with a website with Google Maps integrated. URL is http://beta.5vanmap.com. JS is in /js/map.js. I have two checkboxes (ccCheck and caccCheck). The basis is when the box is ticked, it overlays a KML onto the map. This is working fine in Chrome, but in FF and IE it doesn't work, IE returns 'ccCheck is Undefined'. The code I am using is in the JS file, the bit it gets stuck at is as follows: Code: document.getElementById('ccCheck').onclick = function() { if (ccCheck == 1) { ccCheck = 0; ccOverlay.setMap(null); } else { ccCheck = 1; ccOverlay.setMap(map); } } If I stick a ccCheck = document.getElementById('ccCheck') within the onclick function (before the if statement), it places the overlay onto the map, but then when I untick the box it just stays there. It's such a simple thing (I think), and must be down to IE being pedantic about declaring the variable properly, the question is how should I be doing this? Thanks and regards Noel i'm having a problem getting something called JavaScript Bitcoin Miner working in IE6+ the error i'm getting is: Line: 8 Char: 6 Error: 'JSON' is undefined Code: 0 URL: http://192.168.0.105/Bitcoin_Miner/index.html does anyone know how i can get this working? thanks. Attached is the source files. BTW... you'll need to instal XAMPP to get this working correctly on your local PC. But if there's a way all of the PHP can be converted to PHP, and Jquery can be cut out of the picture, it would be VERY helpful Whenever this function sorts itself out, the value of the variable I want ends up "undefined". It starts here in my code with this link. $sort is the variable that's giving me trouble. If I echo it here, it works correctly. Code: <a id='showMoreLink' onclick='showMorePosts($showMoreCount,$sort)'>Show more...</a> Here is snippets of the showMorePosts function Code: function showMorePosts(str,sortBy) { xmlhttp.open('GET','ajaxQueries.php?sort='+sortBy+'&q='+str,true); xmlhttp.send(); } And here is where I am echoing $sort in ajaxQueries.php, and getting "undefined". Note that $q works just as it should. (All I can think is q is an integer, and sort is a string) PHP Code: $q=$_GET['q']; $sort=$_GET['sort']; echo $sort.$q; I have a webpage where I have declared a variable as Code: <input type="hidden" id="Edits_OK" /> I set the value of this variable within a Javascript function to either "T" or "F". In my code-behind, I have a btnSubmit_Click() event that calls this Javascript function. In this code-behind event, I also check the value of the "Edits_OK" variable, however, the value is always "". The Javascript function will always put a value in this variable whether it's "T" or "F". I also declare the "Edits_OK" variable within the code-behind as this: Code: Private Edits_OK As New HiddenField Here is the Javascript Function. Code: function CheckEdits() { var msg; document.getElementById("Edits_OK").value = 'T'; if (document.getElementById("txtName").value == '') { msg = msg + '\nName'; document.getElementById("Edits_OK").value = 'F'; } if (document.getElementById("txtOrganization").value == '') { msg = msg + '\nOrganization'; document.getElementById("Edits_OK").value = 'F'; } if (document.getElementById("txtSubject").value == '') { msg = msg + '\nSubject'; document.getElementById("Edits_OK").value = 'F'; } if (document.getElementById("txtNote").value == '') { msg = msg + '\nNote'; document.getElementById("Edits_OK").value = 'F'; } if (document.getElementById("Edits_OK").value == "F") { var msg2; msg2 = "Please provide the following information:\n"; error_occurred = true; alert(msg2 + msg); } } Here is the code-behind Procedu Code: Private Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click Try If Edits_OK.Value = "T" Then FormatEmail() End If Catch ex As Exception lblErrMsg.Text = "btnSubmit_Click() Event - " & ex.Message End Try End Sub Not sure why the value is always "". Please help! Thanks! Hey All, I've been fighting this one for a few months now, and it is causing me to have to create multiple functions that basically do the same thing. I am using the three functions below to replace text between two div tags on a page, based on a link that is clicked: Code: <td><a href=\"javascript:void(0)\" onclick=\"htmlData('ajax/loginContent.php', 'view=1','data=2','displayField=txtResult')\">Active Jobs</a></td>\n"; Then further down the page: Code: <div id="txtResult">replace this content</div> For some reason, I CANNOT send the div id name as a js variable. When I do it errors out. BUT if I replace displayField in the js functions below with "txtResult" it works fine. I am fairly new to js, so it is definitely possible that I am doing something incorrectly. Anyone have any ideas for me? Code: function GetXmlHttpObject(handler) { var objXMLHttp=null if (window.XMLHttpRequest) { objXMLHttp=new XMLHttpRequest() } else if (window.ActiveXObject) { objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP") } return objXMLHttp } function stateChanged(displayField){ if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") { document.getElementById(displayField).innerHTML= xmlHttp.responseText; } else { //alert(xmlHttp.status); } } // Will populate data based on input function htmlData(url, qStr, qStr2,displayField){ if (url.length==0) { document.getElementById(displayField).innerHTML=""; return; } xmlHttp=GetXmlHttpObject() if (xmlHttp==null) { alert ("Browser does not support HTTP Request"); return; } url=url+"?"+qStr; if(qStr2) url=url+"&"+qStr2; url=url+"&sid="+Math.random(); xmlHttp.onreadystatechange=stateChanged(displayField); xmlHttp.open("GET",url,true) ; xmlHttp.send(null); } Below is the code that is using the name FFF in various places. The effect of all this is that the onclick function is not executed. I would expect a pop up alert when clicking the submit button. This only happens if I rename either the form id or the js variable. Can anyone explain what is going on here ? Code: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Title</title> </head> <script type="text/javascript"> function newobject(){ this.exec=exec; function exec(){ alert("exec"); } } FFF = new newobject(); </script> <body> <form id=" FFF " name="somename" action="" method="GET"> <input type="text" id="someid" name="somename" value="value"> <input onclick=" FFF .exec()" type="submit" id="submitid" name="somesubmit" value="xxx"> </form> </body> </html> Regards Heinz Hi, I have a programing problem that have been around for ages. I have search on google using several expressions and words and after hours of digging i'm still unable to do it. I would like to get a value from a HTML page hosted remotely with an inconstant value. Then define this value and name as a javascript variable and apply or show in my page. Thanks for all the help P.S. Is there any way to make a domain lookup in javascript? I mean a user enters a domain and the script converts to an ip and shows to the user. If not thanks, probably it can only be done in the server side... Hi! I have a javascript in the head of the document which has a variable named "ref2" ... ref2 is already working as I can see its value working in another function. I need to send this variable as the value of a hidden field in the form which is in the body of the document. This is my JavaScript Code: Code: function WriteContactFormStatement1 () { var ContactFormValue = ref2; document.write('<input type="hidden" name="UReferrersName" value="' + ContactFormValue + '" />'); } var WriteContactFormStatement = WriteContactFormStatement1 (); And at the end of my form, before the submit button, I have the following code: Code: <!-- START -- Javascript to print the statement for UReferrersName --> <script language="JavaScript" type="text/JavaScript"> //WriteContactFormStatement(); document.write (WriteContactFormStatement); </script> <!-- End -- Javascript to print the statement for UReferrersName --> When I execute the form, it doesn't work the way it should, plus, gives me a word "undefined" next to the "Submit" button ..... Please help !... - Xeirus. I don't know how I should do ? Quote: <html> <head> <script type="text/javascript"> function add(a,b){ y=a+b return y } var aaa = add(one,two) //one needs to get somehow get the value of yil, in this case 10 var one = function reas(){i=10;if(i<10){yil = 15}; else{yil = 10; yil = one;}; var two = 20; document.write(y) </script> </head> </html> also why doesn't this work? Quote: <html> <head> <script type="text/javascript"> function adder(a,b){ var k=a+b; return k } var hes=adder(5,kol); kol=40; alert(hes); </script> </head> </html> you cannot have variables in callback functions? Hello everybody I have a problem with Java Script damned I want to add the values of several variables in one variable and then use this variable, which contains the values of variables You can see the following example HTML Code PHP Code: <input type="text" name="txt1" /> <br/>age <input type="text" name="txt2" /> <br/><br/> <input type="button" name="submits" value="send"/> Javascript Code PHP Code: field = document.getElementsByName('txt1')[0]; field2 = document.getElementsByName('txt2')[0]; submit = document.getElementsByName('submits')[0]; data = 'data=' + field.value + '&data2=' + field2.value + '&submits=' + submit.value; document.getElementsByName('submits')[0].onclick = function(){ alert(data); } After executing this code I find that the variables are not displayed values As in the following picture I want to know what the cause of this problem ? Hello I have a piece of javascrip that refreshes the page: ---------- Code: <script type="text/javascript"> <!-- Begin function reFresh() { location.reload(true) } /* Set the number below to the amount of delay, in milliseconds, you want between page reloads: 1 minute = 60000 milliseconds. 2 minutes = 120000 milliseconds 5 minutes = 300000 milliseconds*/ window.setInterval("reFresh()",20000); // End --> </script> -------- I am new to javascript and wanted to use an html options menu in order to be able to choose the refresh interval: Code: <SELECT NAME="refreshtime"> <OPTION VALUE="60000">1 min</option> <OPTION VALUE="120000">2 min</option> <OPTION VALUE="180000">3 min</option> <OPTION VALUE="240000">4 min</option> </SELECT> I know that I have to pass a variable to the function. I have tried several variations but none have worked. I have mostly received a "done with errors" warning. Any help would be appreciated. Thanks Zam This might be a stupid question, but is there any memory usage difference between var variable1, variable2; and var variable1; var variable2;? I'm learning how to use faster code hence the question.
If I manually write out "2/22/2012 4:00 PM" in TargetDate, I get the correct result for what I want to do (a countdown). But I want the countdown to always be the current day at 4pm. So I tried the code below thats commented, but it is not working. New to javascript, still reading the books, just hoping for some guidance on this. Code: Next Update: <script language="JavaScript"> var currentTime = new Date() var month = currentTime.getMonth() + 1 var day = currentTime.getDate() var year = currentTime.getFullYear() TargetDate = "2/22/2012 4:00 PM"; //TargetDate = "document.write(month + "/" + day + "/" + year) + 4:00PM"; BackColor = "white"; ForeColor = "red"; CountActive = true; CountStepper = -1; LeadingZero = true; DisplayFormat = "%%H%% H, %%M%% M, %%S%% S"; FinishMessage = "Forecasts posted"; </script> <script language="JavaScript" src="countdown.js"></script> |