?
Solved

reload/refresh does not untiliz named anchor

Posted on 2009-05-12
9
Medium Priority
?
779 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 150 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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
 
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 225 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

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
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…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

771 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