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 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Mar 2009
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    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]

  • #2
    Regular Coder
    Join Date
    Mar 2009
    Location
    Portland Oregon
    Posts
    690
    Thanks
    44
    Thanked 63 Times in 62 Posts
    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>

  • #3
    New to the CF scene
    Join Date
    Mar 2009
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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.

  • #4
    Regular Coder
    Join Date
    Mar 2009
    Location
    Portland Oregon
    Posts
    690
    Thanks
    44
    Thanked 63 Times in 62 Posts
    check your prompt syntax

  • #5
    New to the CF scene
    Join Date
    Mar 2009
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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?


  •  

    Tags for this Thread

    Posting Permissions

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