Results 1 to 3 of 3

Thread: Mind stopped working

  1. #1
    Join Date
    Jan 2006
    Location
    At home
    Posts
    72
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Mind stopped working

    I am trying to add a script one of my page of my site but a error occouring while doing it. Actully i want to insert a prompt box before loading the page. I want the user insert his/her name in the prompt box and want to insert that name in the html coading of page. but as soon as i enter some name in the prompt box all the formatting of the page disappers like bg color and text color and every thing. here is my code
    <html>
    <head>
    <script type="text/javascript">
    function vaibhav()
    {
    var name=prompt("Please enter your name","Harry Potter")
    if (name!=null && name!="")
    {
    document.write("Hello " + name + "! How are you today?")
    }
    }
    </script>
    </head>
    <body onload="vaibhav()" value="Call function" bgcolor="#000000" text="#FFFFFF">>


    </body>
    </html>

  2. #2
    Join Date
    Aug 2005
    Posts
    971
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    vaibhav24in: document.write() overwrites the whole page. Use <element>.innerHTML instead.

    Code:
    <html>
    <head>
    <script type="text/javascript">
    function vaibhav()
    {
    var name=prompt("Please enter your name","Harry Potter")
    if (name!=null && name!="")
    {
    document.getElementById("name").innerHTML = "Hello "+name+"!! How are you today??";
    }
    }
    </script>
    </head>
    <body onload="vaibhav()" value="Call function" bgcolor="#000000" text="#FFFFFF">
    <div id="name"></div>
    </body>
    </html>

  3. #3
    Join Date
    Jul 2006
    Location
    Canada
    Posts
    2,581
    Thanks
    13
    Thanked 28 Times in 28 Posts

    Default

    I would try to avoid the innerHTML method. Try the DOM way:
    Code:
    <html>
    <head>
    <script type="text/javascript">
    function vaibhav()
    {
    var name=prompt("Please enter your name","Harry Potter")
    if (name!=null && name!="")
    {
    var text = document.createTextNode("Hello "+name+"!! How are you today??")
    var tname = document.getElementById("name")
    while (tname.firstChild) {
    tname.removeChild.firstChild
    }
    tname.appendChild(text);
    }
    }
    </script>
    </head>
    <body onload="vaibhav()" value="Call function" bgcolor="#000000" text="#FFFFFF">
    <div id="name"></div>
    </body>
    </html>
    - Mike

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
  •