SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Enthusiast
    Join Date
    Oct 2006
    Posts
    88
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    handling HTML strings with JavaScript

    I Have built a system that uses a simple templating system to separate the logic from layout, but after I have populated my templates with data on the server I want to send this to javascript on the client so that I can dynamically add the templates and content to the page.

    I am having some issues handling HTML in javascript. I tried serializing in PHP and then unserializing in javascript with no luck. If I use json_encode() PHP function this gives me errors. My PHP return is just a simple arraywith HTML as one element.

    I have also been looking at JavaScript templating systems as I like the idea of using the server just to process data leaving the client to process layout and templates. But is there a way to copy HTML in JavaScript.

    Any help or advice appreciated.

  2. #2
    SitePoint Guru
    Join Date
    Jan 2007
    Posts
    934
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    I've had luck with this using innerHTML to insert the html and mysql_escape_string() to prevent double and single quotes from causing a problem.

  3. #3
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    mysql_escape_string() is not a suitable way to prepare a javascript string. json_encode() is perfect though.

  4. #4
    SitePoint Enthusiast
    Join Date
    Oct 2006
    Posts
    88
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I am having issues using json_encode(). Actually, I have opted to just generate json from a php array and send this to the browser and once javascript has picked this up implement a javascript based templating system. Problem now is how do I pull an external HTML template file into javascript as an object?

    I have been looking at the jquery clone() method.


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
  •