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 8 of 8
  1. #1
    New Coder
    Join Date
    Feb 2012
    Location
    England
    Posts
    59
    Thanks
    12
    Thanked 2 Times in 2 Posts

    Question Encountered problem with the "function" command!

    Ok so i was trying to get a script to run on button click, when i clicked the button the code ran but i a white screened tab. Additinal info - when I ran my script on page load it worked perfectly.

    Here is the ccode i'm trying to run
    Code:
    <script type = "text/javascript">
    function runMyJavascript_Caught() 
    {
    // get user to add name
    var name_entry = prompt ("Please Enter your Name!", "James");
    
    // declare some short strings
    
    var stating = ", I knew you did it!";
    var told = "I told you that I would find out";
    var mistake = "I guess you just made a mistake entering your name, ";
    var punish = "Now go back to your room";
    var heading = "OI Stop";
    
    // construct some longer strings
    
    var stating_name = name_entry + stating;
    var mistake_name = mistake + name_entry;
    
    document.writeln(heading.bold() + "<br>");
    document.writeln(stating_name + "<br>");
    document.writeln(told + "<br>");
    document.writeln(mistake_name + "<br>");
    document.writeln(punish + "<br>");
    }
    </script>
    Last edited by MancunianMacca; 03-01-2012 at 10:52 PM.

  • #2
    New Coder
    Join Date
    Feb 2012
    Location
    England
    Posts
    59
    Thanks
    12
    Thanked 2 Times in 2 Posts
    I use this to activate it
    Code:
     <input type = "button" onclick = "runMyJavascript_Caught()" value = "Here">

  • #3
    New Coder
    Join Date
    Feb 2012
    Location
    England
    Posts
    59
    Thanks
    12
    Thanked 2 Times in 2 Posts
    Just realised why... The document.write command can't be used once page has loded... Ok now I need a new thread.

  • #4
    Senior Coder ckeyrouz's Avatar
    Join Date
    Jun 2009
    Location
    Montreal, Canada
    Posts
    1,044
    Thanks
    5
    Thanked 179 Times in 179 Posts
    Seems to be working fine.

    Can you please explain what is the output you want?
    Software and cathedrals are much the same - first we build them, then we pray.

  • #5
    New Coder
    Join Date
    Feb 2012
    Location
    England
    Posts
    59
    Thanks
    12
    Thanked 2 Times in 2 Posts
    Quote Originally Posted by ckeyrouz View Post
    Seems to be working fine.

    Can you please explain what is the output you want?
    I wanted to click a button and have that code write to the same page the button was on.

  • #6
    Senior Coder ckeyrouz's Avatar
    Join Date
    Jun 2009
    Location
    Montreal, Canada
    Posts
    1,044
    Thanks
    5
    Thanked 179 Times in 179 Posts
    Add the result in a div as follows:

    Code:
    <html>
    	<head>
    		<script type = "text/javascript">
    			function runMyJavascript_Caught() 
    			{
    				// get user to add name
    				var name_entry = prompt ("Please Enter your Name!", "James");
    				
    				// declare some short strings
    				
    				var stating = ", I knew you did it!";
    				var told = "I told you that I would find out";
    				var mistake = "I guess you just made a mistake entering your name, ";
    				var punish = "Now go back to your room";
    				var heading = "OI Stop";
    				
    				// construct some longer strings
    				
    				var stating_name = name_entry + stating;
    				var mistake_name = mistake + name_entry;
    				
    				var result = heading.bold() + "<br>"
    				  		   + stating_name + "<br>"
    				  		   + told + "<br>"
    				  		   + mistake_name + "<br>"
    				  		   + punish + "<br>";
    				
    				document.getElementById("result").innerHTML = result;
    				//document.writeln(heading.bold() + "<br>");
    				//document.writeln(stating_name + "<br>");
    				//document.writeln(told + "<br>");
    				//document.writeln(mistake_name + "<br>");
    				//document.writeln(punish + "<br>");
    			}
    		</script>
    	</head>
    	<body>
    		 <input type = "button" onclick = "runMyJavascript_Caught()" value = "Here">
    		 <div id="result"></div>
    	</body>
    </html>
    Software and cathedrals are much the same - first we build them, then we pray.

  • Users who have thanked ckeyrouz for this post:

    MancunianMacca (03-01-2012)

  • #7
    New Coder
    Join Date
    Feb 2012
    Location
    England
    Posts
    59
    Thanks
    12
    Thanked 2 Times in 2 Posts
    Quote Originally Posted by ckeyrouz View Post
    Add the result in a div as follows:

    Code:
    <html>
    	<head>
    		<script type = "text/javascript">
    			function runMyJavascript_Caught() 
    			{
    				// get user to add name
    				var name_entry = prompt ("Please Enter your Name!", "James");
    				
    				// declare some short strings
    				
    				var stating = ", I knew you did it!";
    				var told = "I told you that I would find out";
    				var mistake = "I guess you just made a mistake entering your name, ";
    				var punish = "Now go back to your room";
    				var heading = "OI Stop";
    				
    				// construct some longer strings
    				
    				var stating_name = name_entry + stating;
    				var mistake_name = mistake + name_entry;
    				
    				var result = heading.bold() + "<br>"
    				  		   + stating_name + "<br>"
    				  		   + told + "<br>"
    				  		   + mistake_name + "<br>"
    				  		   + punish + "<br>";
    				
    				document.getElementById("result").innerHTML = result;
    				//document.writeln(heading.bold() + "<br>");
    				//document.writeln(stating_name + "<br>");
    				//document.writeln(told + "<br>");
    				//document.writeln(mistake_name + "<br>");
    				//document.writeln(punish + "<br>");
    			}
    		</script>
    	</head>
    	<body>
    		 <input type = "button" onclick = "runMyJavascript_Caught()" value = "Here">
    		 <div id="result"></div>
    	</body>
    </html>
    It works, thanks man. Your great

  • #8
    Senior Coder ckeyrouz's Avatar
    Join Date
    Jun 2009
    Location
    Montreal, Canada
    Posts
    1,044
    Thanks
    5
    Thanked 179 Times in 179 Posts
    Most welcome. Glad it worked for you.
    Software and cathedrals are much the same - first we build them, then we pray.


  •  

    Posting Permissions

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