1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Question about setting a cookie

Discussion in 'JavaScript' started by j_o, Oct 18, 2011.

  1. #1
    Hey Everyone,

    I have a php page that I created a login for it uses a jquery drop down to show the form that displays the login and I am trying to fix it to work also if js is disabled. Right now it is set so that if java script is enabled and you click on a link (href="#") then the box drops down. However if js is disbaled i need that link to instead show (href="login.php").

    My thought was to check if javascript is enabled by setting a cookie and then php will just check if that cookie is created. So i go and set my cookie using:
    
    <SCRIPT LANGUAGE="JavaScript">
    var dt = new Date(), expiryTime = dt.setTime( dt.getTime() + 1800000 );
    document.cookie = 'JsCheck=' + 'Javascript Check' + ';expires=' + dt.toGMTString();
    </SCRIPT>
    
    Code (markup):
    Then right under that I do:

    
    <?php
    if(isset($_COOKIE['JsCheck'])){
    die('JS Enabled');	//The die is just here for testing purposes
    }
    ?>
    
    PHP:
    First I make sure that the cookie is not set and then with js enabled on my browser I go to my page. Once loaded I check my cookies and firefox reports back to me that my cookie was successfully created. However my php check did not trigger and kill the loading of the page. Once that is done though if I go and reload the page with the cookie already set php picks up the cookie and kills the refresh.

    Is there a reason why this check does not work upon the initial loading when the cookie is set?


    Thanks in advance,

    Joe
     
    j_o, Oct 18, 2011 IP
  2. HowDoYou

    HowDoYou Well-Known Member

    Messages:
    443
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    130
    #2
    Check to see if there browser supports JS:
     
    PHP:
    
    $results = get_browser(); 
    if ($results["javascript"] == 1) { 
    echo "You have javascript"; 
    }
     
    Code (markup):
    99% will be enabled.

    But if it is important to know if it is enabled then on load have your JS change the link using Jquery. by default have the link point where it would go without javascript.


    
    $("a[href^='http://google.com']") 
       .each(function() 
       {  
          this.href = this.href.replace(/^http:\/\/javascript\.google\.com/,  
             "http://google.com"); 
       }); 
    
    
    Code (markup):
     
    HowDoYou, Oct 18, 2011 IP
    j_o likes this.
  3. JohnnySchultz

    JohnnySchultz Peon

    Messages:
    277
    Likes Received:
    4
    Best Answers:
    7
    Trophy Points:
    0
    #3
    that code will not run if javascript is disabled in the first place..
     
    JohnnySchultz, Oct 19, 2011 IP
  4. j_o

    j_o Well-Known Member

    Messages:
    516
    Likes Received:
    2
    Best Answers:
    1
    Trophy Points:
    113
    #4
    That was the point. If it ran JS was enabled otherwise JS was disabled. But HowDoYou's suggestion of using get_browser() is much better.

    Thanks for the help.
     
    j_o, Oct 19, 2011 IP
  5. j_o

    j_o Well-Known Member

    Messages:
    516
    Likes Received:
    2
    Best Answers:
    1
    Trophy Points:
    113
    #5
    I went to try get_browser however browscap is not set in my php.ini (which for some reason my hosting company wont let me change) so that option is out. How would I go about replacing the link as you said. the two links would be
    
    <a id="open" class="open" href="login.php">Log In</a> <!--JS disabled -->
    <a id="open" class="open" href="#">Log In</a> <!--JS disabled -->
    
    Code (markup):
     
    j_o, Oct 19, 2011 IP
  6. HowDoYou

    HowDoYou Well-Known Member

    Messages:
    443
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    130
  7. j_o

    j_o Well-Known Member

    Messages:
    516
    Likes Received:
    2
    Best Answers:
    1
    Trophy Points:
    113
    #7
    Perfect, thank you!
     
    j_o, Oct 19, 2011 IP
  8. JohnnySchultz

    JohnnySchultz Peon

    Messages:
    277
    Likes Received:
    4
    Best Answers:
    7
    Trophy Points:
    0
    #8
    as for my experience, get_browser() does not work everytime depending on the server. but if it works for you, its fine..
     
    JohnnySchultz, Oct 20, 2011 IP
  9. Rukbat

    Rukbat Well-Known Member

    Messages:
    2,908
    Likes Received:
    37
    Best Answers:
    51
    Trophy Points:
    125
    #9
    Detecting and accounting for no Javascript is trivial:
    
     <script>
     alert('js is on');
     </script>
     <noscript>
     <a href="http://www.mysite.com">noscript link</a>
     </noscript>
    
    Code (markup):
    If Javascript is on you get the alert, if not you get the link. It works in every modern browser.
     
    Rukbat, Oct 21, 2011 IP