Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

JavaScript Forum


You are currently viewing our JavaScript Forum as a guest. Please register to participate.
Login



Reply
Escape returns in Javascript
Old 06-17-2011, 04:23 PM Escape returns in Javascript
Skilled Talker

Posts: 87
Trades: 0
Okay. What would I do with returns in a Javascript function parameter? For example:

Code:
onclick="displayEditNotesModal('1', 'Hi.

Hi again.', 'collegeNotes');"
Wreaks havoc! Is there a PHP or Javascript function that will either strip the returns or somehow encode them so that it works? Thanks so much.
__________________

Please login or register to view this content. Registration is FREE
- The most comprehensive online resource for students and educators.
yuechen is offline
Reply With Quote
View Public Profile Visit yuechen's homepage!
 
 
Register now for full access!
Old 06-17-2011, 07:29 PM Re: Escape returns in Javascript
lizciz's Avatar
Super Spam Talker

Posts: 807
Name: Mattias Nordahl
Location: Sweden
Trades: 0
Write all newlines as \n. So,

Code:
onclick="displayEditNotesModal('1', 'Hi.\n\nHi again.', 'collegeNotes');"
__________________
Your answers will only be as good as your question. Formulate it well and give all the necessary information.
lizciz is online now
Reply With Quote
View Public Profile Visit lizciz's homepage!
 
Old 06-17-2011, 08:14 PM Re: Escape returns in Javascript
Skilled Talker

Posts: 87
Trades: 0
Okay, that's what I thought, but here's the actual situation. A user inputs some text into a <textarea> and a submit button is pressed. The text is processed using PHP from the $_POST Super Global Variable. How can I detect returns and replace them with \n? I've tried preg_replace(), but to no avail.
__________________

Please login or register to view this content. Registration is FREE
- The most comprehensive online resource for students and educators.
yuechen is offline
Reply With Quote
View Public Profile Visit yuechen's homepage!
 
Old 06-18-2011, 11:10 AM Re: Escape returns in Javascript
lizciz's Avatar
Super Spam Talker

Posts: 807
Name: Mattias Nordahl
Location: Sweden
Trades: 0
I'm not at all sure about this, there is in fact some guessing going on. But as far as I know, the newline character '\n' is in fact a character, just like 'a' or 'b', which just happens to be represented as the two characters \ and n.

Now here's the guessing part. When the php parser finds the \ and n characters together they are parsed as a newline. What you want is the php parser to not parse it as a newline, but just the two characters \ and n, so that that parsing can be done by the javascript instead when you pass it to your function.

Again, just guessing, but perhaps you could replace all '\n' with an escaped backslash, '\\n'. Something like this
PHP Code:
str_replace('\n''\\n'$your_string); 
Otherwise, perhaps someboy else can shed some light on this
__________________
Your answers will only be as good as your question. Formulate it well and give all the necessary information.
lizciz is online now
Reply With Quote
View Public Profile Visit lizciz's homepage!
 
Old 06-19-2011, 07:31 AM Re: Escape returns in Javascript
chrishirst's Avatar
Missing! presumed drunk.

Posts: 42,383
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
Test for chr(10) and chr(13)

10 is ascii new line
13 is ascii carriage return.
__________________
Chris. ->>
Please login or register to view this content. Registration is FREE
<<-

A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 06-19-2011, 07:53 AM Re: Escape returns in Javascript
Skilled Talker

Posts: 87
Trades: 0
Thank you! I've found an alternate way to do what I wanted, but this will definitely come in handy later.
__________________

Please login or register to view this content. Registration is FREE
- The most comprehensive online resource for students and educators.
yuechen is offline
Reply With Quote
View Public Profile Visit yuechen's homepage!
 
Old 06-19-2011, 07:57 AM Re: Escape returns in Javascript
chrishirst's Avatar
Missing! presumed drunk.

Posts: 42,383
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
Would you post the alternate way please. As it may be of use to future readers of this thread.
__________________
Chris. ->>
Please login or register to view this content. Registration is FREE
<<-

A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 06-19-2011, 08:00 AM Re: Escape returns in Javascript
Skilled Talker

Posts: 87
Trades: 0
Oh, it has nothing to do with the actual solution. What I was doing before was allowing a user to edit a block of text using a textarea. However, I was passing the text to the Javascript function displayEditNotesModal(), which would display a modal with a <textarea> element and a save button. The form would then be processed by php. The problem was in passing the text to the modal. Now, however, I simply display the text inside a <textarea> element and added a submit button below. So, no longer a need to open the modal or call the function displayEditNotesModal().
__________________

Please login or register to view this content. Registration is FREE
- The most comprehensive online resource for students and educators.
yuechen is offline
Reply With Quote
View Public Profile Visit yuechen's homepage!
 
Reply     « Reply to Escape returns in Javascript
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.89242 seconds with 12 queries