0

I'm working on an android/ios app. i need some sort of infinit scroll, so when user scroll and reaching the end of the page new content will load for him.

In the native code i store the new content in an string and then append it to the page with javascript/jquery.

for android:

String js = "javascript:(function() { document.body.innerHTML += '" + newContent + "';}())";
loadUrl(js);

so far so good, BUT if my newContent contains a set of specific char this code will fail and nothing append.

I discovered this char so far: ' \n « »  

If i replace this chars in the newContent then the code works fine and new content will append to body.

The problem is every time i think it's over and i find all illigal char a new char cause my code to fail.

I also tried to parse my string to html then add it, but it fails also.

js = "javascript:(function(){var html = $.parseHTML( '"+newContent+"' ); $(\"body\").append(html);}())";
loadUrl(js);

so is there any general way to get ride of this illegal chars?

any help would be appreciated.

mehdok
  • 1,499
  • 4
  • 29
  • 54

1 Answers1

1

Your text contains unescaped special characters which causes error. Try escaping the text with encodeURI

Edit: It might be though that the javascript already fails because of the special characters, then you would have to escape the string with Java already. Try to google for "java escape string for javascript" and you'll find plenty of information.

Danmoreng
  • 2,367
  • 1
  • 19
  • 32
  • tnx alot, i will try it. – mehdok Oct 16 '15 at 09:27
  • I tried `String results = StringEscapeUtils.escapeJavaScript(str);` and i give me something like this: `

    \u064A\u0648\u0645 \u0643\u0627\u0645\u0644. 3 \u0640 \u0627\u0644\u0642\u0635\u0631

    `, it sometimes works and sometimes not !! whats the problem?
    – mehdok Oct 16 '15 at 10:27
  • I still need to replace `«` `» ` ` `, how to get rid of these? – mehdok Oct 16 '15 at 11:11
  • 1
    Check this: http://stackoverflow.com/questions/16207625/string-contents-are-same-but-equals-method-returns-false   is html code already, probably you have to unescape that back, I am not sure. You have to try it out: StringEscapeUtils.unescapeHTML(str). – Danmoreng Oct 16 '15 at 11:28
  • I tried `body = StringEscapeUtils.unescapeHtml4(body); body = StringEscapeUtils.escapeEcmaScript(body);` But it still fail on some string. like body of this page: http://pastebin.com/CMNDsgt5 – mehdok Oct 16 '15 at 12:21
  • Well, I'm sorry got no other idea. – Danmoreng Oct 16 '15 at 13:25