Hi everyone
I have a bit of JavaScript code that I need a bit of help with. It concerns Local and Global scoped variables. In the code sample below, and in the function showLocation, are the two problem variables. If I un-comment the two document.writes, then obviously I can see the two values. But if I leave them commented and go to the bottom of this snippet, then the same two variables are blank.
I have read somewhere online that if you declare a variable outside a function, it is a Global variable and can be used within a function, presumably similar to my code below. But it doesn't work?
What I am trying to do is use GeoLocation to get my Latitude and Logitude (which this snippet does, but only within the Function) and then make use of these two variables later on within the same PHP/JavaScript page.
So my question is this.. How can I make use of a variable, created within a Function, outside of that Function?
Thanks
Terry

        var latitude = "";
        var longitude = "";
        period = "oneday";
        formDate = "<?php echo $formDate;?>";
        offset = "<?php echo $summerTime;?>";

        function showLocation(position) {
            latitude = position.coords.latitude;
            longitude = position.coords.longitude;

//document.write("Latitude 1: " + latitude + "<br/>"); // Variable correct if un-commented
//document.write("Longitude 1: " + longitude + "<br/>"); // Variable correct if un-commented

        }

        function getLocation(){
            if(navigator.geolocation){
                var options = {timeout:60000};
                navigator.geolocation.getCurrentPosition(showLocation,errorHandler,options);
            }else{
                alert("Sorry, browser does not support geolocation!");
            }
        }

document.write("JavaScript-Latitude 2: " + latitude + "<br/>"); // Variable is blank
document.write("JavaScript-Longitude 2: " + longitude + "<br/>"); // Variable is blank

document.write("JavaScript-Period: " + period + "<br/>");
document.write("JavaScript-FormDate: " + formDate + "<br/>");
document.write("JavaScript-Offset: " + offset + "<br/>");

It looks like you had to edit out some code in your post, so it's difficult to be sure what the problem is. But I suspect that your variable names 'latitude' and 'longitude' are being used by some code elsewhere. That feeling is reinforced by the fact that they're also the names of properties of the 'position.coords' object. Change your variable names to 'myLat' and 'myLong' and I bet the code works as expected.

Hi rtrethewey
The code I showed was the complete JavaScript, except for the opening/closing tags. The variables "latitude" and "longitude" arn't used anywhere else, although that is what I eventually need to do. Where do you suggest I change the variable names? In the function? Or else where?
Terry

Member Avatar for diafol

If you're not running the function showLocation, then the write statements outside the function will be blank.

If you call the function after the "external" writes, again they will show blanks.

Change the name of the variables as I suggested everywhere in your code where you intend to refer to your particular global variables.

Variables don't change on their own. Either there's more JavaScript somewhere than the code that you posted (which never calls showLocation() or getLocation()) or the browser's geo-location library somehow conflicts with those variable names. I find it's always best to use a unique prefix on variable names when mixing my code with other people's.

Hi again rtrethewey

I have changed the variable names as suggested, but it still doesn't work. And just to allay any worries about extra code, here is the entire file, called salat.php. There is definitely no other code involved. Like you, I wish there was a bit of "forgotten" code somewhere, because that might have been where the problem is? But there isn't!

And as you can see, the script doesn't actually do anything yet, mainly because I need the 2 variables (myLatitude and myLongitude) outside of the function showLocation() so I can use the values elsewhere.

<!DOCTYPE HTML>
<html lang="en">
<head>
    <?php
        // SUMMERTIME ------------------------------------------------------------------------------
        $summerTime = "";
        $ThisYear = (date("Y")); 
        $MarStartDate = ($ThisYear."-03-25"); 
        $OctStartDate = ($ThisYear."-10-25"); 
        $MarEndDate = ($ThisYear."-03-31"); 
        $OctEndDate = ($ThisYear."-10-31"); 

        //work out the Unix timestamp for 1:00am GMT on the last Sunday of March, when BST starts 
        while ($MarStartDate <= $MarEndDate) { 
            $day = date("l", strtotime($MarStartDate)); 
            if ($day == "Sunday") {
                $BSTStartDate = ($MarStartDate); 
            } 
            $MarStartDate++; 
        } 

        $BSTStartDate = (date("U", strtotime($BSTStartDate))+(60*60)); 

        //work out the Unix timestamp for 1:00am GMT on the last Sunday of October, when BST ends 
        while ($OctStartDate <= $OctEndDate) { 
            $day = date("l", strtotime($OctStartDate)); 
            if ($day == "Sunday") { 
                $BSTEndDate = ($OctStartDate); 
            } 
            $OctStartDate++; 
        } 

        $BSTEndDate = (date("U", strtotime($BSTEndDate))+(60*60)); 

        $now = time(); 

        if (($now >= $BSTStartDate) && ($now <= $BSTEndDate)){ 
            $summerTime = 1;
        }else{
            $summerTime = 0;
        }

        // FORMDATE --------------------------------------------------------------------------------
        $formDate = date("l j<\s\up>S</\s\up> F Y");
    ?>

    <script type="text/javascript">
        var myLatitude = "";
        var myLongitude = "";
        period = "oneday";
        formDate = "<?php echo $formDate;?>";
        offset = "<?php echo $summerTime;?>";

        function showLocation(position) {
            myLatitude = position.coords.latitude;
            myLongitude = position.coords.longitude;

//document.write("Latitude 1: " + myLatitude + "<br/>");
//document.write("Longitude 1: " + myLongitude + "<br/>");

        }

        function errorHandler(err) {
            if(err.code == 1) {
                alert("Error: Access is denied!");
            }else if( err.code == 2) {
                alert("Error: Position is unavailable!");
            }
        }

        function getLocation(){
            if(navigator.geolocation){
                var options = {timeout:60000};
                navigator.geolocation.getCurrentPosition(showLocation,errorHandler,options);
            }else{
                alert("Sorry, browser does not support geolocation!");
            }
        }

document.write("JavaScript-Latitude 2: " + myLatitude + "<br/>");
document.write("JavaScript-Longitude 2: " + myLongitude + "<br/>");

document.write("JavaScript-Period: " + period + "<br/>");
document.write("JavaScript-FormDate: " + formDate + "<br/>");
document.write("JavaScript-Offset: " + offset + "<br/>");

    </script>
</head>

<?php
    echo "PHP-Formdate: ".$formDate;
?>

<body>
&nbsp;
</body>
</html>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.