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. Edited by HDRebel88
Link to comment
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).

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.