Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    Senior Coder jerry62704's Avatar
    Join Date
    Oct 2007
    Location
    Springfield, IL
    Posts
    1,100
    Thanks
    13
    Thanked 87 Times in 87 Posts

    Question Get code in javascript variable

    I want to put some code into javascript variables so that I can then insert them into the html page. Since the code includes quotes (") and the backslash (\) I figured to encase it in a protector. var x = '![CDATA[ ... ]]';

    That didn't work. Any ideas? Was the cdata the proper way to think of it?
    .
    .
    ...and gladly would he learn and gladly teach

    Visit www.LiberalsWin.com for humor and the unique Bush/Obama Approval Polls

  • #2
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,633
    Thanks
    0
    Thanked 649 Times in 639 Posts
    Code quotes as \" and backslashes as \\

    Alternatively take a look at http://javascriptexample.net/extobjects93.php
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #3
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,301
    Thanks
    10
    Thanked 586 Times in 567 Posts
    use JSON.stringify() to balance all the toothpicks for you and give you back a double-quoted (") string literal.
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/5/28) IE7:0.1, IE8:5.3, IE11:8.4, IE9:3.2, IE10:3.2, FF:18.2, CH:46, SF:7.9, NON-MOUSE:32%

  • #4
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,253
    Thanks
    12
    Thanked 341 Times in 337 Posts
    Quote Originally Posted by jerry62704 View Post
    I figured to encase it in a protector. var x = '![CDATA[ ... ]]';
    <![CDATA[ ... ]]> only makes sense in an SGML/XML context, wich JS definitely is not. so adding that into JS code only can make it worse.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #5
    Senior Coder jerry62704's Avatar
    Join Date
    Oct 2007
    Location
    Springfield, IL
    Posts
    1,100
    Thanks
    13
    Thanked 87 Times in 87 Posts
    Thanks all. I was hoping CDATA would help, but it didn't. Good to see the reason why. Remember when we use to hide all our code in it?

    I think I would have to install some libraries to use JSON, but it does sound like the more elegant solution. Until I figure that out I'll have to go with escaping all '"', "'", "\" characters.
    .
    .
    ...and gladly would he learn and gladly teach

    Visit www.LiberalsWin.com for humor and the unique Bush/Obama Approval Polls

  • #6
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,301
    Thanks
    10
    Thanked 586 Times in 567 Posts
    Quote Originally Posted by jerry62704 View Post
    I think I would have to install some libraries to use JSON, but it does sound like the more elegant solution. Until I figure that out I'll have to go with escaping all '"', "'", "\" characters.
    there's nothing to install, you just use it. unless you 're in IE7, but even then you can just link a script tag to json2.js. it takes less time to re-code to use JSON than one toothpick re-balance does, and you'll never have to do it again.

    see http://en.wikipedia.org/wiki/Leaning_toothpick_syndrome for more info
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/5/28) IE7:0.1, IE8:5.3, IE11:8.4, IE9:3.2, IE10:3.2, FF:18.2, CH:46, SF:7.9, NON-MOUSE:32%

  • #7
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,253
    Thanks
    12
    Thanked 341 Times in 337 Posts
    Quote Originally Posted by jerry62704 View Post
    I think I would have to install some libraries to use JSON
    all modern browsers can use JSON natively.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #8
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,301
    Thanks
    10
    Thanked 586 Times in 567 Posts
    Quote Originally Posted by jerry62704 View Post
    I think I would have to install some libraries to use JSON, but it does sound like the more elegant solution. Until I figure that out I'll have to go with escaping all '"', "'", "\" characters.
    heads up:
    you can escape text blocks automatically using my code formatter, http://danml.com.slim/

    you can paste a chunk of html/code into the IN box, and generate a valid JS string on the right-side OUT box.

    there are actually several engines available in the app that will do this:
    • JSON.stringify (always ")
    • JS String (smaller choice between ' and " end caps)
    • Slim (compressed into a formula that spits out a string)
    • Zip Input Into Script (deflates the string, wraps in inflate() call)
    • or even DataURL, to pseudo-externalize the content. (use <script src=data....)
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/5/28) IE7:0.1, IE8:5.3, IE11:8.4, IE9:3.2, IE10:3.2, FF:18.2, CH:46, SF:7.9, NON-MOUSE:32%


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •