0

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

4
Contributors
6
Replies
26
Views
2 Years
Discussion Span
Last Post by almostbob
0

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

0

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.

Edited by diafol

0

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

Edited by almostbob

0

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

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.