Hi
I am having a bit of a problem with some JavaScript and PHP. The script (below) works exactly as wanted, but if I run it with the latest version of IE, the JavaScript appears to be looping and it makes the link flash, and it can only be stopped by closing the IE browser. I Firefox, it's probably doing the same, but without any flashing.
What I would like is a means of detecting that the JavaScript has already been run, and therefore shouldn't be run again.

<head>
    <script>
        if(navigator.geolocation) {
            navigator.geolocation.getCurrentPosition(function(position) {
                var lat = position.coords.latitude
                var lon = position.coords.longitude
                document.location = "prayer-times.php?latitude=" + lat + "&longitude=" + lon
            });
        }
    </script>

<?php
    error_reporting(0);

    $lat = $_GET['latitude'];
    $lon = $_GET['longitude'];

        echo "Latitude: " . $lat . "<br/>";
        echo "Longitude: " . $lon . "<br/>";
?>

I'm really hoping that there is a DaniWeb expert out somewhere who can offer a solution?
Thanks
Terry

Is prayer-times.php the same page - ie are you redirecting to the same page?

If it is then just check if certain things are in the query string and only run the JavaScript if they are not.

Member Avatar for diafol

You can check for runs in the console. A ...

console.log('say something...');

In an appropriate place in your code should help. Ensure that you have the browser console open when you load the page to see if it is in fact looping.

Is there a reason why you're echoing out php data in the head section?

//EDIT

Ah hold on, just noticed DA's comment about prayertimes being the same page. If this is the case, then yes it will loop ad infinitum.

Not sure why you need PHP involved here, you can run it all on the client (js). You seem to be forcing a reload (redirect) just in order to pass the js data to php.

runonce

<?php if(!$_GET['runonce']) {echo <<<redir
<script>
 if(navigator.geolocation) {
  navigator.geolocation.getCurrentPosition(function(position) {
   var lat = position.coords.latitude
   var lon = position.coords.longitude
   document.location = "prayer-times.php?runonce=done&latitude=" + lat + "&longitude=" + lon }); }
 </script>
redir; }
else echo <<<page ?>
ENTIRE DISPLAY PAGE HERE
<?php page; ?>

html5 geo* runs better in javascript, but prayertimes is a complicated calc script, the times change relative to both lat and long, the base is not 0,0, and direction is great circle, the intersection of two circular functions, Qibla is difficult to plot

the above script will send a tiny page, just the script to get co-ordinates, then refresh with the complete page, once only
If the page is built where ENTIRE DISPLAY PAGE HERE is

Hi almostbob
Of course I could always say "Why didn't I think of that?". But thankfully you've got there first.
And your answer looks promising as well. I'll check it out later today and come back here with the result.
Thanks
Terry

its real easy to fix code, writing em takes thought

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.