...

View Full Version : Resolved Encountered problem with the "function" command!



MancunianMacca
03-01-2012, 10:16 PM
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


<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>

MancunianMacca
03-01-2012, 10:41 PM
I use this to activate it
<input type = "button" onclick = "runMyJavascript_Caught()" value = "Here">

MancunianMacca
03-01-2012, 10:52 PM
Just realised why... The document.write command can't be used once page has loded... Ok now I need a new thread.

ckeyrouz
03-01-2012, 10:53 PM
Seems to be working fine.

Can you please explain what is the output you want?

MancunianMacca
03-01-2012, 10:56 PM
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.

ckeyrouz
03-01-2012, 11:01 PM
Add the result in a div as follows:



<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>

MancunianMacca
03-01-2012, 11:15 PM
Add the result in a div as follows:



<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 :D

ckeyrouz
03-02-2012, 04:07 PM
Most welcome. Glad it worked for you.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum