Multi line strings with Javascript

This post shows how to do multi line strings with Javascript. Please note that it is likely the method presented here is not part of the Javascript/ECMA standard but it does work (and has been tested by me) in Internet Explorer 6 to 8, Firefox 1 to 3 and the current releases of Opera, Safari and Chrome.

Add to the end of each line

It’s simple… add a to the end of each line with nothing after it and you have a multi-line string:

var s = "abc
 def
 ghi
 jkl";
 

Now display the above:

window.alert(s);
 

which shows:

abcdefghijkl
 

Note that the newlines are not rendered at all, so you need to make sure the appropriate spacing or line breaks (n) etc are present in the string.

Another approach

Another approach I found is shown below, but as far as I can tell it only works in Firefox 2+ and not at all in IE, Opera, Chrome and Safari.

var s = (<r><![CDATA[abc
 def
 ghi
 jkl]]></r>).toString();
 

This does render the line breaks so if you used window.alert on the above it would show:

abc
 def
 ghi
 jkl
 

So don’t use the second approach due to lack of browser support 🙂

Target links to _top with jQuery

Amazingly some people still use <frameset> and <frame> tags around the web, as I discovered the other day when someone linked my Running Calendar website into their frameset website. A common approach to this problem is to detect if the page is in a frameset and to bust out of it; in this post I offer an alternative which is to change the target for all links to _top where a target is not already set.

jQuery: set title of anchor tags to the href for offsite links

Over a year ago I posted how to use jQuery to make all offsite links open in a new window. I recently received a comment on that page asking how to make the title attribute of all anchor tags on a page for offsite links be the same as the href. This post shows how to do this with jQuery, but also leaves any <a> tags with a title as-is.

Write content into a dynamic Javascript popup

I’ve posted a few articles about the Facebox lightbox dialog recently but sometimes a quick and dirty post of content into a regular window.open popup is all that is required. This post shows how to open a Javascript window and write content into it without having to load another page.