SitePoint Sponsor

User Tag List

Results 1 to 9 of 9

Hybrid View

  1. #1
    SitePoint Addict
    Join Date
    Jan 2001
    Location
    Ottawa ON
    Posts
    315
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Javascript: wholesale escape a large number of characters from a string

    Is there a way in Javascript to wholesale escape a large number of characters from a string?

    In PHP, one can use the following Syntax:
    $var = <<<ENDB
    Content with any character I want - anything goes
    ENDB;

    That creates a variable with its contents being *anything and everything* that's between the ENDB's - including doublequotes (") and other characters that would normally have to be individually escaped.

    Is there any equivalent in Javascript for including a large amount of text with characters that would normally have to be escaped?

  2. #2
    You talkin to me? Anarchos's Avatar
    Join Date
    Oct 2000
    Location
    Austin, TX
    Posts
    1,438
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No
    ck :: bringing chris to the masses.

  3. #3
    SitePoint Addict
    Join Date
    Jan 2001
    Location
    Ottawa ON
    Posts
    315
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the fast reply! But I'm not very thankful for the answer

    Methinks this would be useful - the creators of PHP saw a need for it, and I can see needs for it on the client side. Alas, it is not to be ...

  4. #4
    JavaScript Guru (Big Ego) Arielladog's Avatar
    Join Date
    Jul 1999
    Location
    SC, USA
    Posts
    390
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The creators of PHP created PHP with newbies in mind. JavaScript was created more for the intermediate programmer. And Java is more for the strict programmer. So, JavaScript is more strict than PHP and less strict than Java, falling in the middle. And this is why doesn't have those easy shortcuts (also, it was primarily created before anyone thought of that)

    aDog

  5. #5
    You talkin to me? Anarchos's Avatar
    Join Date
    Oct 2000
    Location
    Austin, TX
    Posts
    1,438
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No, PERL originated this structure a good 15 years ago, which was quite a while before Javascript. Javascript is less strict than PHP: it requires no semicolons or variable identifiers like PHP, variables are assumed to be global by default. Internet Explorer will even search several objects for methods when no object is specified. For example: if you do <input type="text" onfocus="blah()" /> IE will search the current form element, the window object, and the document object automatically, whereas PHP would require the object to be specified.
    ck :: bringing chris to the masses.

  6. #6
    JavaScript Guru (Big Ego) Arielladog's Avatar
    Join Date
    Jul 1999
    Location
    SC, USA
    Posts
    390
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You're got me there Anarchos, Perl was the first to do something like that. Though, I tend to disagree that PHP is stricter than JavaScript, I think what else you said was right.

    aDog

  7. #7
    SitePoint Addict
    Join Date
    Jan 2001
    Location
    Ottawa ON
    Posts
    315
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If Javascript were to include the aforementioned fucntion, it would allow the language to be used as a form of 'client side includes' system whereby one could specify an external .js file to be inserted into the current document that would include content to be included in the current document. However, if I have to eliminate all double-quotes from my work or create special handlers to escape them I'll find a different solution.

  8. #8
    You talkin to me? Anarchos's Avatar
    Join Date
    Oct 2000
    Location
    Austin, TX
    Posts
    1,438
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It can already be used as such with document.write().
    ck :: bringing chris to the masses.

  9. #9
    SitePoint Addict
    Join Date
    Jan 2001
    Location
    Ottawa ON
    Posts
    315
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    How? This is what I'd like to do in one file:

    $var1 = "page title"
    $var2 = "content"
    $var3 = "more content"

    And then be able to write that those variables into the document in the appropriate places. How can I do that if I want to include a double-quote in the article for instace? (Without having to create a complicated system whereby text is scanned for the double quote and then a slash is inserted before it ... )


Bookmarks

Posting Permissions

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