Results 1 to 7 of 7

Thread: "&"

  1. #1
    Join Date
    Feb 2007
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default "&"

    Hi all,

    I am trying to write a php code which outputs javascript code like this:
    Code:
    var a="b&c"
    but finally, the output source code is
    Code:
    var a="b&c"
    My question is: Is there a javascript function read a string containing "&" as "&" like function htmlspecialchars_decode in php?

    Thanks,

    KC.

  2. #2
    Join Date
    Feb 2007
    Location
    England
    Posts
    254
    Thanks
    0
    Thanked 5 Times in 5 Posts

    Default Yes,

    Its not a built in function, but here is one.
    http://javascript.crockford.com/remedial.html
    See String Methods >> entityify()


  3. #3
    Join Date
    Feb 2007
    Location
    England
    Posts
    254
    Thanks
    0
    Thanked 5 Times in 5 Posts

    Default Whoops

    Ok.

    Just add this at the top of your script and call it with

    var a = "b & c".deentityify()


    Code:
    String.prototype.deentityify = function () 
      {
        return this.replace(/&amp;/g,"&").replace(/&lt;/g,"<").replace(/&gt;/g,">");
    }
    Last edited by Bob90; 03-28-2007 at 03:22 PM. Reason: Missing }

  4. #4
    Join Date
    Feb 2007
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Hi Bob90,

    Thanks. But I think replacing method does not work here, as every "&" will be translated to "&amp;" in html source code:

    Code:
    var a = "b &amp; c".deentityify()
    
    String.prototype.deentityify = function () 
      {
        return this.replace(/&amp;/g,"&").replace(/&lt;/g,"<").replace(/&gt;/g,">");
      }
    becomes:

    Code:
    var a = "b &amp;amp; c".deentityify()
    
    String.prototype.deentityify = function () 
      {
        return this.replace(/&amp;amp;/g,"&amp;").replace(/&amp;lt;/g,"<").replace(/&amp;gt;/g,">");
      }
    Am I right?

    KC.

  5. #5
    Join Date
    Feb 2007
    Location
    England
    Posts
    254
    Thanks
    0
    Thanked 5 Times in 5 Posts

    Default Umm

    Check out my second post. Made my own.

  6. #6
    Join Date
    Feb 2007
    Location
    England
    Posts
    254
    Thanks
    0
    Thanked 5 Times in 5 Posts

    Default

    OK i see now. Well that has to be PHP then as I can't think of any code.

    Unless you use unescape() and instead of &amp; you use %26?

    But probably not useful.

  7. #7
    Join Date
    Feb 2007
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Hi Bob90,

    Noop, it really helps. This is what I finally came up with:

    Code:
    var a = 'b' + unescape('&#37;26') + 'c';
    There is no "&" in the code and the code works just fine. Thanks a lot.

    KC.

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
  •