View Full Version : Need help passing value to global variable

David P.
03-15-2009, 06:28 AM
Please help me with my javascript problem. I've been stuck on this all day.

I have a site that calls the function welcome() and displays a prompt. The same site also has an iframe that calls the next function, add_name(), but when the pages are loaded it says that "answer" is undefined.

It says on the page, where the <p id="guest"></p> is, "Welcome undefined". How do I make it so that the value of answer, which is given onLoad of the 1st page with the prompt, is displayed as text in function add_name()?

function welcome(){
var x = document.getElementById("body");
answer = prompt("Welcome to my site. What's your name?");

if( answer ){
x.style.visibility = "visible";

var answer;

function add_name(){
var a = document.getElementById("guest");
a.style.color = "white";
a.style.fontSize = "21px";

document.getElementById("guest").innerHTML = 'Welcome ' + answer;

03-15-2009, 06:35 AM
<title>prompt test</title>
<script type="text/javascript">
function hw()
var n = prompt('What is your name?', 'Anonymous');
var hello = document.getElementById('hello');
hello.innerHTML = '<h1>Hello, ' + n + '!</h1>';

<body bgcolor="#FFFFFF">
<div id="hello">
Hi! <span style="text-decoration: underline; color: blue; cursor: pointer;" onClick="hw();">Click</span> to set your name.


David P.
03-15-2009, 06:44 AM
I can't. the 1st website has this css...

visibility: hidden;

Thats why in welcome() when you give a value, the css changes to 'visible', so you can see the site. The <p> is in the iframe, the second site, so the function can't call it because it doesn't exist there; it exists in the iframe. I made the visibility of the 1st site hidden, because i didn't want the viewer to see the javascript add his/her name to the site. I wanted it it so that when you add your name, visibility turns on, and the add_name() function makes it appear as if it was always there.

03-15-2009, 07:09 AM
check your prompt syntax

David P.
03-15-2009, 07:23 AM
The prompt works perfectly fine. I can do what I need to do, but I need to display two prompt which is really ugly. Why isn't the value being passed to the global variable, where, there, it can be grabbed by a separate function and displayed as text?