tags:

views:

275

answers:

4

Hi All:
I am using the follwing javascript function in my jsp file

 <script language="javascript">

                    function openPopup()
                    {
                        var firstname = escape(<%=addressBean.getFirstName()%>);
                        var lastname = escape(<%=addressBean.getLastName()%>);
                        alert(firstname);
                        alert(lastname);
                        var mywindow = window.open('<%= link("*", "BBFBUpdatePickupInfo")%>&ShoppingCartKey=<%=cartKey%>&operation=<%=operation%>&isEproAcc=<%=isEpro%>&firstName="+firstname+"&lastName="+lastname+"&areaCode=<%=addressBean.getPhoneAreaCode()%>&phoneNum=<%=addressBean.getPhoneNumber()%>','static','width=625,height=500,toolbar=0,location=0,directories=0,status=0,menuBar=0,scrollBars=yes,resizable=0');
                        mywindow.focus();
                    }


                </script>

When addressBean.getFirstName() and addressBean.getLastName() returns names like "RED" as firstname and "WHITE" as lastname it is working fine. But when these two methods returns name like "VALENTINE" as firstname and "D'LOREY" as second name it is throwing below javascript error

  Error: Unterminated string constant
  Code: 0

Please help me to come out of this problem. Thanks in advance

+3  A: 

(You haven't mentioned what your server-side language is; below I've assumed Java, so make adjustments if it's something else.)

Think about what's actually getting sent to the browser when you do this:

var firstname = escape(<%=addressBean.getFirstName()%>);

When it goes out, it'll look like this:

var firstname = escape(JOE);

...which is almost certainly not what you want -- there are no quotes, for a start.

So first we need quotes:

var firstname = escape("<%=addressBean.getFirstName()%>");

But we also need to escape anything in the string that's going to be an issue in the resulting JavaScript string (on the server, when sending it to the browser). Off the cuff, that'll be at least quotes and backslashes, so:

var firstname = escape("<%=addressBean.getFirstName().replace("\\", "\\\\").replace("\"", "\\\"")%>");

...but that's not thorough at all, you'll need to handle lots of other cases (newlines in the string, etc.). Really, you're going to want a utility function that does the work thoroughly, handling (for instance) unicode sequences, etc., if there's any possibility they'll be in the source data (which there very much is, if this involves people's names).

And many have noted that instead of escape, you want encodeURIComponent.

T.J. Crowder
As other commenters have said, use `encodeURIComponent` instead of `escape`.
Andy E
i have used encodeURIComponent instead of escape but the problem exists
raja
@raja: `encodeURIComponent` is featured in exactly one sentence of my answer. Did you read the **rest** of it?
T.J. Crowder
@raja - That is in addition to the fixes described in the answer, no instead.
David Dorward
@Crowder, I read all other content also. But the fact is, the special character in the name vary dynamically. So i cant the particular special character in the code. One more thing i read out in the article is encodeURIComponent will not encode ~!*()'
raja
this won't work because he needs to escape on the SERVER side first.
Mike Sherov
@Mike: That's **exactly** what I'm telling him to do. The bits within `<%` and `%>` are server-side in JSP pages (and most other server-side templating systems, like ASP and such).
T.J. Crowder
@raja: The characters that need to be escaped are well-defined, just write (or locate) a function to do it. Fundamentally, the point is that you need to think about *what you're sending to the browser* and make sure that that's valid JavaScript. So that means putting quotes around the string content, escaping anything in the content that will mess things up (like other quotes, newlines, etc.). At the very least you need to handle quotes, backslashes, backspaces, newlines, carriage returns, and formfeeds. And you'll probably want to use unicode escape sequences for anything that isn't ASCII.
T.J. Crowder
sorry, TJ. I haven't had my coffee yet this morning.
Mike Sherov
@Mike: No worries :)
T.J. Crowder
+1  A: 

You need to properly format your string so that it is compatible with JavaScript syntax in variables definition.

This is what I do with PHP through the json_encode() function:

var firstname = <?=json_encode(addressBean.getFirstName())?>;

The JSON parser takes care of escaping the appropriate values and makes it JavaScript compatible.

I'm not familiar with your language, but if it's ASP you can try out aspjson.

Luca Matteis
I am using jsp and html in my code.
raja
*"You need to JSON encode your variable"* No, he doesn't. He just needs to send a properly-formatted string. Although actually, if he has a JSON encoder lying around, that might be a really convenient way to do it.
T.J. Crowder
@Crowder: Yes, that was badly written.
Luca Matteis
A: 

HTML and Javascript are 2 escapes. I had this problem and nonscientifically just observed switch (too fast) from georss to KML just solved

LarsOn
A: 

Just create the url server-side since it is made of server-side variables. No need to do it with strings and escapes in javascript when you have everything at hand server-side.

Alsciende