-3

I am writing a webpage for reading notices/messages which requires me to take the notice as input from a database. I am taking the input using php and passing it to a javascript function using echo.

Now the problem is that if the notice is very large(>1000 characters or something) the function stops working. Is there a way to store large strings in javascript variables?

Thankn you in advance.

The javascript function goes like this:

function display(subject, notice, from,to)
{
    alert(notice);
    document.getElementById('reader_default').innerHTML='';
    document.getElementById('subject').innerHTML="SUBJECT : "+subject;
    document.getElementById('from').innerHTML="From: "+from;
    document.getElementById('to').innerHTML="To: "+to;
    document.getElementById('noticedisp').innerHTML=""+notice;
}

The php variable pass goes like:

echo '<a href="javascript:void(0)" onclick="display(\''.ucfirst($subject).'\',\''.$notice.'\',\''.ucfirst($hostel).' Hostel'.'\',\''.$toid.'\')" style="padding:2%; text-decoration:none; color:#0033CC;">'.ucfirst($subject).'</a>'.'<br /><span style="padding:2%;"><i>From: </i>'.ucfirst($hostel).'Hostel</span><br/>'.'<br/><br/>';

First I provided the following string:

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut molestie luctus lectus, nec volutpat ipsum porta sit amet. Phasellus ut egestas sapien. Curabitur mauris erat, eleifend quis adipiscing sollicitudin, posuere tempor ante. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut ut erat nibh. Maecenas malesuada lacus pulvinar risus placerat convallis. Cras vitae urna in elit aliquam mollis a eu nisi. Nulla id molestie ante. Aenean faucibus interdum nibh, in tristique dui viverra eu. Nulla facilisi. Integer at placerat augue.

Sed quis risus dolor, in tristique est. Maecenas non tellus vel quam blandit elementum. Sed vestibulum dolor non mi eleifend luctus. Proin interdum mollis congue. Fusce quis consequat augue. Integer dictum lectus a ligula egestas in volutpat odio tincidunt. Sed placerat dictum velit quis dapibus.

Cras non quam ut neque auctor hendrerit. Phasellus tincidunt dapibus metus, id rutrum purus fringilla vel. Pellentesque ac sapien elit. Maecenas nibh justo, lobortis id volutpat a, bibendum egestas enim. Vivamus sit amet justo vel tortor ullamcorper molestie ac sed nibh. Donec adipiscing dui sed odio hendrerit quis aliquet turpis posuere. Donec pharetra mi ut odio luctus sodales. Donec elit nisi, cursus elementum cursus at, scelerisque quis ante. Nunc vel ipsum ut nibh dictum tempus ut ut tortor. Donec dignissim pharetra scelerisque. Curabitur pellentesque urna a velit ultricies at lacinia leo euismod. Nulla id dolor ut odio ultrices accumsan in id erat. Aliquam lorem turpis, tristique nec tincidunt id, lacinia sit amet ante.

When this didn't work after trying everything, I shortened the string to:

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut molestie luctus lectus, nec volutpat ipsum porta sit amet. Phasellus ut egestas sapien. Curabitur mauris erat, eleifend quis adipiscing sollicitudin, posuere tempor ante. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut ut erat nibh. Maecenas malesuada lacus pulvinar risus placerat convallis. Cras vitae urna in elit aliquam mollis a eu nisi. Nulla id molestie ante. Aenean faucibus interdum nibh, in tristique dui viverra eu. Nulla facilisi. Integer at placerat augue.

And this works!!

Community
  • 1
  • 1
  • 9
    1000 is tiny. Even 100,000 won't be enough to hit a limit. You must have some mistake in your function. – Dave Apr 04 '13 at 22:48
  • Afaik there are no issues in using large strings with JavaScript. I have cases where a string contained over 1MB, and it worked like a charm. Provide us with some code to analyze. – Havenard Apr 04 '13 at 22:48
  • Yeah 1000 chars is nothing in the real world, show us some code. Are you using JSON? – elclanrs Apr 04 '13 at 22:48
  • 4
    I don't think your problem is the size of the message. If you're doing this via `GET`, then your limitation is there. Try sending the data via `POST`. – Cypher Apr 04 '13 at 22:49
  • 1
    http://stackoverflow.com/questions/5926263/javascript-object-max-size-limit – Cypher Apr 04 '13 at 22:50
  • @Havenard Actually the code works fine for smaller strings. That I have already checked. If the size is not the issue then I am confused about what can it be. – vish_anything Apr 04 '13 at 22:57
  • 1
    Check if you are passing some " or ' along with the string. Those must be escaped. – Havenard Apr 04 '13 at 22:59
  • Care to define what "stops working" means? You're getting console errors? The browser hangs? Something else? –  Apr 04 '13 at 23:09
  • @Havenard No, I passed lorem ipsum text. It didn't have any " or '. Also, I wrapped it in htmlspecialchars(). Still wouldn't work. – vish_anything Apr 04 '13 at 23:10
  • @amnotiam javascript function when stops working does nothing. – vish_anything Apr 04 '13 at 23:11
  • Everything else works? There's no console errors? –  Apr 04 '13 at 23:12
  • @amnotiam yup! No console errors. Nothing else at all. It's just when I shorten the length of the string in the database it works. – vish_anything Apr 04 '13 at 23:14
  • Edit your question to show an example of the rendered output given to the browser that causes the issue. –  Apr 04 '13 at 23:16

2 Answers2

1

If javascript parse it like a string it will break on linebreaks. Instead of [ENTER] you should try use \n\[ENTER]..

CRABOLO
  • 8,605
  • 39
  • 41
  • 68
fernandosavio
  • 9,849
  • 4
  • 24
  • 34
0

Your data probably has single or double quotes in it. I would wrap what you're echoing in htmlspecialchars, so that these and any other problematic HTML chars won't break your output.

echo htmlspecialchars('<a href="javascript:void(0)" onclick="display(\''.ucfirst($subject).'\',\''.$notice.'\',\''.ucfirst($hostel).' Hostel'.'\',\''.$toid.'\')" style="padding:2%; text-decoration:none; color:#0033CC;">'.ucfirst($subject).'</a>'.'<br /><span style="padding:2%;"><i>From: </i>'.ucfirst($hostel).'Hostel</span><br/>'.'<br/><br/>');

http://php.net/manual/en/function.htmlspecialchars.php

MattDiamant
  • 8,561
  • 4
  • 37
  • 46
  • No, I passed lorem ipsum text. It didn't have any " or '. Also, I wrapped it in htmlspecialchars(). Still wouldn't work. – vish_anything Apr 04 '13 at 23:14