473,886 Members | 2,540 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

I need js code to run after returning to a page

3 New Member
I'm new at js, so go easy on me. I need some js code to run AFTER returning to a page from an external page. If I use onfocus, and I link away using target=_blank to spawn a new window, it works perfectly -- that is, the original onfocus fires when I return. But if I use target=_self, or NO target, onfocus does not regognize my return. The Yahoo Finance page does this perfectly. If I link away, it stops updating, and when I return, it quickly updates (AJAX). And it doesn't matter if the link opened a new window or not. I hope some of you js or ajax expertsan help. (ps, it needs to work on IE, pref. 6+)
Robert Sherman
Aug 31 '07 #1
6 1660
346 Recognized Expert Contributor
You could use window.onfocus = yourfunction;

Expand|Select|Wrap|Line Numbers
  2. window.onfocus = yourajaxfunction;
  4. function yourajaxfunction() {
  5. //do whatever
  6. }
Place between <head> </head> tags of the page which you wish to run your function when that page has focus.

I tested this simple page/function - If i move away from the page and come back it works.

Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head>
  3. <script>
  4. window.onfocus = myfunc;
  6. function myfunc() {
  7. alert('window has focus!');
  8. }
  9. </script>
  10. </head>
  11. </head>
Aug 31 '07 #2
3 New Member
Thanks. We're 99% there, but this code still requires that I click somewhere on the page after returning to it. I need it to start running the code without a user click (again, like what I see at Yahoo Finance). Should I use some other event (onmouseover?? onblur followed by onfocus?? or ???) Or is it just a builtin ajax feature that code suspends when you link or tab to another page and then automatically restarts when you return?

Still puzzled,
Robert Sherman
Aug 31 '07 #3
16,027 Recognized Expert Moderator MVP
When you return, you are giving the window focus, aren't you?

Show your code.
Sep 1 '07 #4
346 Recognized Expert Contributor
If the page didn't have focus, you'd have to click on it (to give it focus), otherwise - the code should run fine.
Sep 1 '07 #5
3 New Member
OK, here's my whole test page...
Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head>
  3. <script>
  4. window.focus()
  5. window.onfocus = myfunc;
  6. function myfunc() {
  7. alert('window has focus!')
  8. }
  9. </script>
  10. </head>
  11. </head>
  12. <body bgcolor="lightgrey">
  13. focus test<P>
  14. <a href="http://www.yahoo.com" target=_self>Yahoo</a> 
  15. </body>
  16. </html>
First, it works fine, and the alert displays. Then I link to Yahoo. When I return, I need to click to activate the alert. I did put in a window.focus() line to try to force focus.

Yahoo Finance somehow does this correctly. Sit on tthe page, and (at least during market hours) the page continually autoupdates (ajax), link away, and it seems to stop updating. Return, and without a click it starts updating again. How do I mimic this???
Sep 1 '07 #6
346 Recognized Expert Contributor
Ah, yes.. i see what you mean. That's really wierd actually, never noticed that before with the onfocus event. After going to Yahoo, and then hitting the back button the browser.. the window still has focus, but the event doesn't fire. I even tried blurring the page and then giving it focus again and that didn't work either.

I played around with it, and come to think of it for your situation window.onload would probably be better since it fires any time the page gets displayed on your screen.

Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head>
  3. <script>
  4. window.onload = myfunc;
  5. function myfunc() {
  6.      alert('window has focus!')
  7. }
  8. </script>
  9. </head>
  10. </head>
  11. <body bgcolor="lightgrey">
  12. focus test<P>
  13. <a href="http://www.yahoo.com" target=_self>Yahoo</a> 
  14. </body>
  15. </html>
This will only occur when someone first visits the page, or goes back to it in the browser. If you want to continually update the page (say every x seconds/minutes) like you described yahoo does.. you can put a setTimeout on it.

Replace the test function with have above with this...

Expand|Select|Wrap|Line Numbers
  1. function myfunc() {
  2.      alert('window has focus!')
  3.      window.setTimeout("myfunc()",1000)
  4. }
That will cause the page to pop up an alert box every second.

The code will automatically stop execution when you browse away from the page because you're simply.. not on that page anymore. When you go back to the page (you're firing the onload event again) it starts over again.

Try it.. it (very annoyingly) worked for me.
Sep 1 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

by: Eric West | last post by:
Greetings- I have a web application that has a form that triggers a server process that I would like to provide a "Searching..." page for. My strategy was to send the browser the "top half" of a web page, including an animated "Searching..." gif. I write the contents of this page to the HttpServletResponse output, and then flush the buffer (which I had thought would cause the bytes written thus-far to make their way to the client). I...
by: http://ray1.net/ | last post by:
Dear Readers. Using Javascript in ASP I need a way of returning through ADO (connection or recordset) the message returned via SQL when I run a query that UPDATES, DELETES or an SP that returns a message. So I basically when the below code gets executed I need a way of returning the message if it's there and if not nothing. a)
by: George Hester | last post by:
In a page I have when the user left-clicks the page a Input box for a form gets the focus. But if the user right-clicks the page the Input box is not getting the focus. I'd like the Input box to get the focus no matter where on the page the user clicks be it right-click or left-click. Right now there is no context menu when the user right-clicks. Do you think that's the problem? Any ideas how to get this right-click left-click Input box...
by: Chris | last post by:
I pop up a Modal form on a web page that allows the user to update the selections of a dropdownlist via adding a new item to the db (Sql Server).The only thing is this list is on the page beneath it. So when the user updates the db with a new selection, and then closes the form, the page below must be refreshed to see the new option. Any ideas on adding the new option to the control below the modal form? thanx.
by: Shapper | last post by:
Hello, I am working on an ASP.NET/VB web site. I have several links including menu links. Considerer a want to load a page named page.aspx. I can do it using javascript. Or using this code: Sub loadPage(sender As Object, e As System.EventArgs, pageURL as String)
by: Learner | last post by:
Hello, Here is the code snippet I got strucked at. I am unable to convert the below line of code to its equavalent vb.net code. could some one please help me with this? static public List<RoleData> GetRoles() { return GetRoles(null, false); }
by: freemann | last post by:
Can anyone provide example code showing how to send form results to a results page, email and a comma delimited file? Notice that I need it going to all three locations. Details: I have forms created and working. The first form the user fills out and submits. The form properties are set to Send to other: "Custom ISAPI, NSAPI, CGI, OR ASP SCRIPTING. The method is "POST" and the action is "secondpage.asp". I had to go this route because...
by: CM | last post by:
Hi, Could anyone please help me? I am completing my Master's Degree and need to reproduce a Webpage in Word. Aspects of the page are lost and some of the text goes. I would really appreciate it. The link to the document is http://www.surveymonkey.com/s.asp?u=689952259313 I have spent 15 hours trying to sort this but to no avail.
by: MrHelpMe | last post by:
Hello again experts, I have successfully pulled data from an LDAP server and now what I want to do is drop the data into a database table. The following is my code that will insert the data but that has problems. FullName=Request.Form("Name") Email=Request.Form("Email") GivenName=Request.Form("GivenName")
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.