Web Analytics Made Easy -
StatCounter Creating Quotes with javascript - CodingForum

Announcement

Collapse
No announcement yet.

Creating Quotes with javascript

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Creating Quotes with javascript

    Hey all another day and another question... Well actually quite a few days... anyway I've got a question. I'm writing HTML code into a varaible and I want to beable such as:
    function name()
    {
    myvar="<img src= "images source">"
    }

    As you can see the quote to tell the src, messes up the storing of the variable. Is there a way I can store this into my variable?

    If anyone cares the reason i'm doing this is so that I can have a page with document.write(myvar) and the page is self generating depending on user input.

  • #2
    You just have to escape the quotes with a backslash like this:
    Code:
    function name()
    {
        myvar="<img src=\"images source\">";
    }
    I added a semicolon to the end also. It's a good practice to always do that even when you can get away without it.
    ScriptingMagic.com

    Comment


    • #3
      Thanks so very much!!!


      Sorry... spoke too soon . It didn't work. Anyone know how to get it to work?
      Last edited by questionable; Aug 2, 2005, 06:45 PM. Reason: too much excitement... and didn't work

      Comment


      • #4
        I guess I should have looked a little closer at the code. You're not doing anything with the variable after assigning it. For that particular function you can do away with the variable altogether.
        Code:
        function name()
        {
            return "<img src=\"images source\">";
        }
        ScriptingMagic.com

        Comment


        • #5
          Actually in this case (I'm sure this is merely a simple example) it would be more proper to do as such:

          Code:
          function name()
          {
             var myvar='<img src= "images source">';
             return myvar;
          };
          Javascript treats single and double quotes pretty much the same, you just can't have single inside single or visa versa, that's when they need to be escaped. When dynamically writing html to the page it's best to encompass the string in single quotes, since proper html uses double quotes.

          Basscyst
          Helping to build a bigger box. - Adam Matthews

          Comment


          • #6
            Thank you very much I'll give it a try

            Comment

            Working...
            X