-1

I'm sorry about this newbie question, but I'm on early beggining of jquery.

I'm trying to grab the H1 title from an external page and append to a DIV on my website. I'm having trouble to get the content, and I need to check for changes on H1 every 10 seconds. What am I doing wrong?

<script type="text/javascript">
    jQuery.ajax({
            setInterval(function() {
            url: "livetime.chronon.com.br/p1.html",
            success: function(result) {
                h1Content = $(body).find(h1).html();
            },10000);
              
                $('#h1Title').append(h1Content);
            },
        });
    </script>
<div id="h1Title"></div>
SoMeGoD
  • 135
  • 5
  • 13

1 Answers1

0

Pretty sure it's impossible to define a setInterval function inside an AJAX call. Try this:

<script type="text/javascript">
    setInterval(function() {
        jQuery.ajax({
            url: "livetime.chronon.com.br/p1.html",
            success: function(result) {
                h1Content = $(body).find(h1).html();
                $('#h1Title').append(h1Content);
            }
        });
    }, 10000);
</script>

Regular AJAX calls are restricted to pages that are inside the current domain. If this is case you can use the jQuery.load to retrieve a page's HTML and insert its content on the matched elements. After that, you can navigate through that element to check the h1 tag. If the site you are trying to access is outside the current domain, you'll have to use something called JSONP. You can learn more about this technique with this answer.

Community
  • 1
  • 1
André Silva
  • 1,110
  • 8
  • 24
  • Hey Man, thanks for the response. The H1 it's not coming to my site, I think there's something wrong with my code and it's not loading the H1 first. My div is empty – SoMeGoD Nov 08 '15 at 11:48
  • I'm just trying to grab the H1 from another page, show on my div and make update automatically every 10 seconds without the page refresh. I'm really on the earlier stages of coding, so I don't know to answer your question. I gathered what I could from the net and tried to put together everything. – SoMeGoD Nov 08 '15 at 12:01
  • I've got your point. Ajax calls are restricted to pages inside the same domain. This settles everything. Thank you so much for the answer. I'll take a look at the link. – SoMeGoD Nov 08 '15 at 12:28