Solved

reload/refresh does not untiliz named anchor

Posted on 2009-05-12
9
770 Views
Last Modified: 2013-12-20
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
Comment
Question by:steveweems
  • 3
  • 3
  • 3
9 Comments
 
LVL 4

Expert Comment

by:galadore
ID: 24368576
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
 

Author Comment

by:steveweems
ID: 24368764
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
 
LVL 4

Assisted Solution

by:galadore
galadore earned 50 total points
ID: 24369032
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
 

Author Comment

by:steveweems
ID: 24369713
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 4

Expert Comment

by:galadore
ID: 24370138
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
 
LVL 27

Expert Comment

by:azadisaryev
ID: 24371502
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
 
LVL 27

Expert Comment

by:azadisaryev
ID: 24371518
GACK! spoke too soon! further testing revealed that IE still ignores even non-empty named anchor...
back to testing...

Azadi
0
 
LVL 27

Accepted Solution

by:
azadisaryev earned 75 total points
ID: 24371550
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
 

Author Closing Comment

by:steveweems
ID: 31580682
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Preface In the first article: A Better Website Login System (http://www.experts-exchange.com/A_2902.html) I introduced the EE Collaborative Login System and its intended purpose. In this article I will discuss some of the design consideratio…
If you don't have the right permissions set for your WordPress location in IIS, you won't be able to perform automatic updates. Here's how to fix the problem.
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…

760 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

20 Experts available now in Live!

Get 1:1 Help Now