/    Sign up×
Community /Pin to ProfileBookmark

Running JS in background

I am a relative amateur to client-side scripting, as I come from a server-side background. My task is this: I would like for my browser to hit an external url frequently, (or for 100 attempts, whichever comes first), to check if it has been updated. If it has been updated, the browser stops refreshing and performs some other task.

This is where I am so far:
while (i<=100)
{
window.location=”http://www.xyz.com“;
[checking to see if the page is updated..]
if (Updated == true) {
i=100; }
else {
i++; }
}

The only way I can think of to do something like this is to type this code into the address by via javascript:while (i<=100)…. but as soon as the window.location changes (ie the page refreshes) the code is gone forever, ie, the loop ends.

Is there a better way to do all this? I’m a javascript retard so i assume there MUST be a better way! Perhaps run this in a background thread? (is that even possible?) Thanks!

to post a comment
JavaScript

8 Comments(s)

Copy linkTweet thisAlerts:
@J2NYCMar 01.2010 — You can stick the page you're checking into an Iframe and run the Javascript from the parent.

Also, I would put a delay in that. Checking 100 times is going to take a very short period of time, so I would do a setTimeout loop if I were you - unless you expect the page to be updated in a couple of seconds.
Copy linkTweet thisAlerts:
@rnd_meMar 01.2010 — use greasemonkey to run a script while visiting the the site you need to monitor. Only then will you be able to check it out.

There is a small exception: if the site hosts an image of a known name, you can monitor the image from a different site; the image's width and height will change when the image loads...
Copy linkTweet thisAlerts:
@ecabooseauthorMar 01.2010 — J2NYC- I had thought about putting everything into an iframe, but wouldn't that violate the same-origin policy (ie reading from a frame whose domain is different from the parent?)

rnd me- good thinking, with the image size- but a more suitable solution for my needs would be to keep refreshing the page until the update is found, and then to stop updating (but perform another task on that page). Greasemonkey seems like it would be helpful, but I would prefer more of a home-brewed solution.

I guess there's no other way to have some code "persist" over a browser instance?
Copy linkTweet thisAlerts:
@rnd_meMar 01.2010 — you can use cookie to keep track of a number between refreshes, but that's not your mian problem, security is.

you cannot reach a site from another site, even in an iframe. images are a special leak, unpatched thus far.

so, you have to execute your checker from the site to be checked. you use greasemonkey to do that. but it won't run if the site's down, so i'm not sure if it would work at all in javascript...
Copy linkTweet thisAlerts:
@ecabooseauthorMar 01.2010 — All I'm trying to do is simply automate the process of constantly clicking "refresh" to see if the site has been updated. I would think there is a very simple way to go about this that I'm just unaware of (without having to use third-party tools like greasemonkey) - but is that really it? Is that really the only way? Thanks
Copy linkTweet thisAlerts:
@ecabooseauthorMar 02.2010 — rnd me, can you explain further how I would check to see if an image has loaded in a given iframe?

Example:

MyPage.html loads www.xyz.com/example.html into an iframe

MyPage.html will continuously reload the iframe until it finds that a specific image, xyz.com/image/hellokitty.jpg has been loaded into the iframe (confirmed by checking the width/height, according to what you said)

What is the necessary JS to check if the image has loaded in the iframe? I appreciate any help. Thanks!
Copy linkTweet thisAlerts:
@rnd_meMar 02.2010 — you can't check it if it's in the iframe. you have tro make an image tag on your site, with the src set to the url of the image on the remote site.

fore example, to monitor this forum, you could do make a page like this:

[CODE]
<html>
<img
src="http://www.internet.com/icom_includes/footers/img/icom_logo_qsfooter.png"
onload="if(this.width<50 && this.count< 100 ){var that=this; old=that.src; setTimeout(function(){ this.src=old;}, 1500 ); this.src='';this.count=this.count?this.count+1:1;}else{alert('image loaded, site is online');}" />

</html>[/CODE]

tested in firefox 3.6
Copy linkTweet thisAlerts:
@ecabooseauthorMar 02.2010 — Thank you for the reply and for that code sample. It appears that that snippet will only check to see if the server is running, but it will not check if a specific page is up/updated. But thank you anyway for your help - it appears what I am trying to do is not doable without an add-on.
×

Success!

Help @ecaboose spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...