Javascript code not working, please help...

Discussion in 'JavaScript' started by CLMMafia, Nov 19, 2012.

  1. #1
    Hello,

    I have the following code

    	function geo(){		navigator.geolocation.getCurrentPosition(showPosition);	}	function showPosition(position){		document.getElementById("HIDDENlatitude").style.display = "";		document.getElementById("HIDDENlongitude").style.display = "";		document.getElementById("GEOlatitude").innerHTML = position.coords.latitude;		document.getElementById("GEOlongitude").innerHTML = position.coords.longitude;				return GETnumber();		return GETstreet();	}	function GETnumber(){	    var xmlhttp = new XMLHttpRequest();	    xmlhttp.open("GET", "/mods/latlng.php?lat="+position.coords.latitude+"&long="+position.coords.longitude+"&number", true);	    xmlhttp.send();	    document.getElementById("addressnumber").value = xmlhttp.responseText;	}	function getPAGE(){	    var GETstreet = new XMLHttpRequest();	    xmlhttp.open("GET", "/mods/latlng.php?lat="+position.coords.latitude+"&long="+position.coords.longitude+"&street", true);	    xmlhttp.send();	    document.getElementById("addressstreet").value = xmlhttp.responseText;	}
    PHP:
    GETnumber and GETstreet don't seem to work.

    Can anyone please help?
     
    CLMMafia, Nov 19, 2012 IP
  2. CLMMafia

    CLMMafia Member

    Messages:
    93
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    28
    #2
    If anyone helps me, I'll send them $20 via PayPal
     
    CLMMafia, Nov 20, 2012 IP
  3. SedNaX

    SedNaX Active Member

    Messages:
    1,326
    Likes Received:
    59
    Best Answers:
    0
    Trophy Points:
    90
    #3
    Your code is very difficult to read. Please include some newlines, otherwise I can't read it.

    If you use AJAX, try a library like jQuery or Prototype. And to debug JS code, use the developer toolkit by chrome (press f12 in chrome). It gives you JS errors.
     
    SedNaX, Nov 21, 2012 IP
  4. NoamBarz

    NoamBarz Active Member

    Messages:
    242
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    58
    #4
    From what I can tell, you have the statement "return GETnumber();" twice, one after the other.
    Remove one.
     
    NoamBarz, Nov 22, 2012 IP
  5. Rukbat

    Rukbat Well-Known Member

    Messages:
    2,908
    Likes Received:
    37
    Best Answers:
    51
    Trophy Points:
    125
    #5
    You have

    return GETnumber();
    return GETstreet();

    You can only return a single value. You can cheat by creating 2 global variables (declare them outside any function) and set them equal to the 2 functions (one variable for each function), then

    return;

    Then your number and street variables will be available to your other code. (showPosition is a callback function - don't return things with it.)

    I'd also base all this code on

    if (navigator.geolocation){
    }

    If the user's computer doesn't do geolocation, your code won't barf.
     
    Rukbat, Nov 22, 2012 IP
  6. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,999
    Best Answers:
    253
    Trophy Points:
    515
    #6
    Rukbat's right -- first thing I noticed... you can only return ONE value. Once you say return... it, well, RETURNS -- anything AFTER return is never run!

    Though since NEITHER function is actually returning a value, why are you calling them via RETURN?

    It's also helpful to make sure that you define a function in the code BEFORE you call it -- so your source order in addition to being an illegible mess due to a lack of carriage returns, is also in the wrong order.

    I would also advise against making functions for things that have a single call inside them -- the overhead of an extra function call typically outweighs any advantages you might think you are getting in code size from functions like your geo() one. Probably also not a good idea to be messing around with STYLE in your scripting, since that's CSS job -- at which point swap a class on a parent element to make styling easier in the long run.

    Also, without seeing the markup this is being run atop, the scripting is pretty much meaningless gibberish to us.
     
    deathshadow, Nov 27, 2012 IP