Echoing javascript alert() message doesn't work when message is too long

all, I have this code:

    echo "<script>alert('Your contact information has been sent.  A respresentative will contact you shortly.  You will now be redirected to the website's home page...');
	 window.location.href='../index.php';</script>";

when i go to the page, nothing happens. however, this shortened message does work:

    echo "<script>alert('Your contact information has been sent.  A respresentative will contact you shortly.');
	  window.location.href='../reporting/trafficreport.php';</script>";

my only guess here is that the first code doesn’t work cuz the message string is too long for the alert dialog box to display it on one line? If that is the case, then I need to add line breaks, and I’ve tried almost every type of syntax I know of, using both < br > and < br / > tags inside and outside the quote marks and I still get no action on the page. am I to use \r\n\ or something instead? what is the syntax for fixing this issue? thanks!

\n 

should work for new line in your alert box.

  • website’s

You are breaking your string with the apostrophe… you will need to escape it:

website\'s

There is no max character limit for an Alert box… (browser dependent I believe)… roughly around 1000 characters before truncating…

whispers, I tried putting 2 ‘’\n’’ breaks in it and I still get nothing. if there’s no limit for an alert box, what am I not getting one? here is what I have now:

echo "<script>alert('Your contact information has been sent.\n\n  A respresentative will contact you shortly.');window.location.href='../reporting/trafficreport.php';</script>";

Your first code snippet didn’t work because your alert text had a ' in it, in the word site's. Because you were using ' to delimit your string, javascript thought that was the end of your string and couldn’t parse the remainder of your code. You can fix that by using your original snippet, and escaping the quote with backslash like so: \'. This would give you:

echo "<script>alert('Your contact information has been sent.  A respresentative will contact you shortly.  You will now be redirected to the website\'s home page...');
 window.location.href='../index.php';</script>";

You can add new lines using \n like whispers said; however, since you’re echoing from PHP you have a string that contains another string, so you have to work a little harder to escape your characters correctly. You need to escape the backslash itself so that PHP will output it to the frontend, so that javscript will find the \n and add a new line. You need to escape the backslash like so, leaving you with \\n. This would give you:

echo "<script>alert('Your contact information has been sent.\\nA respresentative will contact you shortly.  You will now be redirected to the website\'s home page...');
 window.location.href='../index.php';</script>";
1 Like

works fine. thanks much! i learned something today!

I didnt test the \n because I know it works… had I tested with the php echo… I would have seen the secondary issue.

I had already explained/posted about escaping the apostrophe in the string.

the initial example(s) did not have any line breaks in it… and seemed to only be a ‘solution’ in an attempt to fix the broken string issue.

Sponsor our Newsletter | Privacy Policy | Terms of Service