• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 781
  • Last Modified:

reload/refresh does not untiliz named anchor

I am try to use javascript window.location.reload(true) with a setTimeout function to automatically reload a page every 3 miniutes.  The page is dynamically generated by Coldfusion as the database is updated.  

When in use the window.location.reload(true) function, it always loads the page at the top on the page and ingnores the target specified in the url. I need the page to reloaded page to go to the named target #REFRESH_TO.  Instead, it always loads to the top of the page.

Any suggestions?
url is winners.cfm#REFRESH_TO
 
<script language="JavaScript1.2">
<!--
setTimeout( "refresh()", 10*1000 );
function refresh()
{
     window.location.reload(true);
}
//-->
</script>

Open in new window

0
steveweems
Asked:
steveweems
  • 3
  • 3
  • 3
2 Solutions
 
galadoreCommented:
This is a script I use for a auto logout function:

<cfset sessionlength = 3>
    <script language="javascript">
     <!--
     var sessionLength=<cfoutput>#evaluate(sessionlength)#</cfoutput>;
     function Logout(sessionLength){
      location.href="YOUR URL HERE";
     }       
     function ExpireSession(sessionLength){
            setTimeout("Logout(sessionLength)", sessionLength * 60000);
     }
     ExpireSession(sessionLength)
     //-->
    </script>

Just add your URL and modify the timing to suit your needs.
0
 
steveweemsAuthor Commented:
Thanks for the response. I don't think that is exactly what I am looking for.  I need to know how to make the page auto reload to a specific location on the page.  When I use javascript's
window.location.reload() it alway goes to the top of page even though the current url has a named anchor in the url (ie winners.cfm#REFRESH_TO).  It seems to ignore the named anchor.
0
 
galadoreCommented:
Give my script a try - copy and paste the below srcipt.  A refresh should literally only refresh the page, while a location.href should head to the anchor point.  Just replace your script with this one and try it.  Let me know if it works.  You can always change it back.
<cfset sessionlength = "10">
    <script language="javascript">
     <!--
     var sessionLength=<cfoutput>#evaluate(sessionlength)#</cfoutput>;
     function Logout(sessionLength){
      location.href="winners.cfm#REFRESH_TO";
     }       
     function ExpireSession(sessionLength){
            setTimeout("Logout(sessionLength)", sessionLength * 1000);
     }
     ExpireSession(sessionLength)
     //-->
    </script> 

Open in new window

0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
steveweemsAuthor Commented:
It does go to the right place on the page, however it only refreshes once. I need to it to continually refresh every 3  minutes to display the results of changes in the database.

Is this code design to refresh one time or is something wrong on my end?

Thanks
0
 
galadoreCommented:
The code should work everytime it's loaded.  It's possible that in all your testing you're having an issue with page caching, but I'm not sure.  Best bet is let it run once, then view the source of the new page that it takes you to.  Is the code on that page some where?  if not, press refresh on your browser and check again.  If it's still not there, it might be the placement you chose for the code.  Move it around on the page template to be sure that  it's included when the new page is generated.
0
 
azadisaryevCommented:
let me guess: you are testing this in IE? ('cos it works fine in FF; but your js will not work in Safari at all)

for it to work in IE make sure that your named anchor element is not empty. i.e.:

this WILL NOT work:
<a name="REFRESH_TO"></a>

this WILL:
<a name="REFRESH_TO"><!-- --></a>

Safari does not, iirc, support window.location.reload(). For your js to work in Safari, you need to use window.location.href=window.location.href instead.

Azadi
0
 
azadisaryevCommented:
GACK! spoke too soon! further testing revealed that IE still ignores even non-empty named anchor...
back to testing...

Azadi
0
 
azadisaryevCommented:
OK, further testing reveals that using window.location = window.location.href instead of window.location.reload(true) makes it work properly in IE and FF (can't test on Safari here...)

BTW, if you want to reload your page every 3 minutes, you should be using setInterval() function, not setTimeout() - setTimeout executes the specified function only ONCE after specified timeout.

here's full javascript:

<script type="text/javascript">
setInterval( "refreshPage()", 10000 );
var refreshPage = function() {
  window.location = window.location.href;
};
</script>

(i have changed the name of the function to refreshPage because 'refresh' is a js function name, and who knows how it may confuse which browsers...)

Azadi
0
 
steveweemsAuthor Commented:
Thank you for your help.  Your solutions appear to work, however I am somewhat frustrated with the different resposes in different browsers and possible page caching.  I will continue to test variations on these solutions for more consistency, but I must move on for now.

Thanks again. I really appreciate your help.
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

  • 3
  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now