Javascript coding problem...

by WillR
5 replies
Warriors,

I'm hoping someone in here can help?!

I have a website and on several of the pages there is a javascript code in between the head tags. Now let's say someone were to click on a link on my website and go to another page that did NOT have that javascript. From that page they then clicked a link that took them back to the page with the javascript.

I hope that makes sense?!

My question is this. What is the best way for me to disable the javascript when the person returns to that page? In other words, I only want the javascript to be enabled the first time they visit that page and disabled if they return to that page.

I would want to only do this for a certain amount of time though so if they came back to that page the next day the javascript would then be enabled again on the page.

Any ideas? I know this could probably be done using cookies however I would prefer if there was some way around this where cookies would not be necessary.
#coding #javascript #problem
  • Profile picture of the author Headfirst
    Well, I'm sure you could do this in pure JS, but I'm just not comfortable enough with it to make a suggestion.

    My solution would be to drop a cookie after the javascript runs and then check for the existence of the cookie on the page load. If the user has the cookie the javascript won't be passed to the browser.

    Take this code with a grain of salt, I haven't tested it, but this should work:
    PHP Code:
    <?php
    if(!isset(COOKIE["returning"]))
    {
        echo 
    "PUT YOUR JAVASCRIPT HERE";
        
    setcookie("returning","I'm Back"time() + 604800);
    } else {
    setcookie("returning","I'm Back"time() + 604800);
    }
    ?>
    <!-- PUT THE REST OF YOUR SITE HERE -->
    That should set the cookie for one week from the users last visit to the site.
    It should also reset the cookie every time the user visits so they never get the javascript again (unless they wait more than a week before returning)

    Hope that works or at least points you in the right direction.

    Apparently the read cookie code in that php block is causing the post to time out.

    Ok, preface the word COOKIE on the second line with $_
    {{ DiscussionBoard.errors[3702165].message }}
  • {{ DiscussionBoard.errors[3702181].message }}
  • Profile picture of the author oknoorap
    you can try $_COOKIE or $_SESSION, I prefer $_SESSION
    {{ DiscussionBoard.errors[3725714].message }}
    • Profile picture of the author lordspace
      also keep in mind that the code that outputs a cookie must be before any other content because cookie is basically another header variable.

      So you will have this:

      wrong way (will get warnings)
      <html>
      <head>
      .... the suggested check from above
      </head>
      ....
      </html>

      right way
      do a check in php => $js_ok = 1;
      <html>
      <head>
      .... the suggested check from above
      ... and here if ($js_ok) { .... }
      </head>
      ....
      </html>
      Signature

      Are you using WordPress? Have you tried qSandbox yet?

      {{ DiscussionBoard.errors[3796645].message }}
  • Profile picture of the author majick
    You might consider doing it via IP instead. (is that a serverside cookie?)
    This code will write a file with the time according to the IP and check if it is within the last hour and if so not output the javascript.

    <?php
    $userip = $_SERVER["REMOTE_ADDR"];
    $ipfile = "iptemp/".$userip.".txt";
    $currenttime = time();
    if (file_exists($ipfile)) {$fh = fopen($ipfile,"r"); $timestamp = fgets($fh); fclose($fh);
    if ($timestamp > ($currenttime + 3600)) {echo "JAVASCRIPT CODE";}
    }
    else {$fh = fopen($ipfile,"w"); fwrite($fh,$currenttime); fclose($fh); echo "JAVASCRIPT CODE";}
    ?>

    of course not forgetting to create the directory iptemp.
    note: untested i just hammered this out.
    {{ DiscussionBoard.errors[3904439].message }}

Trending Topics