SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Enthusiast
    Join Date
    Oct 2004
    Location
    Mexico
    Posts
    58
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Newbie question about Javascript

    Stupid newbie question on Javascript, how can I..

    print a variable within an html code. I have seen the tutorials at w3schools but all the examples use document.write() which content is printed out without any explicit sentence on the HTML where it should be the output.


    So for example if I have something like:

    var myvar= "Hello World"

    then on the html

    <p> This is my message to the world: $myvar .....
    Alexandro Colorado

  2. #2
    SitePoint Member
    Join Date
    Jul 2007
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
    "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
    <meta http-equiv="Content-type" content="text/html; charset=utf-8">
    <title>gg</title>
    </head>
    <script type="text/javascript" charset="utf-8">
    var foo = "World";
    </script>
    <body>
    Hello <script>document.write(foo)</script>
    </body>
    </html>

  3. #3
    SitePoint Enthusiast
    Join Date
    Oct 2004
    Location
    Mexico
    Posts
    58
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks, is there a way to have script as an argument. I think that's a different method on plugging javascript method like <p script="...." >

    Am I wrong?
    Alexandro Colorado

  4. #4
    SitePoint Member
    Join Date
    Jul 2007
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just another newbie... so I might not be giving the answer you were looking for.
    I am unaware of any <p script="..."> functionality. I believe if you want to pass a variable to html then you have to do that whole:

    <script type="text/javascript" charset="utf-8">
    var foo = "World";
    </script>

    but there is DOM scripting that would allow you to set an "id" as a hook and then create element and textnodes.

    Code JavaScript:
    function hello() {
            var paragraph = document.createElement("p");
            var txt = document.createTextNode("Hello World");
            paragraph.appendChild(txt);
            var test = document.getElementById("test");
            test.appendChild(paragraph);
    }

    then in your html

    <html>
    <body onload=hello();>
    <div id="test"></div>
    </body>
    </html>

    This should result in the "Hello World" being appended into you generated html. It's really not the same as passing a variable, but maybe it helps.


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
  •