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
    Regular Coder
    Join Date
    Jan 2005
    Posts
    140
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question title of popup window to be equal to a variable

    I am creating an employee directory for a client and I want the employee's contact info to appear in a popup window when the employee's name is clicked on. The employee's contact info is taken from an external .js file. I am able to achieve this, but what I want is for the title of the popup page to be equal to a variable containing the employee's name. Here is my code:


    This page contains the list of the employees that can be clicked to display their contact info in a new window:
    <a href="javascript:showEmployee('Smith, J')">Smith, Joseph</a>


    This is the external .js page containing the functions to check which employee has be clicked and to display the info in a popup window:
    function showEmployee(Employee)
    {
    if(Employee == 'Smith, J')
    {
    var Name = "Joseph Smith";
    var Phone = "555-1234";
    var Email = "jsmith@spam.net";
    displayEmployee(Name,Phone,Email);
    }
    }

    function displayEmployee(Name,Phone,Email)
    {
    newwin = window.open('','','top=0,left=0,width=400,height=300');
    if (!newwin.opener) newwin.opener = self;
    with (newwin.document)
    {
    open();
    write('<html>');
    write('<head>');
    write('<title>EMPLOYEE INFORMATION</title>');
    write('</head>');
    write('<body>');
    write('<table>');
    write('<tr><td>' + Name + '</td></tr>');
    write('<tr><td>' + Phone + '</td></tr>');
    write('<tr><td>' + Email + '</td></tr>');
    write('</table>');
    write('</form>');
    write('</body>');
    write('</html>');
    close();
    }
    }

    The code above works fine. What I want is for the title of the popup page to be the employee's name not EMPLOYEE INFORMATION.

    I have tried using <body onLoad="document.title=Name;"> but I get an error saying Name is undefined. Why does it recognize the variable Name and display it in the body of the popup but not recognize it for the title of the page?

    Any help would be greatly appreciated.

    Thanks in advance.

  • #2
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,075
    Thanks
    0
    Thanked 256 Times in 252 Posts
    Change this:

    write('<title>EMPLOYEE INFORMATION</title>');

    to:

    write('<title>' + Name + '</title>');
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________

  • #3
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,075
    Thanks
    0
    Thanked 256 Times in 252 Posts
    I suggest you to take a look at this code to see how the data structure should be. Your structure makes it hard to add as many employees while mine is easier and more organized.
    Code:
    <html>
    <head>
    <script type="text/javascript">
    var employees = [];
    employees['Smith_Js'] = ["Joseph Smith", "555-1234", "jsmith@spam.net"];
    employees['Smith_Jn'] = ["John Smith", "555-4567", "johnsmith@spam.net"];
    employees['Jordan_M'] = ["Michael Jordan", "555-8901", "air@yahoo.com"];
    //add as many employees here...
    
    function showEmployee(key)
    {
      var emp = employees[key];
      if (emp){
        var newwin = window.open('','','top=0,left=0,width=400,height=300');
        var html = '<html>\n<head>\n<title>' + emp[0] + '</title>\n</head>\n<body>\n<table>\n' + 
          '<tr><td>' + emp[0] + '</td></tr>\n' +  //name
          '<tr><td>' + emp[1] + '</td></tr>\n' +  //phone
          '<tr><td>' + emp[2] + '</td></tr>\n' +  //email
          '</table>\n<form><input type="button" value="Close" onclick="window.close()" /></form>\n</body>\n</html>';
        newwin.document.write(html);
        newwin.document.close();
        newwin.focus();
      }
      else {
        alert('Employee not found.');
      }
      return false; //cancel href action
    }
    </script>
    </head>
    <body>
    <a href="#" onclick="return showEmployee('Smith_Js')">Smith, Joseph</a><br />
    <a href="#" onclick="return showEmployee('Smith_Jn')">Smith, John</a><br />
    <a href="#" onclick="return showEmployee('Jordan_M')">Jordan, Michael</a><br />
    </body>
    </html>
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________

  • #4
    Regular Coder
    Join Date
    Jan 2005
    Posts
    140
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Thumbs up

    Glenn you are the man, thanks, I realy appreciate it. I will take a look at your code to make it more organized as well.

  • #5
    Regular Coder
    Join Date
    Jan 2005
    Posts
    140
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question

    New question about this old script:

    Using glenngv's code:
    Code:
    <html>
    <head>
    <script type="text/javascript">
    var employees = [];
    employees['Smith_Js'] = ["Joseph Smith", "555-1234", "jsmith@spam.net"];
    employees['Smith_Jn'] = ["John Smith", "555-4567", "johnsmith@spam.net"];
    employees['Jordan_M'] = ["Michael Jordan", "555-8901", "air@yahoo.com"];
    //add as many employees here...
    
    function showEmployee(key)
    {
      var emp = employees[key];
      if (emp){
        var newwin = window.open('','','top=0,left=0,width=400,height=300');
        var html = '<html>\n<head>\n<title>' + emp[0] + '</title>\n</head>\n<body>\n<table>\n' + 
          '<tr><td>' + emp[0] + '</td></tr>\n' +  //name
          '<tr><td>' + emp[1] + '</td></tr>\n' +  //phone
          '<tr><td>' + emp[2] + '</td></tr>\n' +  //email
          '</table>\n<form><input type="button" value="Close" onclick="window.close()" /></form>\n</body>\n</html>';
        newwin.document.write(html);
        newwin.document.close();
        newwin.focus();
      }
      else {
        alert('Employee not found.');
      }
      return false; //cancel href action
    }
    </script>
    </head>
    <body>
    <a href="#" onclick="return showEmployee('Smith_Js')">Smith, Joseph</a><br />
    <a href="#" onclick="return showEmployee('Smith_Jn')">Smith, John</a><br />
    <a href="#" onclick="return showEmployee('Jordan_M')">Jordan, Michael</a><br />
    </body>
    </html>
    How can I incorporate a "Previous" and "Next" button so that it will then run the script again but display the next person's or previous person's info?


  •  

    Posting Permissions

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