0

I trying to store a piece of xml into a string variable in javascript and in IE8 it keeps throwing an error. FireFox doesn't show the error but of course in IE8 it does. Swictching browsers isn't an option so I have to try to solve this one.

The purpose of the function is to check if the items of a list exist in an xml object or not. So if there is a better way to do that check I am open to that as well. The system we pull from has a function to convert the xml to a string. At the bottom is an output of what that retrieves. Here is the function.

function commodityExists(newCommodityCode){
        var comExists = new Boolean(0);
        newCommodityCode =  ">" + newCommodityCode + "<"
        var strXML = 'tw.local.aribaHeader.commodities.toXMLString()';  //ERROR HERE
        strXML = strXML.toString();
        if(strXML.indexOf(newCommodityCode,0)>0){            
            comExists=true;            
        }
        return comExists;
    };  

Here is the output from strXML.toString(); but as you can see it is essentially xml.

        var strXML = ‘<variable type="NameValuePair[]">
                      <item type="NameValuePair">
                      <name type="String"><![CDATA[No Data Found]]></name>
                      <value type="String"><![CDATA[95990070]]></value>
                      </item>
                      </variable>’;
5
  • Also, I wouldn't use new Boolean(0) for false. if(new Boolean(0)) console.log('a'); will log 'a'. In other words, the Boolean object which wraps false is truthy. Commented Oct 3, 2012 at 4:48
  • Also, there are other concerning problems with this code. It almost looks like an interview question or a homework problem. "How many things are wrong with the following code?" Not meaning to be offensive, but as the others have mentioned the line you indicated can't throw an error. Commented Oct 3, 2012 at 5:03
  • Thank you everyone for your feedback. We are using IBM's Lombardi Websphere with a custom UI, so we are pulling the xml from bpms server so its kinda hard to translate this question to everyone without being able to explain the environment. For example the actual code for the str variable is ar strXML = '<#=tw.local.aribaHeader.commodities.toXMLString()#>'; because it's actually inserting a server variable. Some of the other things like the boolean variable I was playing around with when I was debugging so I absolutely agree. Commented Oct 3, 2012 at 5:41
  • For the newCommodityCode I am adding brackets to the string, that way I am searching for a full string... so a search for >123< won't return true for the variable of >51236< Commented Oct 3, 2012 at 5:44
  • Now we're getting somewhere. My guess is it has something to do with the string being returned by toXMLString. At first glance it looks like a line break problem, because you're showing line breaks in your strXML string, which is not allowed in JavaScript (without ending each line in a backslash). However, it's not allowed on Firefox just as much as on IE, so if that was the case it shouldn't work on Firefox either. Could you post the raw JavaScript output which the browser receives from the server? Commented Oct 3, 2012 at 5:47

3 Answers 3

3

I don't know what you think the code is doing, here is an explanation of what it does:

> function commodityExists(newCommodityCode){
>     var comExists = new Boolean(0);

Do you really want a Boolean object? This function might return a Boolean object or primitive depending on what happens later. Consider:

      var comExists = false;

.

>     newCommodityCode =  ">" + newCommodityCode + "<"

That overwrites whatever value was passed to newCommodityCode from the call.

>     var strXML = 'tw.local.aribaHeader.commodities.toXMLString()';  //ERROR HERE

I can't see how that throws an error, it's a simple assignment of a string.

>     strXML = strXML.toString();

That effectivly does nothing - it calls the toString method of a string, which will just return the same string.

>     if(strXML.indexOf(newCommodityCode,0)>0){            

That test will always be false, since the value of nweCommodityCode is hard coded in the function and does not exist in the (hard coded) value of strXML.

>         comExists = true;            
>     }
>     return comExists; };

The function will always return false (though the original will return a Boolean object with a value of false).

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

1 Comment

The toXMLString is simply part of the WLE syntax that will return an XML object, but it apparently is not an actual string. It's an xmlobject. After this discussion here it really looks to me like my approach won't work so I'll go back to the drawing board on this one. I'll close out this question. Thanks again for your guys feedback. I was afraid this might be the case... I was just hoping there was some amazing item out there that I didn't know about. Thanks guys!
0

You're creating a string:

var strXML = 'tw.local.aribaHeader.commodities.toXMLString()';  //ERROR HERE
             ^---                                           ^---

then converting that string to... a string?

strXML = strXML.toString();

Where would this tw object be defined that you seem to be attempting to use? Because as your code is written now, you're not calling a .toXMLString() method on something in this tw object. You're just assigning the literal text of an object call as a string itself.

3 Comments

You can think of that whole line a big piece of xml code.... <variable type="NameValuePair[]"> <item type="NameValuePair"> <name type="String"><![CDATA[No Data Found]]></name> <value type="String"><![CDATA[95990070]]></value> </item> </variable>
I then need to make that a string so I can use the indexOf function so that is why I use the strXML.toString() There error I'm getting is "Unterminated string constant"... which leads me to believe that something is wrong in with storing the xml into the strXML variable.
Just found this... stackoverflow.com/questions/5125506/… so I am going to close this question.
0

The approach I was trying to take will not work because I am dynamically populating the xml so there is no way for me to escape the characters (well there probably is somehow but clearly it is not worth it). Storing HTML or XML code in javascript variables

Instead I am moving the comparison to the server side instead of retrieving the xml and comparing on the client side and posting back the results via ajax unless someone has a better reccomendation.

Comments

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.