开发者

new line in javascript passed to get argument

Im using .net multiline control. The I use jQuery to get data from that control:

$('.detailsCommentContent').val()

in this moment when I alert that value new lines are visible.

Then I make request www.example.com?commentContent= + "$('.detailsCommentContent').val()"

And in the http request there are no newLines signs at开发者_运维百科 this moment.

What should I do to keep this new Lines symbols ?

thanks for help


Use a POST request for data that changes something, not a GET. That way you can POST whatever data you like without encoding it, and best of all: bots that are just spidering your page can't do any damage to your site. See also http://thedailywtf.com/Articles/The_Spider_of_Doom.aspx


You're going to have to URL-encode the value:

var url = "http://www.yourpage.com?commentContent=" + 
  encodeURIComponent($('.detailsCommentContent').val());


<Textarea> elements store "new lines" based on the host operating system, and sometimes even just the browser.

Between windows, mac, and linux, this could be \r\n, \n, or \r.

\r is a "carriage return", and \n is a "line feed". (C++ people will be familiar with crlf)

These characters make up a new line.

Here's the problem:

When you get the value of a textarea, it doesn't always preserve the line marker when going to store to a variable. You can try and replace the line-break, or encode it as per @Pointy's answer.

This question's existed in a few forms, but they're all hard to find because we have a lot of differnt names for the line-break.

Basically, the browser hates you for some reason, you need to hide what you're doing because it's trying to be really really smart.


use the escape() function on the value, or use a POST instead of a GET.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜