SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Zealot
    Join Date
    Jan 2005
    Location
    md
    Posts
    180
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Write out html from javascript

    What is the best way to embed a js file on a site and have it write out HTML... Is there a way that document.write could write out any generic html, or is there a way to escape it so that i don't have to use ticks, etc.

  2. #2
    SitePoint Member
    Join Date
    Nov 2004
    Location
    compton
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I would avoid JavaScript for doing any mission-critical stuff as not all browsers support it ( for example text browsers ) or have it disabled.
    Better to use a server-side technology to generate your common markup using something like "includes" or templating modules depending on the language.

    UNIX-based languages can make use of "heredocs" this avoids having to over-use quoting.
    Perl has functions for creating quotes - qq, q and qw - and so you can keep your literal quotes for markup.

  3. #3
    SitePoint Zealot
    Join Date
    Jan 2005
    Location
    md
    Posts
    180
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    For what I'll be doing, it will have to be JavaScript. It is a service that needs to generate generic code that can be added into any page to output HTML, which will vary from client to client.

    Is there set rules that javascript cannot print out? I know that I will have to have a backslash before any quotes, but asside from that, is there anything I should worry about?

    Quote Originally Posted by Puffin the Erb
    I would avoid JavaScript for doing any mission-critical stuff as not all browsers support it ( for example text browsers ) or have it disabled.
    Better to use a server-side technology to generate your common markup using something like "includes" or templating modules depending on the language.

    UNIX-based languages can make use of "heredocs" this avoids having to over-use quoting.
    Perl has functions for creating quotes - qq, q and qw - and so you can keep your literal quotes for markup.

  4. #4
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,861
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    It is best to avoid document.write as that is invalid with XHTML and only works in HTML when run before the page finishes loading. The better way of updating the page using Javascript is to use innerHTML and the best way is to use the DOM methods.

    All quotes and slashes will need to be escaped in any text that you output to your page from Javascript.

    Also remember that anything written by Javascript will not be visible to 10% of your visitors so the page needs to be usable without it.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">


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
  •