www.webdeveloper.com
Results 1 to 9 of 9

Thread: Java hide/show Links

  1. #1
    Join Date
    Feb 2012
    Posts
    38

    Java hide/show Links

    I was wondering if there was a way to make a Link Disapear After clicked on it So if a person goes back to the website the link is gone for X amount of time and is based on Login so i believe through Cookies, But the problem i been havng is i made it when a player gose to it and the link isent there BUT once the timer is up it changes the page to a blank page with the link there but all i want it to do is Just show up so for example


    If a player clicks on the website they must wait 10 - 15secs before the link appears to attack BUT i want them to be able to browse through the website and come back in 10 - 15secs Then the link will be there Compaired them sitting there on that page waiting..

    I been looking every where to get this to work but cant figure it out Please help

  2. #2
    Join Date
    Nov 2010
    Posts
    1,083
    this seems to work, although it may be a little excessive for your needs:

    Code:
    <html>
    <head>
    <script type="text/javascript">
    var int;
    
    function setCookie(pLabel, pVal, pSecs){
    document.getElementById("test").style.display="none"
     var tExpDate=new Date();
     tExpDate.setTime( tExpDate.getTime()+(pSecs*1000) );
     document.cookie= pLabel + "=" +escape(pVal)+
       ( (pSecs==null) ? "" : ";expires="+ tExpDate.toGMTString() );
    var int=setInterval(checkCookie,1000)
    }	
    
    function getCookie(c_name)
    {
    var i,x,y,ARRcookies=document.cookie.split(";");
    for (i=0;i<ARRcookies.length;i++)
    {
      x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
      y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
      x=x.replace(/^\s+|\s+$/g,"");
      if (x==c_name)
        {
        return unescape(y);
        }
      }
    }
    
    function checkCookie(){
    var cname=getCookie('MyCookie');
      if (cname!=null && cname!="")  {
      document.getElementById("test").style.display="none"
        }
    else {
    document.getElementById("test").style.display="block"
    clearInterval(int);
    		}
    }
    </script>
    </head>
    <body onload="checkCookie()">
    <div id="test"><a href="javascript: void(0)" onclick="setCookie('MyCookie', 'SomeValue',15);">a link</a></div>
    </body>
    <html>

  3. #3
    Join Date
    Feb 2012
    Posts
    38
    *Eyes widen* wow i got lots to learn lol :P
    so im guessing this line

    tExpDate.setTime( tExpDate.getTime()+(pSecs*1000) );

    set the time from written the it starts the code in 1sec?
    i am still a rookie and wanna learn as much as i can so if you have time do u think u could explain how it works lol i dont want to be one of those people that ask how u do something then just copy and paste i wanna learn

    Quote Originally Posted by xelawho View Post
    this seems to work, although it may be a little excessive for your needs:

    Code:
    <html>
    <head>
    <script type="text/javascript">
    var int;
    
    function setCookie(pLabel, pVal, pSecs){
    document.getElementById("test").style.display="none"
     var tExpDate=new Date();
     tExpDate.setTime( tExpDate.getTime()+(pSecs*1000) );
     document.cookie= pLabel + "=" +escape(pVal)+
       ( (pSecs==null) ? "" : ";expires="+ tExpDate.toGMTString() );
    var int=setInterval(checkCookie,1000)
    }	
    
    function getCookie(c_name)
    {
    var i,x,y,ARRcookies=document.cookie.split(";");
    for (i=0;i<ARRcookies.length;i++)
    {
      x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
      y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
      x=x.replace(/^\s+|\s+$/g,"");
      if (x==c_name)
        {
        return unescape(y);
        }
      }
    }
    
    function checkCookie(){
    var cname=getCookie('MyCookie');
      if (cname!=null && cname!="")  {
      document.getElementById("test").style.display="none"
        }
    else {
    document.getElementById("test").style.display="block"
    clearInterval(int);
    		}
    }
    </script>
    </head>
    <body onload="checkCookie()">
    <div id="test"><a href="javascript: void(0)" onclick="setCookie('MyCookie', 'SomeValue',15);">a link</a></div>
    </body>
    <html>

  4. #4
    Join Date
    Nov 2010
    Posts
    1,083
    Quote Originally Posted by shadowkiller View Post
    so im guessing this line
    tExpDate.setTime( tExpDate.getTime()+(pSecs*1000) );
    set the time from written the it starts the code in 1sec?
    kind of. actually, the best place to set the time that the cookie lasts for is in the function call, which is on the onclick of the link, here:
    Code:
    setCookie('MyCookie', 'SomeValue',15)
    that 15 means that the cookie will expire after 15 seconds.

    the function names are kind of self-explanatory. the setInterval at the end of the setCookie function calls te checkCookie function every 1000 milliseconds (ie, every second) to see if the cookie has expired yet. if it has, the code shows the link and cancels the interval (otherwise it keeps calling that function, which would be harmless but wasteful)

    the getCookie function is a little elaborate, written (by someone else) to check all of the cookies and tell the function that called it if the cookie exists - if you know the page is only ever going to create one cookie this could be simplified substantially.

    the reason why I think this code is excessive for your use is that if you just want to hide a link while the page is loaded you can do that easily with setTimeout, but then if you want to make sure that if they navigate away from the page then return before the "time limit" then the link is still hidden, cookies are probably the way to go.

    But be aware that people can (and do) disable cookies in their browser, which is why I think it should be used as a last resort.

  5. #5
    Join Date
    Feb 2012
    Posts
    38
    Quote Originally Posted by xelawho View Post
    kind of. actually, the best place to set the time that the cookie lasts for is in the function call, which is on the onclick of the link, here:
    Code:
    setCookie('MyCookie', 'SomeValue',15)
    that 15 means that the cookie will expire after 15 seconds.

    the function names are kind of self-explanatory. the setInterval at the end of the setCookie function calls te checkCookie function every 1000 milliseconds (ie, every second) to see if the cookie has expired yet. if it has, the code shows the link and cancels the interval (otherwise it keeps calling that function, which would be harmless but wasteful)

    the getCookie function is a little elaborate, written (by someone else) to check all of the cookies and tell the function that called it if the cookie exists - if you know the page is only ever going to create one cookie this could be simplified substantially.

    the reason why I think this code is excessive for your use is that if you just want to hide a link while the page is loaded you can do that easily with setTimeout, but then if you want to make sure that if they navigate away from the page then return before the "time limit" then the link is still hidden, cookies are probably the way to go.

    But be aware that people can (and do) disable cookies in their browser, which is why I think it should be used as a last resort.
    Yeah i understand that Cookies can be disabled i am actally making a online RPG so at the Register Page i will define that cookies must be enabled to play this lol

    Also thank you for All the information I am gonna go start testing differnt things i do have Differnt Cookies in there for example Login and password cookies

    Sorry was wondering also what is SomeValue do i leave that alone?

  6. #6
    Join Date
    Feb 2012
    Posts
    38
    This is my code I wanted it to work on All the Links i have 3 differnt links so far and thought that it could all work on the same cookie but guess not how can i turn this into setting more Cookies Without Writing this 3 or more time over and over lol

    I know the code is all over and a huge mess with be cleaning it up once i learn the proper way with to make configs pages lol.

    Code:
    <head>
    <script language="javascript">
    
    var int;
    
    function setCookie(pLabel, pVal, pSecs){
    document.getElementById("test").style.display="none"
     var tExpDate=new Date();
     tExpDate.setTime( tExpDate.getTime()+(pSecs*1000) );
     document.cookie= pLabel + "=" +escape(pVal)+
       ( (pSecs==null) ? "" : ";expires="+ tExpDate.toGMTString() );
    var int=setInterval(checkCookie,1000)
    }	
    
    function getCookie(c_name)
    {
    var i,x,y,ARRcookies=document.cookie.split(";");
    for (i=0;i<ARRcookies.length;i++)
    {
      x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
      y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
      x=x.replace(/^\s+|\s+$/g,"");
      if (x==c_name)
        {
        return unescape(y);
        }
      }
    }
    
    function checkCookie(){
    var cname=getCookie('MyCookie');
      if (cname!=null && cname!="")  {
      document.getElementById("test").style.display="none"
        }
    else {
    document.getElementById("test").style.display="block"
    clearInterval(int);
    		}
    }
    
    </script>
    
    </head>
    
    <?php 
     // Connects to your Database 
     mysql_connect("localhost", "root") or die(mysql_error()); 
     mysql_select_db("underworld") or die(mysql_error()); 
     
     $name = 'name';
     
     //checks cookies to make sure they are logged in 
     if(isset($_COOKIE['ID_my_site'])) 
     { 
     	$username = $_COOKIE['ID_my_site']; 
     	$pass = $_COOKIE['Key_my_site']; 
     	 	$check = mysql_query("SELECT * FROM users WHERE username = '$username'")or die(mysql_error()); 
     	while($info = mysql_fetch_array( $check )) 	 
     		{ 
     
     //if the cookie has the wrong password, they are taken to the login page 
     		if ($pass != $info['password']) 
     			{ 			header("Location: login.php"); 
     			} 
     
     //otherwise they are shown the admin area	 
     	else 
     			{ 
     			} 
     		} 
     		} 
     else 
     
     //if the cookie does not exist, they are taken to the login screen 
     {			 
     header("Location: login.php"); 
     } 
     
     $att = mysql_query("update * FROM users set exp = '110' WHERE username = '&username'");
     
      $data1 = mysql_query("SELECT * FROM mobs WHERE ID = '1'") 
     or die(mysql_error());
     $test = mysql_fetch_array( $data1 );
     
       $data1 = mysql_query("SELECT * FROM mobs WHERE ID = '2'") 
     or die(mysql_error());
     $test2 = mysql_fetch_array( $data1 );
     	 
     ?> 
     <body onload="checkCookie()">
      <center>
    <table border="1">
     	<tr>
     <td><? Print "<b>Name:</b> ".$test['name'] . " ";  ?></td>
     <td><? Print "<b>Level:</b> ".$test['level'] . " ";  ?></td>
     <td><? Print "<b>Attack:</b> ".$test['attack'] . " ";  ?></td>
     <td><? Print "<b>Deffence:</b> ".$test['deffence'] . " ";  ?></td>
          <td>
    <div id="test"><a href="bgmob.php" onclick="setCookie('MyCookie', 'SomeValue',15);">Attack</a></div>
    </td>
    </table>
      <br>
      <table border="1">
     <tr>
     <td><? Print "<b>Name:</b> ".$test['name'] . " ";  ?></td>
     <td><? Print "<b>Level:</b> ".$test['level'] . " ";  ?></td>
     <td><? Print "<b>Attack:</b> ".$test['attack'] . " ";  ?></td>
     <td><? Print "<b>Deffence:</b> ".$test['deffence'] . " ";  ?></td>
          <td>
    <div id="test"><a href="bgmob.php" onclick="setCookie('MyCookie1', 'SomeValue',15);">Attack</a></div>
    </td>
      </table>
      <br />
        <table border="1">
     <tr>
     <td><? Print "<b>Name:</b> ".$test['name'] . " ";  ?></td>
     <td><? Print "<b>Level:</b> ".$test['level'] . " ";  ?></td>
     <td><? Print "<b>Attack:</b> ".$test['attack'] . " ";  ?></td>
     <td><? Print "<b>Deffence:</b> ".$test['deffence'] . " ";  ?></td>
          <td>
    <div id="test"><a href="bgmob.php" onclick="setCookie('MyCookie2', 'SomeValue',15);">Attack</a></div>
    </td>
      </table>
      <br><br><br><br><br>
    <a href="Member.php">| Menu |</a>
    | Contact |
    | About |
    | News |
    <a href="logout.php">| Logout |</a>
    </center>
    </body>

  7. #7
    Join Date
    Nov 2010
    Posts
    1,083
    here's one way to do it. there's probably something much slicker but I haven't worked with cookies enough to see it. This answers your other question, though - SomeValue was just a place holder in case you wanted to pass some other information along to the cookie. We don't really do that here (because we need that information after the cookie has expired, to show the link it was hiding), but we still use that argument space to pass along the id of the link so we can hide it and also make an object which we will use later to display it.

    Code:
    <html>
    <head>
    <script type="text/javascript">
    var int;
    var cookieNames=[]
    function setCookie(pLabel, pVal, pSecs){
    cki={name:pLabel, val:pVal}
    cookieNames.push(cki)
    document.getElementById(pVal).style.visibility="hidden"
     var tExpDate=new Date();
     tExpDate.setTime( tExpDate.getTime()+(pSecs*1000) );
     document.cookie= pLabel + "=" +escape(pVal)+
       ( (pSecs==null) ? "" : ";expires="+ tExpDate.toGMTString() );
    int=setInterval(checkCookie,1000)
    }	
    
    function getCookie(c_name){
    var i,x,y,ARRcookies=document.cookie.split(";");
    for (i=0;i<ARRcookies.length;i++){
      x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
      y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
      x=x.replace(/^\s+|\s+$/g,"");
      if (x==c_name)
        {
        return unescape(y);
        }
      }
    }
    
    function checkCookie(){
    var vis=0;
    for (i=0;i<cookieNames.length;i++){
    var cname=getCookie(cookieNames[i].name);
      if (cname!=null && cname!="")  {
      document.getElementById(cookieNames[i].val).style.visibility="hidden"
        }
    else {
    vis++
    document.getElementById(cookieNames[i].val).style.visibility="visible"
    		}
    	}
    if (vis==cookieNames.length&&int!=undefined){
    clearInterval(int)
    cookieNames.length=0;	
    	}
    }	
    </script>
    </head>
    <body onload="checkCookie()">
    <a href="javascript: void(0)" id="test1" onclick="setCookie('MyCookie',this.id,15);">a link</a><br>
    <a href="javascript: void(0)" id="test2" onclick="setCookie('MyCookie2',this.id,15);">another link</a><br>
    <a href="javascript: void(0)" id="test3" onclick="setCookie('MyCookie3',this.id,15);">one more link</a><br>
    </body>
    </html>

  8. #8
    Join Date
    Feb 2012
    Posts
    38
    I have put in the code you gave me and once i click on the link it disapears but then less then a seconds before it even changed pages it re-appears lol

    Code:
    <head>
    <script language="javascript">
    var int;
    var cookieNames=[]
    function setCookie(pLabel, pVal, pSecs){
    cki={name:pLabel, val:pVal}
    cookieNames.push(cki)
    document.getElementById(pVal).style.visibility="hidden"
     var tExpDate=new Date();
     tExpDate.setTime( tExpDate.getTime()+(pSecs*1000) );
     document.cookie= pLabel + "=" +escape(pVal)+
       ( (pSecs==null) ? "" : ";expires="+ tExpDate.toGMTString() );
    int=setInterval(checkCookie,1000)
    }	
    
    function getCookie(c_name){
    var i,x,y,ARRcookies=document.cookie.split(";");
    for (i=0;i<ARRcookies.length;i++){
      x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
      y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
      x=x.replace(/^\s+|\s+$/g,"");
      if (x==c_name)
        {
        return unescape(y);
        }
      }
    }
    
    function checkCookie(){
    var vis=0;
    for (i=0;i<cookieNames.length;i++){
    var cname=getCookie(cookieNames[i].name);
      if (cname!=null && cname!="")  {
      document.getElementById(cookieNames[i].val).style.visibility="hidden"
        }
    else {
    vis++
    document.getElementById(cookieNames[i].val).style.visibility="visible"
    		}
    	}
    if (vis==cookieNames.length&&int!=undefined){
    clearInterval(int)
    cookieNames.length=0;	
    	}
    }	
    
    </script>
    
    </head>
    
    <?php 
     // Connects to your Database 
     mysql_connect("localhost", "root") or die(mysql_error()); 
     mysql_select_db("underworld") or die(mysql_error()); 
     
     $name = 'name';
     
     //checks cookies to make sure they are logged in 
     if(isset($_COOKIE['ID_my_site'])) 
     { 
     	$username = $_COOKIE['ID_my_site']; 
     	$pass = $_COOKIE['Key_my_site']; 
     	 	$check = mysql_query("SELECT * FROM users WHERE username = '$username'")or die(mysql_error()); 
     	while($info = mysql_fetch_array( $check )) 	 
     		{ 
     
     //if the cookie has the wrong password, they are taken to the login page 
     		if ($pass != $info['password']) 
     			{ 			header("Location: login.php"); 
     			} 
     
     //otherwise they are shown the admin area	 
     	else 
     			{ 
     			} 
     		} 
     		} 
     else 
     
     //if the cookie does not exist, they are taken to the login screen 
     {			 
     header("Location: login.php"); 
     } 
     
     $att = mysql_query("update * FROM users set exp = '110' WHERE username = '&username'");
     
      $data1 = mysql_query("SELECT * FROM mobs WHERE ID = '1'") 
     or die(mysql_error());
     $test = mysql_fetch_array( $data1 );
     
       $data1 = mysql_query("SELECT * FROM mobs WHERE ID = '2'") 
     or die(mysql_error());
     $test2 = mysql_fetch_array( $data1 );
     	 
     ?> 
     <body onload="checkCookie()">
      <center>
    <table border="1">
     	<tr>
     <td><? Print "<b>Name:</b> ".$test['name'] . " ";  ?></td>
     <td><? Print "<b>Level:</b> ".$test['level'] . " ";  ?></td>
     <td><? Print "<b>Attack:</b> ".$test['attack'] . " ";  ?></td>
     <td><? Print "<b>Deffence:</b> ".$test['deffence'] . " ";  ?></td>
          <td>
    <a href="bgmob.php" id="test1" onclick="setCookie('MyCookie',this.id,15);">Attack</a><br>
    </td>
    </table>
      <br>
      <table border="1">
     <tr>
     <td><? Print "<b>Name:</b> ".$test['name'] . " ";  ?></td>
     <td><? Print "<b>Level:</b> ".$test['level'] . " ";  ?></td>
     <td><? Print "<b>Attack:</b> ".$test['attack'] . " ";  ?></td>
     <td><? Print "<b>Deffence:</b> ".$test['deffence'] . " ";  ?></td>
          <td>
    <a href="bgmob.php" id="test2" onclick="setCookie('MyCookie2',this.id,15);">Attack</a>
    </td>
      </table>
      <br />
        <table border="1">
     <tr>
     <td><? Print "<b>Name:</b> ".$test['name'] . " ";  ?></td>
     <td><? Print "<b>Level:</b> ".$test['level'] . " ";  ?></td>
     <td><? Print "<b>Attack:</b> ".$test['attack'] . " ";  ?></td>
     <td><? Print "<b>Deffence:</b> ".$test['deffence'] . " ";  ?></td>
          <td>
    <a href="bgmob.php" id="test3" onclick="setCookie('MyCookie3',this.id,15);">Attack</a>
    </td>
      </table>
      <br><br><br><br><br>
    <a href="Member.php">| Menu </a>
    | Contact 
    | About 
    | News 
    <a href="logout.php">| Logout </a>
    </center>
    </body>

  9. #9
    Join Date
    Feb 2012
    Posts
    38
    I have 1 page that is working 100% fine then i made a second page with the coding i have But it dosent work and i cant figure it out i cant post my code b/c All together its over 15000+ Characters and max is only 10000 so if i Could Send my Code thru a email Or skype or somthing Please let me know Dont mean to go off forums to do this but the limit kinda restricts me b/c they need to see all the code to help lol

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles