katdude
asked on
How can I get my page to refresh with User Options???
Here is what I am trying to do:
The user makes a selection from two radio buttons.
(Yes,I want this page to refresh Automatically;
or No I don't want the page to refresh
(No is set as default)
If the Yes is selected, then
A Refresh button appears along with a text box.
The page will refresh at a set interval set in the text box by the user.
The user clicks the refresh button and the page is set to refresh at the rate set by the user.
If the No is selected then
do nothing
I can make the page automatically refresh with code, but I want the user to be able to set the intervals.
What do I need to do for this to work???
The user makes a selection from two radio buttons.
(Yes,I want this page to refresh Automatically;
or No I don't want the page to refresh
(No is set as default)
If the Yes is selected, then
A Refresh button appears along with a text box.
The page will refresh at a set interval set in the text box by the user.
The user clicks the refresh button and the page is set to refresh at the rate set by the user.
If the No is selected then
do nothing
I can make the page automatically refresh with code, but I want the user to be able to set the intervals.
What do I need to do for this to work???
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
You could also store the value in a cookie.
The following sample shows all three methods in action:
<html>
<head>
<script>
function QueryString(key)
{
var value = null;
for (var i=0;i<QueryString.keys.len gth;i++)
{
if (QueryString.keys[i]==key)
{
value = QueryString.values[i];
break;
}
}
return value;
}
QueryString.keys = new Array();
QueryString.values = new Array();
function QueryString_Parse()
{
var query = window.location.search.sub string(1);
var pairs = query.split("&");
for (var i=0;i<pairs.length;i++)
{
var pos = pairs[i].indexOf('=');
if (pos >= 0)
{
var argname = pairs[i].substring(0,pos);
var value = pairs[i].substring(pos+1);
QueryString.keys[QueryStri ng.keys.le ngth] = argname;
QueryString.values[QuerySt ring.value s.length] = value;
}
}
}
QueryString_Parse();
function setCookie(name, value) {
var expires = new Date()
expires.setTime(expires.ge tTime() + 1000*60*60)
document.cookie =
name + "=" + escape(value) +
((expires == null) ? "" : ("; expires=" + expires.toGMTString()))
}
function getCookie(Name) {
var search = Name + "="
if (document.cookie.length > 0) {
offset = document.cookie.indexOf(se arch)
if (offset != -1) {
offset += search.length
end = document.cookie.indexOf("; ", offset)
if (end == -1)
end = document.cookie.length
return unescape(document.cookie.s ubstring(o ffset, end))
}
}
return null
}
function getObject(id) {
if (document.all) {
return document.all(id);
} else if (document.getElementById) {
return document.getElementById(id );
}
return null;
}
var defaultInterval = 10000;
var frameInterval = defaultInterval;
var interval = defaultInterval;
var myInterval = null;
var refreshFunction = QueryString("refreshFuncti on");
function refresh() {
if (refreshFunction!="frame") {
if (refreshFunction=="cookie" && getObject("myForm").refres hRate.valu e==String( interval)) {
getObject("myForm").refres hRate.valu e = "";
}
getObject("myForm").submit ();
}
}
function setRefreshInterval() {
if (myInterval != null) {
window.clearInterval(myInt erval);
}
myInterval = window.setInterval("refres h();",inte rval);
getObject("myForm").refres hRate.valu e = String(interval);
}
function getInterval() {
if (refreshFunction=="cookie" ) {
if (getCookie("interval") != null) {
interval = parseInt(getCookie("interv al"));
}
setCookie(String(interval) );
}
if (refreshFunction=="query") {
if (QueryString("refreshRate" ) != null) {
interval = parseInt(QueryString("refr eshRate")) ;
}
}
}
function saveInterval(theForm) {
if (myInterval != null) {
window.clearInterval(myInt erval);
}
refreshFunction = theForm.refreshFunction.va lue;
var interval2 = parseInt(theForm.refreshRa te.value);
if (isNaN(interval2)) {
alert("Enter a number");
theForm.refreshRate.value = String(interval);
} else {
if (interval2 < 3000) {
alert("Enter a number >= 3000");
theForm.refreshRate.value = String(interval);
} else {
if (refreshFunction=="cookie" ) {
setCookie("interval",theFo rm.refresh Rate.value );
theForm.refreshRate.value= "";
}
theForm.submit();
}
}
}
function loadPage() {
getInterval();
setRefreshInterval();
var refreshFunction = QueryString("refreshFuncti on");
if (refreshFunction != null) {
var objSelector = getObject("myForm").refres hFunction;
for (var i=0;i<objSelector.options. length;i++ ) {
if (objSelector.options[i].va lue==refre shFunction ) {
objSelector.options[i].sel ected = true;
}
}
if (objSelector.value == "cookie") {
getObject("myForm").refres hRate.valu e = getCookie("interval");
}
}
var refreshRate = QueryString("refreshRate") ;
if (refreshRate != null && refreshRate != "") {
getObject("myForm").refres hRate.valu e = refreshRate;
} else {
if (refreshFunction="cookie") {
getObject("myForm").refres hRate.valu e = getCookie("interval")
}
}
}
</script>
</head>
<body>
<form id="myForm" name="myForm" action="temp.htm" style="visibility:visible; ">
<input type="hidden" name="frame" value="false"> Refresh Using:
<select name="refreshFunction">
<option value="cookie">Cookie</opt ion>
<option value="query" selected>Query String</option>
<option value="frame">Frame</optio n>
</select>
<br>
Refresh Rate: <input type="text" name="refreshRate" value="">
<br>
<input type="button" value="Set Interval" onclick="saveInterval(this .form);">
</form>
<script>
loadPage();
if (refreshFunction=="frame") {
document.write("<iframe width='300' height='150' src='" + getObject("myForm").action + "?frame=true&refreshFuncti on=query&r efreshRate =" + QueryString("refreshRate") + "'>");
} else {
var tmpFunction = QueryString("refreshFuncti on");
if (tmpFunction == null) {tmpFunction = "query"}
var tmpRate = QueryString("refreshRate") ;
if (tmpFunction=="cookie") { tmpRate=getCookie("interva l"); } else {
if (tmpRate == null || tmpRate=="") {tmpRate = "10000";}
}
document.write("Interval: " + tmpRate);
if (QueryString("frame") == "true") {
getObject("myForm").style. visibility = "hidden";
getObject("myForm").frame. value="tru e";
} else {
document.write(" refreshFunction: " + tmpFunction);
}
}
</script>
</body>
</html>
The following sample shows all three methods in action:
<html>
<head>
<script>
function QueryString(key)
{
var value = null;
for (var i=0;i<QueryString.keys.len
{
if (QueryString.keys[i]==key)
{
value = QueryString.values[i];
break;
}
}
return value;
}
QueryString.keys = new Array();
QueryString.values = new Array();
function QueryString_Parse()
{
var query = window.location.search.sub
var pairs = query.split("&");
for (var i=0;i<pairs.length;i++)
{
var pos = pairs[i].indexOf('=');
if (pos >= 0)
{
var argname = pairs[i].substring(0,pos);
var value = pairs[i].substring(pos+1);
QueryString.keys[QueryStri
QueryString.values[QuerySt
}
}
}
QueryString_Parse();
function setCookie(name, value) {
var expires = new Date()
expires.setTime(expires.ge
document.cookie =
name + "=" + escape(value) +
((expires == null) ? "" : ("; expires=" + expires.toGMTString()))
}
function getCookie(Name) {
var search = Name + "="
if (document.cookie.length > 0) {
offset = document.cookie.indexOf(se
if (offset != -1) {
offset += search.length
end = document.cookie.indexOf(";
if (end == -1)
end = document.cookie.length
return unescape(document.cookie.s
}
}
return null
}
function getObject(id) {
if (document.all) {
return document.all(id);
} else if (document.getElementById) {
return document.getElementById(id
}
return null;
}
var defaultInterval = 10000;
var frameInterval = defaultInterval;
var interval = defaultInterval;
var myInterval = null;
var refreshFunction = QueryString("refreshFuncti
function refresh() {
if (refreshFunction!="frame")
if (refreshFunction=="cookie"
getObject("myForm").refres
}
getObject("myForm").submit
}
}
function setRefreshInterval() {
if (myInterval != null) {
window.clearInterval(myInt
}
myInterval = window.setInterval("refres
getObject("myForm").refres
}
function getInterval() {
if (refreshFunction=="cookie"
if (getCookie("interval") != null) {
interval = parseInt(getCookie("interv
}
setCookie(String(interval)
}
if (refreshFunction=="query")
if (QueryString("refreshRate"
interval = parseInt(QueryString("refr
}
}
}
function saveInterval(theForm) {
if (myInterval != null) {
window.clearInterval(myInt
}
refreshFunction = theForm.refreshFunction.va
var interval2 = parseInt(theForm.refreshRa
if (isNaN(interval2)) {
alert("Enter a number");
theForm.refreshRate.value = String(interval);
} else {
if (interval2 < 3000) {
alert("Enter a number >= 3000");
theForm.refreshRate.value = String(interval);
} else {
if (refreshFunction=="cookie"
setCookie("interval",theFo
theForm.refreshRate.value=
}
theForm.submit();
}
}
}
function loadPage() {
getInterval();
setRefreshInterval();
var refreshFunction = QueryString("refreshFuncti
if (refreshFunction != null) {
var objSelector = getObject("myForm").refres
for (var i=0;i<objSelector.options.
if (objSelector.options[i].va
objSelector.options[i].sel
}
}
if (objSelector.value == "cookie") {
getObject("myForm").refres
}
}
var refreshRate = QueryString("refreshRate")
if (refreshRate != null && refreshRate != "") {
getObject("myForm").refres
} else {
if (refreshFunction="cookie")
getObject("myForm").refres
}
}
}
</script>
</head>
<body>
<form id="myForm" name="myForm" action="temp.htm" style="visibility:visible;
<input type="hidden" name="frame" value="false"> Refresh Using:
<select name="refreshFunction">
<option value="cookie">Cookie</opt
<option value="query" selected>Query String</option>
<option value="frame">Frame</optio
</select>
<br>
Refresh Rate: <input type="text" name="refreshRate" value="">
<br>
<input type="button" value="Set Interval" onclick="saveInterval(this
</form>
<script>
loadPage();
if (refreshFunction=="frame")
document.write("<iframe width='300' height='150' src='" + getObject("myForm").action
} else {
var tmpFunction = QueryString("refreshFuncti
if (tmpFunction == null) {tmpFunction = "query"}
var tmpRate = QueryString("refreshRate")
if (tmpFunction=="cookie") { tmpRate=getCookie("interva
if (tmpRate == null || tmpRate=="") {tmpRate = "10000";}
}
document.write("Interval: " + tmpRate);
if (QueryString("frame") == "true") {
getObject("myForm").style.
getObject("myForm").frame.
} else {
document.write(" refreshFunction: " + tmpFunction);
}
}
</script>
</body>
</html>
Save the above code in a file: temp.htm
No comment has been added lately, so it's time to clean up this TA.
I will leave the following recommendation for this question in the Cleanup topic area:
Accept: fritz_the_blank {http:#8228418}
Please leave any comments here within the next seven days.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
jAy
EE Cleanup Volunteer
I will leave the following recommendation for this question in the Cleanup topic area:
Accept: fritz_the_blank {http:#8228418}
Please leave any comments here within the next seven days.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
jAy
EE Cleanup Volunteer
I think my answer is more useful & I would appreciate some points for it.
parse the documents location element for the users setting. If a refresh variable exists set a timout based on the variable and ensure the element containing the timer textbox is visble. If the variable exists then after a timout period navigate back to tyhe location ensuring you append the setting before you do.
."mooo?refresh=60
as sugested you could also use frames ensuring you dont need to parse the location for any variables.