• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1134
  • Last Modified:

unterminated string constant on in IE for complex function

Hello,
I'm attempting to get this code to work in IE.
The script I have below works fine in Firefox but doesn't work in IE.
Does anyone have any suggestions to make this work in IE?

The error I get is "ERROR: unterminated string constant"
The error is on line 55 which is the line below starting with inputs[i].onclick.


 var texts = new Array();
      function spanify()
      {
                  //addField();                                         
                var inputs = document.getElementsByTagName('input');
                 for(var i=0; i<inputs.length; i++)
                  {
                        var curnode = inputs[i].nextSibling;
                        var newSpan = document.createElement('span');
                        while(curnode != null)
                        {
                                if(curnode.tagName != null && curnode.tagName.match(/input/i))
                                {
                                          curnode = null;
                                }
                                else
                                {
                                          newSpan.appendChild(curnode.cloneNode(true));
                                          curnode = curnode.nextSibling;
                                }
                        }
                  inputs[i].onclick = new Function("var answers = ('" + megatrim(newSpan.innerHTML.toString()) + "'); popinframe(answers); doSubmit();");      
              }
             
      }

0
cubicalmonkey
Asked:
cubicalmonkey
  • 6
  • 6
  • 4
1 Solution
 
arunrsCommented:
What exactly are you trying to do in the megatrim function.

Sometimes, there might be \n character that need to be replaced which might cause this problem.

try adding this inside the megatrim

megatrim (inputStr) {
...
...
inputStr = inputStr.replace(/\n/g, " "); // This will replace all the \n character with space.
}

Hope this is useful.
arunrs

0
 
jmanGJHS97Commented:
getElementsByTagName isn't valid in IE6.  Try something like this instead:

http://www.thescripts.com/forum/thread153313.html
0
 
jmanGJHS97Commented:
Or, use getElementById
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
cubicalmonkeyAuthor Commented:
Basicinstinct help me with the megtrim function.
The link below explains what's happening in the code.
http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_22842770.html

Thanks for the advise, unfortunately I'm unable to use the getElementById option however, I'll check out the link jmang posted.  
0
 
jmanGJHS97Commented:
Why can't you use getElementById?
0
 
cubicalmonkeyAuthor Commented:
The radio buttons on the form is dynamically generated by a java applet which I don't have any control over nor know anything about.
http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_22842770.html

Once the page is written, I'm adding some javascript to the <input tags in order for the form to return which option is selected.
I suppose this explanation would make more sense if you check out my related question in the link provided.
0
 
jmanGJHS97Commented:
Ok, well technically if there are form controls on the page, then you can navigate through the DOM to get at the inputs.  Granted, this is a hassle, but if that's not feasible, see if you can get that example I sent to work.

Thanks for the clarification.
0
 
arunrsCommented:
HI,

The unterminated string constant could also happen if there are any single quotes (') or double quotes(") as a part of the innerHTML value. If so we need to handle those cases as well.

If you want to maintain the quotes then you can use the following

function megatrim(str) {
        str = str.replace(/'/g, "\\\'");
      str = str.replace(/"/g, "\\\"");
      atr = str.replace(/^\s+|\s+$|\n/g, '');
        return str;
}

If you do not want to preserve the quotes, you can try this.

function megatrim(str) {
        str = str.replace(/'/g, "");
      str = str.replace(/"/g, "");
      atr = str.replace(/^\s+|\s+$|\n/g, '');
        return str;
}

If you are still getting the same error, can you please post what is the value of the innerHTML that you are getting?

Hope this is useful.
arunrs
Hope this is useful.
0
 
arunrsCommented:
There was a small spell mistake in the code ..

Change
atr = str.replace(/^\s+|\s+$|\n/g, '');
 to
str = str.replace(/^\s+|\s+$|\n/g, '');

arunrs
0
 
cubicalmonkeyAuthor Commented:
Arunrs,
Thank you for your suggestion.
I've replaced the existing megatrim function with your function however,
I'm still experiencing the same error in IE.
Worked great in Firefox, even firebug doesn't spit out any warnings ect...

As requested this is the data retrieved in innerHTML.
Again the content within the form is dynamically populated from a java applet.
The content changes based on which options are selected.


<FORM NAME=MYFORM id='MYFORM' METHOD=POST ACTION=advisor.jsp OnSubmit='doSubmit()'>
<INPUT TYPE=HIDDEN NAME=ROLLUP VALUE=0><p><br><b>Select the setup the customer having problems with </b></B><BR>
<INPUT TYPE=RADIO OnClick='doSubmit()' NAME=default_Q1_2_2136 VALUE=222085 >1 Computer directly connected to the modem<BR>
<INPUT TYPE=RADIO OnClick='doSubmit()' NAME=default_Q1_2_2136 VALUE=222086>1 Computer out of many having a problem connected through a router<BR>
<INPUT TYPE=RADIO OnClick='doSubmit()' NAME=default_Q1_2_2136 VALUE=222087>Are ALL computers connected through the router having a problem<P>
<INPUT TYPE=HIDDEN NAME=CompanyID VALUE=2><INPUT TYPE=HIDDEN NAME=RuleID VALUE=2136>
<INPUT TYPE=HIDDEN NAME=SubRuleID VALUE=81926>
<INPUT TYPE=HIDDEN NAME=RuleControlID VALUE=2136>
<input type=hidden name=Path value=http://web01.ad.net:8080/advisor/pages/templates/>
<input type=hidden name=History value=''>
<input type=hidden name=Current value=''>
<input type=hidden name=Template value=imtg12.html>
</FORM>
0
 
cubicalmonkeyAuthor Commented:
Strange, when I escape the " in the line of code shown below, IE is happy. No Errors.
But when I call the fucntion popinframe(answers);    
Which esentially just displays when ever was after the <input> tag, it shows +megatrim(newSpan.innerHTML.toString())+  instead of the correct data that shows in Firefox.


inputs[i].onclick = new Function("var answers=('\"+megatrim(newSpan.innerHTML.toString())+\"');popinframe(answers);doSubmit();");
0
 
cubicalmonkeyAuthor Commented:
Anyone have any other Ideas?

Although I'm only a novice at best, I wasn't aware getElementsByTagName isn't valid in IE6.
I thought I've used that method some place, maybe not...
0
 
arunrsCommented:
HI,

I think the problem is basically because of "\r" character in addition to the "\n" character.

 Try this and see if this solves your problem.

function megatrim(str) {
        str = str.replace(/'/g, "\\\'");
      str = str.replace(/"/g, "\\\"");
      str = str.replace(/^\s+|\s+$|\n|\r/g, '');
        return str;
}

arunrs
0
 
arunrsCommented:
I guess you can use getElementsByTagName in IE 6.0. It should not create any problem because I have used it in my code.

arunrs
0
 
cubicalmonkeyAuthor Commented:
arunrs,

Thank you very much!
That worked to resolve my issue.
I cant thank you enough!
0
 
arunrsCommented:
Not a problem. Sorry I could not reply back immediately after your reply with the innerHTML content as I was bit busy with my work.

arunrs
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 6
  • 6
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now