Jump to content


Photo

Running jQuery AJAX only once


  • Please log in to reply
1 reply to this topic

#1 HDRebel88

HDRebel88

    Member

  • Members
  • PipPip
  • 24 posts

Posted 28 July 2013 - 01:50 PM

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, 28 July 2013 - 02:04 PM.


#2 nogray

nogray

    Advanced Member

  • Members
  • PipPipPip
  • 930 posts
  • LocationSan Francisco CA

Posted 29 July 2013 - 02:27 PM

A better approach would be to load the page as if javascript was turned off and add the controls using javascript. (http://en.wikipedia....ive_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).


NoGray.com





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com