Solved

Hide div and show div after 15 minutes automatically without page refresh using javascript

Posted on 2009-03-29
9
866 Views
Last Modified: 2012-05-06
I have a Hide/show link inside <div> tag. Url will hide the <div> tag.
<div> tag will be visible automatically after 15 minutes.

Would you please provide the javascript code that will do this. Below code will hide the <div>

Problem: I dont know how to show <div> after 15 minutes automatically without page refeesh javascript
function SetCookie() {

	if (document.cookie.length > 0)

	{

		myCookie = document.cookie.indexOf("MsgCookie");

		if (myCookie != -1) {

			document.getElementById('Msg').style.visibility = 'hidden';		

		}

	}

}
 

function DismissMsg()

{

    document.cookie = "MsgCookie";

	document.getElementById('Msg').style.visibility = 'hidden';	
 

}

Open in new window

0
Comment
Question by:Saroj13
  • 4
  • 4
9 Comments
 
LVL 13

Expert Comment

by:numberkruncher
ID: 24014816
You can automatically run your "DismissMsg" function after a set timeout:
setTimeout(DismissMsg, 1000*60*15);

Open in new window

0
 
LVL 13

Accepted Solution

by:
numberkruncher earned 500 total points
ID: 24014823
Just to clarify, your source would be changed to the following:
function SetCookie() {

        if (document.cookie.length > 0)

        {

                myCookie = document.cookie.indexOf("MsgCookie");

                if (myCookie != -1) {

                        document.getElementById('Msg').style.visibility = 'hidden';             

                }

        }

}

 

function DismissMsg()

{

    document.cookie = "MsgCookie";

        document.getElementById('Msg').style.visibility = 'hidden';     

 

}
 

// This will call "DismissMsg" once after 15 mins.

setTimeout(DismissMsg, 1000*60*15);

Open in new window

0
 
LVL 9

Expert Comment

by:djon2003
ID: 24015044
Just to do a little correction.. The code above will hide the box after minutes.. and in the question is showing.. so :
function ShowHideMsg(showing) // Old DismissMsg

{

   if (showing == "undefined") showing = true;
 

    document.cookie = "MsgCookie";

        document.getElementById('Msg').style.visibility = showing ? 'visible' : 'hidden';     

 

}

 

// This will call "ShowHideMsg" once after 15 mins.

setTimeout(ShowHideMsg, 1000*60*15);
 

//Code to hide : ShowHideMsg(false);

//Code to show : ShowHideMsg(true);

Open in new window

0
 
LVL 13

Expert Comment

by:numberkruncher
ID: 24015166
Sorry, my mistake, I misread the question.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:Saroj13
ID: 24015471
Thanks for the responses.
I have put the complete code. Please check if its correct or not.Currently, Its not working.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >

<head>

    <title>Untitled Page</title>

    <script>

    window.onload=function()

{

    SetCookie();

    setTimeout(ShowHideMsg, 1000*60*1);
 

}
 

function SetCookie() {

	if (document.cookie.length > 0)

	{

		myCookie = document.cookie.indexOf("MsgCookie");

		if (myCookie != -1) {

			document.getElementById('Msg').style.visibility = 'hidden';		

		}

	}

}
 
 
 

function ShowHideMsg(showing) // Old DismissMsg

{

   if (showing == "undefined") showing = true;

 

    document.cookie = "MsgCookie";

        document.getElementById('Msg').style.visibility = showing ? 'visible' : 'hidden';     

 

}

    

    

    </script>

</head>

<body>

<div id="Msg" >

	<a href="#"  style="color:#688FCF; font-size:1.1em" onClick="ShowHideMsg(true);" >Show/Hide Div</a>

</div>

</body>

</html>

Open in new window

0
 

Author Comment

by:Saroj13
ID: 24015533
Please make the changes in the below code as soon as possible.

1. Show/Hide Div link will hide the link first.

2. After 15 minutes, div tag will become visible again without page refresh

Thanks
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >

<head>

    <title>Untitled Page</title>

    <script>

    window.onload=function()

{

    SetCookie();

    setTimeout(ShowHideMsg, 1000*60*15);

 

}

 

function SetCookie() {

	if (document.cookie.length > 0)

	{

		myCookie = document.cookie.indexOf("MsgCookie");

		if (myCookie != -1) {

			document.getElementById('Msg').style.visibility = 'hidden';		

		}

	}

}

 

 

 

function ShowHideMsg(showing) // Old DismissMsg

{

   if (showing == "undefined") showing = true;

 

    document.cookie = "MsgCookie";

        document.getElementById('Msg').style.visibility = showing ? 'visible' : 'hidden';     

 

}

    

    

    </script>

</head>

<body>

<div id="Msg" >

	<a href="#"  style="color:#688FCF; font-size:1.1em" onClick="ShowHideMsg(true);" >Show/Hide Div</a>

</div>

</body>

</html>

Open in new window

0
 

Author Comment

by:Saroj13
ID: 24015718
Hello Guys,

Please see the code if its fine or not...
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >

<head>

    <title>Untitled Page</title>

    <script>

    window.onload=function()

{

    SetCookie();

    setTimeout(ShowHideMsg, 1000*60*15);

 

}

 

function SetCookie() {

	if (document.cookie.length > 0)

	{

		myCookie = document.cookie.indexOf("MsgCookie");

		if (myCookie != -1) {

			document.getElementById('Msg').style.visibility = 'hidden';		

		}

	}

}

 

 

 

function ShowHideMsg(showing) // Old DismissMsg

{

   if (showing == "undefined") showing = true;

 

    document.cookie = "MsgCookie";

        document.getElementById('Msg').style.visibility = showing ? 'visible' : 'hidden';     

 

}

    

    

    </script>

</head>

<body>

<div id="Msg" >

	<a href="#"  style="color:#688FCF; font-size:1.1em" onClick="ShowHideMsg(true);" >Show/Hide Div</a>

</div>

</body>

</html>

 

Open in New Window Select All 

Open in new window

0
 
LVL 13

Expert Comment

by:numberkruncher
ID: 24018897
There is a syntax error in your above code, your missing a semi-colon after the onload event handler.

Also, you have some text "Open in New Window Select All" which appears below the closing html tag. You cannot have text below the closing html tag.

Just to clarify, "setTimeout" will execute a block of code once after the specified period. If you want code to run every 15 minutes, then you would need to use "setInterval" instead.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >

<head>

    <title>Untitled Page</title>

    <script>

    window.onload = function() {

       SetCookie();

       setTimeout(ShowHideMsg, 1000*60*15);

    };
 

    function SetCookie() {

       if (document.cookie.length > 0)

       {

          myCookie = document.cookie.indexOf("MsgCookie");

          if (myCookie != -1) {

             document.getElementById('Msg').style.visibility = 'hidden';             

          }

       }

    }
 

    function ShowHideMsg(showing) { // Old DismissMsg

       if (showing == "undefined") showing = true;
 

       document.cookie = "MsgCookie";

       document.getElementById('Msg').style.visibility = showing ? 'visible' : 'hidden';

    }

    </script>

</head>

<body>

<div id="Msg" >

        <a href="#"  style="color:#688FCF; font-size:1.1em" onClick="ShowHideMsg(true);" >Show/Hide Div</a>

</div>

</body>

</html>

Open in new window

0
 

Author Comment

by:Saroj13
ID: 24019606
If you want code to run every 15 minutes, I have used 15 minutes. i want to show <div> tag every 15 min..
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Where do I learn ES6? 3 49
CSS Position not returning 2 46
Auto refresh part of an MVC5 View 7 57
Interview question Javascript, database 12 29
When you need to keep track of a simple list of numbers or strings, the Array object is your most direct tool.  As we saw in my earlier EE Article (http://www.experts-exchange.com/A_3488.html), typical array handling might look like this: (CODE) B…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

912 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now