line breaks in Javascript alert()

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

line breaks in Javascript alert()

Post by raghavan20 »

Alright, I have set up alerts at various points in my application.
I am trying to display a multi-line message in the alert box which is done by using "\n" as line breaks.
I have personally worked with line breaks in alert before and they worked but i am not able to figure out the problem with this one.

This is a calendar application, you can see a few dates in it being highlighted/anchored and I have set up alerts for those, which do not happen.

on view source, the alert message looks alright and here is the alert output
page: http://raghavan.100webcustomers.com/Calendar1.class.php

Code: Select all

onclick='alert("This is the URL you give as input.
 the action can be anything like editPost/viewPost/viewAppointment/viewBlog/viewBirthday.
The id can be post_id, blog_id, appointment_id, birthday_record_id.
yourfilename.ext?action=youraction&id=32");'
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Post by anjanesh »

I dont think your onclick event gets parsed like that.
No sure abt this, but Im sure that <sp an>text</sp an> is not parsed as <span>text</span>.

Why dont you replace

Code: Select all

onclick='alert("This is the URL you give as input.
 the action can be anything like editPost/viewPost/viewAppointment/viewBlog/viewBirthday.
The id can be post_id, blog_id, appointment_id, birthday_record_id.
yourfilename.ext?action=youraction&id=32");'
with

Code: Select all

onclick='alert("This is the URL you give as input.\n the action can be anything like \n editPost/viewPost/viewAppointment/viewBlog/viewBirthday.\n The id can be post_id, blog_id, appointment_id, birthday_record_id.\n yourfilename.ext?action=youraction&id=32");'
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Post by raghavan20 »

alright let me give you more information

Code: Select all

$message = "This is the URL you give as input.\n the action can be anything like editPost/viewPost/viewAppointment/viewBlog/viewBirthday.\n";
$message .= "The id can be post_id, blog_id, appointment_id, birthday_record_id.\n";
echo "<span><a href='".$this->redirectLink.$tempArray[$i]." ' onclick='alert(\"".$message.$this->redirectLink.$tempArray[$i]."\");' class='links'>$i</a></span>";
edit: the problem is, rather interpreting the \n within the alert box, the messages was interpreted earlier and alert can not understand the message which has got several lines.

now i need to know how to format the above message so that the alert interprets by itself.
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Post by anjanesh »

Since \n in $message is parsed on the server side - PHP part, you'll need to replace \n with \\n
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Post by raghavan20 »

yeah....that solved my problem. thanks very much anjanesh.
I feel bad that I could not think of it. :(
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

added info: if the carriage return is output in the string (not using \n), Javascript doesn't support multi-line strings.
pilau
Forum Regular
Posts: 594
Joined: Sat Jul 09, 2005 10:22 am
Location: Israel

Post by pilau »

Raghavan20, I noticed some problems with your calendar.
First of all, you can only get to January 2050 and not further. If you click next you will get to January 1970 (UNIX timestamp anyone?)

Second, If you are on January 1970, and you click previous, you won't go back to Jan 2050. Don't know whther it's an error or not, but thats all.
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Post by raghavan20 »

pilau, I had a regex script to validate year and month and I earlier had the limit from 1900-2050. now I have increased it from 1800 - 2060
Post Reply