开发者

Executing javascript after loading different site

I need to write a webapp, that will check if sit开发者_运维问答e contains an element (e.g div with id="iamhere"). This site can be any site, it's not in the same domain that my script is. I thought about doing something like:

  1. Go to my site, load javascript library.
  2. Load the other site.
  3. Execute my function.

This seems to be impossible - am I right (script execution stops after window.location.assign() and there's no way to "bind" a script tag to site's DOM)? Does anybody has an idea, how to make it work? I know I could use yql, but maybe there's better solution.


Check out JavaScript's setTimeout() function. You can periodically check the readiness of whatever with something like this:

function checkReadyState() 
{
  if (somethingIsReady)  {
    doStuffWhenReady();
  }
  else {
    setTimeout(function() { checkReadyState(); }, 200);   //Waits 200 milliseconds and calls checkReadyState() again.
  }
}


You will need to fetch the site with a scripting language to act as a proxy. I'll use PHP in this example:

<?php

$file = file_get_contents('http://www.google.com');

echo $file;

?>

When necessary to populate the div - you can do so with your javascript. If you're using jQuery it would look something like this:

<script>
$(function(){
    $('#amihere').load('myphpproxyfile.php');
});
</script>


I'd use YQL.

Replace the last 2 query string parameters and check the json result.

http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20html%20where%20url=@url%20and%20xpath=@xpathformat=json&callback=cvfunc&url=<your-url>&xpath=<your-xpath>

Example:

http://query.yahooapis.com/v1/public/yql?format=json&callback=cbfunc&url=http://www.stackoverflow.com&xpath=//div[@id=%27header%27]&q=select%20*%20from%20html%20where%20url=@url%20and%20xpath=@xpath
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜