Jump to content

Running jQuery AJAX only once


HDRebel88

Recommended Posts

I'm trying to run a check via jQuery/AJAX to see if the user loading the page has Javascript turned on... if they do I need to reload the page, after setting a session variable in PHP so I can activate javascript based controls that I need to have turned off if the user doesn't have javascript turned on. The controls turned off is the default...

 

Here's my AJAX call:

$(document).ready(function(){
$.ajax({ url: "jscheck.php",
        context: document.body,
        success: function(data){
            if (data=='yes')
            {
            window.location = '/films/'+filmTitle;
            }
            else
            { }
        }});
});
Here's my php:

<?php
session_start();
$_SESSION['jsCheck']="yes";
echo $_SESSION['jsCheck'];
?>
This works but the page just keeps looping and looping.
Link to comment
https://forums.phpfreaks.com/topic/280608-running-jquery-ajax-only-once/
Share on other sites

A better approach would be to load the page as if javascript was turned off and add the controls using javascript. (http://en.wikipedia.org/wiki/Progressive_Enhancement)

 

The reason your page keeps loading is because your success function basically keeps redirecting the page. You can use a php if statement to check if the session has been set or not. If not, print out the ajax request.

 

Also, you can also simply set the default to show the javascript controls by default (since most people have javascript support) and use the noscript and meta refresh tags to set the session to no javascript controls (you still would need to check if the session is set before printing out the noscript tag).

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.