SitePoint Sponsor

User Tag List

Results 1 to 6 of 6

Hybrid View

  1. #1
    Currently Occupied; Till Sunda Andrew-J2000's Avatar
    Join Date
    Aug 2001
    Location
    London
    Posts
    2,475
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Java - document.write('')

    How can i just get document.write('') to add to the document and not overwrite the whole code?

    cheers

    <html><head><title></title>
    <script>
    function table(a)
    {
    var a;

    switch(a)
    {
    case 1 : document.write("1"); break;
    case 2 : document.write("2"); break;
    case 3 : document.write("3"); break;
    case 4 : document.write("4"); break;
    case 5 : document.write("5"); break;
    }
    }
    </script>
    <head><body>

    <a href = "javascript:void(0)" onclick="table(1)">First</a></font>
    <a href = "javascript:void(0)" onclick="table(2)">Second</a></font>
    <a href = "javascript:void(0)" onclick="table(3)">Third</a></font>
    <a href = "javascript:void(0)" onclick="table(4)">Fourth</a></font>
    <a href = "javascript:void(0)" onclick="table(5)">Fifth</a></font>

    </body><html>

  2. #2
    What? Maelstrom's Avatar
    Join Date
    Oct 2001
    Location
    Whistler BC originally from Guelph Ontario
    Posts
    2,175
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    First off. I would like to say Java and Javascript ar distinctly different and you are refering to Javascript.That done here is your answer.

    You can't document.write will always replace you screen. There are some small fixes but they are not browser compatible and may work in some but not all browsers. Here is my prefered way of dealing your situation.

    Code:
    <div id="menu1" style="display:none">
        <h1>this is a test</h1>
    </div>
    That is one of the items you want to change the area to

    Code:
    <body>
    blahblahblahblahblahblah
    <div id='textareatochange'>
        <p>This is the original text that will change when this code is done</p>
    </div>
    It depends on how you want to change thing. since you want a link here you go

    Code:
        <a href="javascript:void(0)" onclick="changeText('menu1')">
    In your head have a function that looks like this

    Code:
    function changeText(menuitem){
       document.getElementById('textareatochange').innerHTML=document.getElementById(menuitem).innerHTML
    }
    Hopefully that makes sense but essentilly what you are doing is taking the html of the original and changing it to the html in the hidden layer. This way it has alreayd been written to the page but is interchangeable. This will only work on Ie5.0 + and Netscape 6.0 andall newer browsers. Not NN4.7.

    This is rough and I may have missed a thing or two but the idea is there I hope it helps
    Maelstrom Personal - Apparition Visions
    Development - PhP || Mysql || Zend || Devshed
    Unix - FreeBSD || FreeBsdForums || Man Pages
    They made me a sitepoint Mentor - Feel free to PM me or Email me and I will see if I can help.

  3. #3
    Currently Occupied; Till Sunda Andrew-J2000's Avatar
    Join Date
    Aug 2001
    Location
    London
    Posts
    2,475
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Sry about the title

    I was trying to send the code to a blank window, in the mean time, but have been having problems sending the script due to "</script>". Can I do this without putting the javascript in a .js file.

    <script>
    function newwin(num)
    {
    newWindow = window.open("","dyn","")
    newWindow.document.write(

    "<html><head><title>Lexi</title>"+ "<script>function Lexi_table(a){"+
    "var a="+num+" + ;"+ "switch(a){"+ "case 1 : document.write(\"1\"); break;"+ "case 2 : document.write(\"2\"); break;"+ "case 3 : document.write(\"3\"); break;"+ "case 4 : document.write(\"4\"); break;"+ "case 5 : document.write(\"5\"); break;}}"+
    "</script><head>"+
    "<body></body></html>");
    <script>
    <body>
    <a href = "javascript:void(0)" onclick="newwin()">Win</a></font>
    </body>

    Cheers

    Andrew

  4. #4
    SitePoint Guru Vincent Puglia's Avatar
    Join Date
    Feb 2000
    Location
    where the World once stood
    Posts
    700
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Andrew,

    "</script>" is an extremely bothersome tag to document.write; I have seen a number of workarounds, but the one I use is to split the tag to two lines, something like:

    txt += "</s"
    txt += "cript>"

    You can also try escaping the "/", as in: "<\/script>"

    Re your original question: if you want to add NN4.7 compatibility to Maelstrom's example, see the "dHTML: an introduction" script/tutorial at my site.

    Vinny
    Where the World Once Stood
    the blades of grass
    cut me still

  5. #5
    Currently Occupied; Till Sunda Andrew-J2000's Avatar
    Join Date
    Aug 2001
    Location
    London
    Posts
    2,475
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Cheers

    I tried <\/script>, but the browser reads it still and tries to exit the script.

    But splitting it up works nicely thanks

  6. #6
    SitePoint Guru Vincent Puglia's Avatar
    Join Date
    Feb 2000
    Location
    where the World once stood
    Posts
    700
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Andrew,

    [quote]But splitting it up works nicely thanks[\quote]

    guess that's why it's my preferred method -- you're welcome

    Vinny
    Where the World Once Stood
    the blades of grass
    cut me still


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
  •