0

Still playing with Chrome Extension and have a little problem when I want to get some information from an XML file. Here is how the XML look :

<?xml version='1.0' encoding='utf8'?>
<objects>
    <url>
        <domain>...</domain>
        <nb_clics>...</nb_clics>
        <alias>...</alias>
        <title>...</title>
        <source>...</source>
        <description />
    </url>
</objects>

I've tried this but, no result :

<html>
    <head>
        <style>
            body {
                width:160px;
            }
            p {
                font-weight:bold;
                margin-bottom:0;
            }
        </style>
    </head>
    <script type="text/javascript">
        function DOMImplementation(sUrl, fCallback) {
            var dom;
            if(window.ActiveXObject) {
                dom = new ActiveXObject("Microsoft.XMLDOM");
                dom.onreadystatechange = function() {
                    if(dom.readyState == 4) {
                        fCallback(dom);
                    }
                };
            }
            else if(document.implementation && document.implementation.createDocument) {
                dom = document.implementation.createDocument("", "", null);
                dom.onload = function() { 
                    fCallback(dom); 
                }
            }   
            else {
                alert("ERROR");
                return;
            } 
            dom.load(sUrl);
        }

        function shrimit() {
            var nicknick = localStorage["nick_name"];
            var apiapi = localStorage["api_key"];
            var yourc = document.getElementById("your");

            chrome.tabs.getSelected(null,function(tab) {
                var tablink = tab.url;
                if(!nicknick || !apiapi){
                    yourc.setAttribute("value","Set the options");
                } else {
                    DOMImplementation("post.xml", getData);
                    function getData(oData) {
                        var tablink2 = oData.getElementsByTagName("alias")[0].firstChild.data;
                        yourc.setAttribute("value",tablink2);
                    }
                }
            });
        }
    </script>
    <body onload="shrimit()">
        <input id="your" name="your" type="text" value="" />
    </body>
</html>

Can you help me ?

5
  • 1
    What does "no result" mean exactly? Are you getting any error messages? Commented Dec 30, 2009 at 22:07
  • 2
    If it's a chrome extension why would you use ActiveX? That's an IE technology. I guess you probably just copied and pasted some code and have no idea just what you are doing. Commented Dec 30, 2009 at 22:23
  • 1
    +1 to apphacker for the cold hard truth Commented Dec 30, 2009 at 22:31
  • 1
    @Mike Robinson: Yeah it's the cold hard truth, that's why the OP is asking for help! That's kind of the point of SO isn't it? Commented Dec 30, 2009 at 22:33
  • I'm quite a noob in JS, just nw the base, I've tried this because nothing else were working Commented Dec 30, 2009 at 22:55

1 Answer 1

3

Use XMLHttpRequest to load an XML file. This is standardised and reliable across browsers. document.implementation.createDocument ... load is not.

(You may get an HTMLDocument instead of a plain XML Document. Normally sniff for DOMParser before falling back to document.implementation.createDocument. Also createDocument('', '', null); is invalid: you can't have an empty-string root element name. Use createDocument(null, 'name', null). Also, finally, best not use the name DOMImplementation because that's the name of a built-in interface/class present in Chrome and some others.)

Finally, using a function statement inside an else clause is invalid in JavaScript. Browsers tend to allow it but with variable results. Either declare the function at the top level, or use an inline function expression.

Sign up to request clarification or add additional context in comments.

2 Comments

It's for a Chrome Extension, he doesn't have to fall back on anything. Just use what's available on Chrome.
Yep, it works with this : var xmlhttp = new XMLHttpRequest(); xmlhttp.open("GET","post.xml",false); xmlhttp.send(""); xmlDoc = xmlhttp.responseXML; var tablink2 = xmlDoc.getElementsByTagName("alias")[0].childNodes[0].nodeValue; Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.