gib65
01-20-2012, 06:40 PM
Hello,
I'm trying to write some javascript that will detect if the page has been loaded because of the refresh button being pressed. I've searched google on how to do this, and several websites recommend something similar to the code I'm implementing below:
<script type="text/javascript" language="javascript">
// in head
function checkRefresh() {
alert("BEFORE: value = " + document.getElementById("visited").getAttribute("value"));
if (document.getElementById("visited").getAttribute("value") == null ||
document.getElementById("visited").getAttribute("value") == "")
{
document.getElementById("visited").setAttribute("value", "refreshed")
}
alert("AFTER: value = " + document.getElementById("visited").getAttribute("value"));
}
</script>
...
<body onload="Javascript:checkRefresh();">
...
<form id="hiddenform">
<input type="hidden" id="visited" value="" />
</form>
</body>
My variation differs from most of the examples on the internet in a few ways (which may or may not affect its functionality):
1) Most examples access the elements by directly using their names (as in: document.hiddenForm.visited.value). I'm using document.getElementById(...).getAttribute(...) just because that seems to be the safest way to ensure you are in fact getting the elements you want, and setAttribute(...) to ensure you're setting the attribute in the proper way. This entails that I need to set the ID in the form and input elements rather than the name.
2) I'm accessing the input tag directly (rather than going through the form) because I really don't see how this would make a difference.
3) I'm doing all this within asp:content tags which, from what I understand, can affect the behavior of the elements within it.
I'm not sure if this works out for other programmers, but for me it doesn't seem to be working. My alert messages in the checkRefresh function tell me that the value of the input element does indeed change as expected, but it seems to get wiped out and reinitialized to the original value of "" when the page is refreshed.
Am I doing something wrong?
Thanks for any help.
I'm trying to write some javascript that will detect if the page has been loaded because of the refresh button being pressed. I've searched google on how to do this, and several websites recommend something similar to the code I'm implementing below:
<script type="text/javascript" language="javascript">
// in head
function checkRefresh() {
alert("BEFORE: value = " + document.getElementById("visited").getAttribute("value"));
if (document.getElementById("visited").getAttribute("value") == null ||
document.getElementById("visited").getAttribute("value") == "")
{
document.getElementById("visited").setAttribute("value", "refreshed")
}
alert("AFTER: value = " + document.getElementById("visited").getAttribute("value"));
}
</script>
...
<body onload="Javascript:checkRefresh();">
...
<form id="hiddenform">
<input type="hidden" id="visited" value="" />
</form>
</body>
My variation differs from most of the examples on the internet in a few ways (which may or may not affect its functionality):
1) Most examples access the elements by directly using their names (as in: document.hiddenForm.visited.value). I'm using document.getElementById(...).getAttribute(...) just because that seems to be the safest way to ensure you are in fact getting the elements you want, and setAttribute(...) to ensure you're setting the attribute in the proper way. This entails that I need to set the ID in the form and input elements rather than the name.
2) I'm accessing the input tag directly (rather than going through the form) because I really don't see how this would make a difference.
3) I'm doing all this within asp:content tags which, from what I understand, can affect the behavior of the elements within it.
I'm not sure if this works out for other programmers, but for me it doesn't seem to be working. My alert messages in the checkRefresh function tell me that the value of the input element does indeed change as expected, but it seems to get wiped out and reinitialized to the original value of "" when the page is refreshed.
Am I doing something wrong?
Thanks for any help.