每5秒用jQuery / javascript读取XML [英] Read XML with jquery/javascript every 5 seconds

查看:122
本文介绍了每5秒用jQuery / javascript读取XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个html

index.html:

index.html:

<html>

<head>
    <title>Facebook</title>

    <script type="text/javascript" src="../xdata/js/jquery.js"></script>
</head>

<body>
    Tasks (<span id="tasks">0</span>)
    Messages (<span id="messages">0</span>)
    Notifications (<span id="notifications">0</span>)


    <script type="text/javascript">
    $(document).ready(function() {
    var pagetitle = document.title;
    document.title = pagetitle+' NEW NOTIFICATON';
    });
    </script>
</body>

</html>

和一个xml文件

页面。 xml:

page.xml:

<?xml version="1.0" ?>
<page tasks="1" messages="3" notifications="3"/>

我如何每5秒做一次 index.html 阅读 page.xml 并修改faceboook((1)Facebook)中的标题,并修改任务,消息,通知...

How do i make that every 5 seconds index.html to read page.xml and modify the title like in faceboook ("(1) Facebook") and also modify tasks, messages, notifications ...

我在阅读XML时遇到了问题。如果有人能帮助我?

I'm having problems at reading the xml. If anyone can help me?

PS - 我更喜欢jQuery ...但是JavaScript也可以工作

PS - I prefer jQuery ... but JavaScript works as well

推荐答案

以下是在jquery中完成的,尽管你的xml文件有一个小小的错误导致了解析问题。

The following was done in jquery, though you have a slight error with your xml file that lead to parsing issues.

假设你的xml文件是这样的:

Assuming your xml file is like this:

<?xml version="1.0"?>
<data>
    <page tasks="1" messages="3" notifications="3"/>
</data>

以下代码将相应地修改页面,当然使用jquery:

The following code will modify the page accordingly, using jquery of course:

$(document).ready(function() {
    function get_info() {
        $.ajax({
            type: "GET",
            url: "page.xml",
            dataType: "xml",
            cache: false,
            complete: function(doc) {
                var tasks = $(doc.responseText).find("page").attr("tasks");
                var msgs = $(doc.responseText).find("page").attr("messages");
                var notes = $(doc.responseText).find("page").attr("notifications");
                if (tasks != $('#tasks').text() ||
                    msgs != $('#messages').text() ||
                    notes != $('#notifications').text()) {
                    document.title = "Facebook" + ' NEW NOTIFICATON';
                }
                $('#tasks').text(tasks);
                $('#messages').text(msgs);
                $('#notifications').text(notes);
            }
        });

    }
    setInterval(function() {
        get_info();
    }, 5000);
});

我花了一些时间来开发这个功能,我知道它是有效的。

I spent some time developing this, and I know for a fact it works.

这篇关于每5秒用jQuery / javascript读取XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆