Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 03-15-2009, 06:28 AM   PM User | #1
David P.
New to the CF scene

 
Join Date: Mar 2009
Posts: 8
Thanks: 1
Thanked 0 Times in 0 Posts
David P. is an unknown quantity at this point
Question Need help passing value to global variable

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()?

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

if( answer ){
x.style.visibility = "visible";
}
else{
window.location.reload();
}
}

var answer;

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

document.getElementById("guest").innerHTML = 'Welcome ' + answer;
}
[CODE]
David P. is offline   Reply With Quote
Old 03-15-2009, 06:35 AM   PM User | #2
TinyScript
Regular Coder

 
Join Date: Mar 2009
Location: Portland Oregon
Posts: 690
Thanks: 44
Thanked 63 Times in 62 Posts
TinyScript is on a distinguished road
Code:
<html>
<head>
<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>';
}
</script>
</head>

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

</body></html>
TinyScript is offline   Reply With Quote
Old 03-15-2009, 06:44 AM   PM User | #3
David P.
New to the CF scene

 
Join Date: Mar 2009
Posts: 8
Thanks: 1
Thanked 0 Times in 0 Posts
David P. is an unknown quantity at this point
I can't. the 1st website has this css...
Code:
body{
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.
David P. is offline   Reply With Quote
Old 03-15-2009, 07:09 AM   PM User | #4
TinyScript
Regular Coder

 
Join Date: Mar 2009
Location: Portland Oregon
Posts: 690
Thanks: 44
Thanked 63 Times in 62 Posts
TinyScript is on a distinguished road
check your prompt syntax
TinyScript is offline   Reply With Quote
Old 03-15-2009, 07:23 AM   PM User | #5
David P.
New to the CF scene

 
Join Date: Mar 2009
Posts: 8
Thanks: 1
Thanked 0 Times in 0 Posts
David P. is an unknown quantity at this point
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?
David P. is offline   Reply With Quote
Reply

Bookmarks

Tags
functions, javascript, passing, values, variables

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 02:11 AM.


Advertisement
Log in to turn off these ads.