473,473 Members | 2,098 Online
Bytes | Software Development & Data Engineering Community
Create 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 1640
markrawlingson
346 Recognized Expert Contributor
You could use window.onfocus = yourfunction;

Expand|Select|Wrap|Line Numbers
  1.  
  2. window.onfocus = yourajaxfunction;
  3.  
  4. function yourajaxfunction() {
  5. //do whatever
  6. }
  7.  
  8.  
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;
  5.  
  6. function myfunc() {
  7. alert('window has focus!');
  8. }
  9. </script>
  10. </head>
  11. </head>
  12.  
Aug 31 '07 #2
robertsherman
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
acoder
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
markrawlingson
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
robertsherman
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>
  17.  
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
markrawlingson
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>
  16.  
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. }
  5.  
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

4
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...
2
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...
8
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...
9
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...
3
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:...
5
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...
17
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...
2
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....
9
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
Oralloy
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,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
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...
1
isladogs
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...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
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 ...

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.