Results 1 to 3 of 3

Thread: [Resolved] JavaScript: document.write in a function

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jul 2005
    Location
    Canada
    Posts
    66

    Resolved [Resolved] JavaScript: document.write in a function

    Does anyone know why document.write hangs my browser with the code below. Commenting document.write out solves the problem.

    Thanks,

    <html>
    <head>
    <title>Functions</title>

    <script language="JavaScript"
    type="text/javascript">
    function addem(){ // function defined within <head> tags
    var n = 2;
    var y = 3;
    //alert("hi");
    document.write( n + y, "<br>");
    }
    </script>

    </head>
    <body bgcolor=red>

    <a href="javascript:addem()">Click here</a>

    <h2>Hello</h2>

    </body>
    </html>
    Last edited by whiteWay; Oct 29th, 2005 at 05:15 PM.

  2. #2
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: JavaScript: document.write in a function

    You can only call document.write while the page is loading. If you want to dynamically replace text you should create a div element with an ID and use document.getElementById() to access it and replace its content.
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jul 2005
    Location
    Canada
    Posts
    66

    Re: JavaScript: document.write in a function

    Thanks,

    I forgot that document.write can only be used when the page is loading. Using the div element helps to dynamically fill the info I want.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width