Topamax Online Buy Prozac Aldactone Online Buy Toprol XL Stromectol Online Buy Amoxil Glucotrol Online Buy Stromectol Clarinex Online Buy Nexium

Jesus, this took me a bazillion hours to figure out.

OK, so I wanted to set a String variable in Actionscript. The actual value I wanted was a block of Javascript code (more on why in a second). In PHP we’re allowed to use something called heredoc syntax. So I could write my code like this:

$str = <<<EOD
Example of string
spanning multiple lines
using heredoc syntax.
EOD;

and then my variable would contain the multi-line string with line breaks and all. Sweet, that’s what I wanted to do. Turns out AS3 doesn’t support heredoc syntax. So that sucks.

I spent the next few hours banging my head on a wall trying to figure out how to cut and paste my long block of code that I wanted to get into a String to work correctly in my AS code. Googling “actionscript heredoc” gave me nothing. Googling “actionscript multi-line string” gave me nothing. The golden nugget was finally realizing that AS3 is basically Javascript 2, so maybe some JS guys had figured this one out. Turns out they did. Here’s the JS nugget that led me to my solution.

You can use the same method in Actionscript 3. So your AS code would look like:

private var myString:String = ( <![CDATA[
				Here is my string
				that spans multiple
				lines.
				]]> ).toString();

It’s easier in MXML, in that case all you need to do is something like:

<mx:String id="myString">
	<![CDATA[
		Here is my string
		that spans multiple
		lines.
	]]>
</mx:String>

So that’s how you simulate the heredoc syntax in AS3.

And a quick note: when I was trying to figure this out I was making a class that injected Javascript code into the HTML wrapper for a component to execute. See Abdul Qabiz’s post here for a description of this method and a utility class to help you do this. Basically this let me write a component that used both Actionscript and Javascript and I could keep all my AS code and my JS code within the Actionscript component (as opposed to putting in the HTML wrapper). That’s pretty frickin cool.

22 Responses to “Multi-line strings in Actionscript 3”

  1. Igor Costa says:

    You’d better say

    public var doug:String = ( ”). toString();

    The current doesn’t work.

  2. Doug says:

    woops, looks like wordpress stripped out the cdata part of your comment, email me and I’ll edit the comment and get the right code in there

  3. Igor Costa says:

    Dont’ have your e-mail. you have mine it’s more easy.

  4. Ben says:

    Its funny how the timing of posts happens sometimes. I’ve actually been working with the same basic thing the past few days, embedding a large chunk of VBA code in my Flex app (don’t ask), and went with an XML var defined in MXML. I think I like this approach better though, so thanks!

    PS – I’ll be attending your talk at 360|Flex, I’m looking forward to it.

  5. Joan Garnet says:

    It works without quotation for me.
    It’s nice to do such thing:

    var injectScript:String =
    (
    <![CDATA[
    var e = document.createElement("script");
    void(e.setAttribute("src","MiScript.js"));
    void(e.setAttribute("type","text/javascript"));
    void(document.getElementsByTagName("head")[0].appendChild(e));
    ]]>
    ).toString();
    ExternalInterface.call( “function(){”+injectScript+”}” );

    I also hate modifying the html template…
    Thanx for sharing!
    ;-)

  6. judah says:

    Nice!!! I was abusing the textarea like so:

    <mx:TextArea id=”textarea1″ visible=”false”>
    <mx:text>
    <![CDATA[some stuff]]>
    </mx:text>
    </mx:TextArea>

    Having figured out the way to do heredoc style would you also know how to wrap a cdata tag around content in an xmlnode? For example:

    myXML.someNode = “<![CDATA[" + someXHMTL + "]]>”;

  7. Abdul Qabiz says:

    Cool! I would try to use this technique in my JavaScript Inject code. I used some regex to strip/replace characters there..

    Thanks for sharing..

    -abdul

  8. Tanuvan says:

    This is EXACTLY what I have been looking for!!! Thank you for posting !

  9. Nur says:

    I know this works in AS2, haven’t tried it in AS3:

    myString = “This is line number one.”
    myString += “\n”
    myString += “This is the next line.”

    Great code though from the looks of it. Haven’t had a chance to try it out, but thanks for sharing.

  10. judah says:

    @Joan – Does that method work in major browsers? What is the point of the void() call?

  11. Mark says:

    Thanks Judah for getting me here. This looks promising but when I paste in Joan’s code snippet, I get parse error messages.

  12. judah says:

    I get those errors too but for all the code on this page. I’m using FB3 and I’m wondering if this will not work in it?

    private var injectScript:String = String( ).toString();

    Parse error at ‘ ).toString();\n\t\t’. FlashTerbate FlashTerbate.mxml line 21

  13. naisioxerloro says:

    Hi.
    Good design, who make it?

  14. Idetrorce says:

    very interesting, but I don’t agree with you
    Idetrorce

  15. Jordan Brough says:

    Thanks a ton! I was cringing every time I had to do this before.

  16. Tim says:

    Nice solution Doug!
    It’s a great technique when you’re wanting to dump a single block of HTML text into a Textfield and you don’t feel it justifies the use of its own XML file.

  17. Jason says:

    This has probably long been figured out, but I was looking for a solution to posting xhtml data from flex to a blog using XMLRPC. When I tried this solution, I was also getting the parse errors whenever I append the CDATA end tag as one string. So what I ended up having to do was:

    myBlogPost:String = ”;

    The parse errors stopped.

  18. Jason says:

    Um, oops, that would be:

    myBlogPost: String = ‘[envision the C-DATA start tag here]‘ + blogBody.xhtmlText + ‘]’ +’]’ + ‘>’;

  19. [ fla:pik ] Blog » Multi-line strings in Actionscript 3 says:

    [...] Full Article [...]

  20. Vefhýsing says:

    Excellent!!! Thanks for sharing.

  21. DouG Molidor says:

    one DouG to another, thanks!
    Great little tip. Cheers for going through the trouble of finding and publishing for us.

  22. Damilola "Chayz" Jegede says:

    Hey Ya’ll. Very good stuff. My own area is trying to embed CSS in an as file. Wierd huh? Thanks Doug!

Leave a Reply