Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder MrEnder's Avatar
    Join Date
    Jul 2008
    Location
    Canada Ontario
    Posts
    258
    Thanks
    18
    Thanked 4 Times in 4 Posts

    Question Writing question

    Ok in java if I want to print to prompt I type System.out.print in javascript I write document.write Ok but if I document.write it resets things. I can only do it once... what do i use instead of document.write so it wont reset my program?

    thats most likely a really noob question but I can't seem to figure it out how to get around that...

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,898
    Thanks
    203
    Thanked 2,530 Times in 2,508 Posts
    The document.write command must be carried out during the loading of the page. So, if it is attached to any event that executes after the page has loaded, then the whole page will be replaced with the contents of the document.write command, as you have found.

    The best way is to create a <div> with an id, and then use

    var newtext = "Whatever you want";
    document.getElementById("yourDivId").innerHTML = newtext;


    At least once per year, some group of scientists will become very excited and announce that whatever they announced last year about global warming is wrong.

  • #3
    Regular Coder MrEnder's Avatar
    Join Date
    Jul 2008
    Location
    Canada Ontario
    Posts
    258
    Thanks
    18
    Thanked 4 Times in 4 Posts
    Ok what did I do wrong?

    Code:
    //<![CDATA[
    
    var date = new Date();
    var input = "";
    var inputCheck = false;
    var menu = "";
    var br = "<br />";
    
    var rpsP1 = "";
    var rpsAI = "";
    
    var menuComment1 = "1: Rock Papper Scissors" + br + "2: Craps" + br + br + "Type exit to quit";
    
    var rpsComment1 = br + br + "Welcome to Rock Papper Scissors";
    var rpsComment2 = br + "Type 1 for Rock, 2 for Scissors and 3 for Paper"
    
    function getInput(input) {
    
    	input = ""
    
    	do {
    		
    		inputCheck = false;
    
    		try {
    
    			input = prompt("Please enter in the info.","");
    
    		}
    		catch(ex) {
    
    			inputCheck = true;
    			alert("Bad input");
    
    		}
    
    	}while(inputCheck==true);
    
    	return input;
    
    }
    
    document.getElementById(mainDiv).innerHTML = menuComment1;
    
    menu = getInput();
    
    if(menu=="1") {
    
    	document.getElementById(mainDiv).innerHTML = rpsComment1;
    
    	while(true) {
    
    		document.getElementById(mainDiv).innerHTML = rpsComment2;
    		
    
    		rpsAI = Math.random(Math.floor() *3);
    		rpsP1 = getInput();
    
    		break;
    		
    
    	}
    
    }
    
    
    
    //]]>


  •  

    Posting Permissions

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