Jump to content


Running jQuery AJAX only once

  • Please log in to reply
1 reply to this topic

#1 HDRebel88

  • Members
  • PipPip
  • Member
  • 26 posts

Posted 28 July 2013 - 06: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:
$.ajax({ url: "jscheck.php",
        context: document.body,
        success: function(data){
            if (data=='yes')
            window.location = '/films/'+filmTitle;
            { }
Here's my php:
echo $_SESSION['jsCheck'];
This works but the page just keeps looping and looping.

Edited by HDRebel88, 28 July 2013 - 07:04 PM.

#2 nogray

  • Members
  • PipPipPip
  • Advanced Member
  • 930 posts
  • LocationSan Francisco CA

Posted 29 July 2013 - 07: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).


0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users