user1495475
user1495475

Reputation: 1057

vbscript:Get attribute from classic asp xml object into javascript

I`m having problem in getting an attribute from a xml object into JavaScript from classic asp. The following is my code:

if(len>0){
<%for xx=0 to SNodes.length-1%>//asp code
{
 //Javascript code
var IXmlNode=xmlnewObj.createElement("I");
IXmlNode.setAttribute("a",document.getElementById('a'+xx).value);
IXmlNode.setAttribute("X","<%=SNodes.item(xx).getAttribute("PP")%>");
xmlnewObj.documentElement.appendChild(IXmlNode); 
<% next %>//asp code
}
}

Here SNodes has xml like:

<tag><tag1 a="iii" PP="asdasdf"/><tag1 a="aaa" PP="asdasdf"></tag>

Only this line is troubling me:

IXmlNode.setAttribute("X","<%=SNodes.item(xx).getAttribute("PP")%>");

What is wrong with this line?

Upvotes: 1

Views: 462

Answers (2)

Abeer Varan Dey
Abeer Varan Dey

Reputation: 14

Escape your quotes in the second parameter to the setAttribute function with backslashes, or use single quotes.

Upvotes: 0

AardVark71
AardVark71

Reputation: 4126

What kind of javascript are you trying to output in the first place? As it stand now you'll get something like this which would never work:

if (len > 0) {
    {
        var IXmlNode = xmlnewObj.createElement("I");
            IXmlNode.setAttribute("a", document.getElementById('a' + xx).value);
        IXmlNode.setAttribute("X", "PP");
        xmlnewObj.documentElement.appendChild(IXmlNode);
    }
    {
        var IXmlNode = xmlnewObj.createElement("I");
        IXmlNode.setAttribute("a", document.getElementById('a' + xx).value);
        IXmlNode.setAttribute("X", "PP2");
        xmlnewObj.documentElement.appendChild(IXmlNode);
    }
}

It could work like this, but I still doubt that's what you need:

if (len > 0) {
    (function() {
        var IXmlNode = xmlnewObj.createElement("I");
            IXmlNode.setAttribute("a", document.getElementById('a' + xx).value);
        IXmlNode.setAttribute("X", "asdasdf");
        xmlnewObj.documentElement.appendChild(IXmlNode);
    })();
    (function() {
        var IXmlNode = xmlnewObj.createElement("I");
        IXmlNode.setAttribute("a", document.getElementById('a' + xx).value);
        IXmlNode.setAttribute("X","asdasdf");
        xmlnewObj.documentElement.appendChild(IXmlNode);
    })();
}

You're best option is probably to turn it into a function and pass your asp variables into it:

if(len>0){
    <%for xx=0 to SNodes.length-1%>//asp code
    {
        xmlAppender(<%=xx%>, <%=SNodes.item(xx).getAttribute("PP")%>);
    }
    <% next %>
 }

var xmlAppender = function(i, childData) {
        var IXmlNode = xmlnewObj.createElement("I");
        IXmlNode.setAttribute("a", document.getElementById('a' + i).value);
        IXmlNode.setAttribute("X", childData);
        xmlnewObj.documentElement.appendChild(IXmlNode);
    };​

Upvotes: 1

Related Questions