SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Member
    Join Date
    Oct 2004
    Location
    Bangalore
    Posts
    20
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Unhappy Hiding a div in popup window

    If a page is opened in a popup window, banner1 should be hidden, but it
    should be visible if opened in a normal window.

    I am using following code:

    <script>
    function hideBanner(){
    if(window.opener){
    document.write('<style>#banner*1{ display:none; }</style>');
    }
    }
    hideBanner()
    </script>

    Check out the attachment to see a demo page (rename opener.txt to opener.html)

    This code is working as expected in Win2000/IE 6.
    But in
    OS: Windows XP Home SP2
    IE: 6.0.2900.2180.xpsp_sp2_rtm.040*803-2158

    banner1 is hidden even in opener (that is parent) window. Anything to
    do with return value of 'window.opener' property?

    Any of you guys facing this problem in your browser?

    Thanks in advance

    - Kiran Makam
    Attached Files Attached Files

  2. #2
    SitePoint Evangelist pip's Avatar
    Join Date
    Jun 2001
    Location
    Cape Town, South Africa
    Posts
    548
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I don't have an XP machine to test this on close by, but I don't understand why you would use a javascript function that way. It's not dynamically doing anything except for writing out stylesheet code. I know that it works on 2000, IE6, but the way I would have gone about it is to add a function to the window.onload event, and creating the JavaScript code within the head of the document.

    PHP Code:
    <HEAD>
    <
    SCRIPT LANGUAGE="JavaScript">
    function 
    initDoc() {
     
    pip_HideBanner();
    }
    function 
    pip_HideBanner() {
     if (
    window.opener) {
      var 
    oBanner document.getElementById("banner1");
      if (
    oBanner) {
       
    oBanner.style.display "none";
      }
     }
    }
    window.onload initDoc;
    </SCRIPT>
    </HEAD> 
    Perhaps you'd like to give that a bash and check if it works in XP or not. If it doesn't, I would have to say that this is a rather interesting problem.
    - Pip
    ---------------------------------------------------------------------------------
    Nothing takes the taste out of peanut butter quite like unrequited love.

  3. #3
    SitePoint Member
    Join Date
    Oct 2004
    Location
    Bangalore
    Posts
    20
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks Pip.

    In my original project, what i have to hide is more than a div. Its dynamic, sometimes its a div with an id and sometimes its a div with a class assigned(but no id).

    MY original function is like:

    function hideBanner(){
    if(window.opener){
    document.write('<style>.class1, .class2, #banner{ display:none; }</style>');
    }
    }

    I had to take this approach to cover those divs that did'nt have an id. (we have to change lot of server side code to add ids, instead we tried to user javascript/css to solve the issue.

    - Kiran Makam

  4. #4
    SitePoint Evangelist pip's Avatar
    Join Date
    Jun 2001
    Location
    Cape Town, South Africa
    Posts
    548
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Okay... I think I understand the requirement to have it done that way.

    I don't think that the problem would be on the return value of window.opener. Window.opener always returns null, unless the window was opened by another window.

    I just tested your page on MS Windows Server 2003, and it works both ways.

    If you need to stick to the way that you're currently doing it, try setting the style properties of the elements directly, instead of writing out stylesheet code. Furthermore, try to keep your style and script tags within the head of your document, and execute them onload.

    PHP Code:
    <HEAD>
    <
    TITLE></TITLE>
     
    <
    style>
    #banner1, #banner2 {
     
    display:block;
    }
    </
    style>
     
    <
    SCRIPT LANGUAGE="JavaScript">
    function 
    setCSSattribute(selectorattributevalue) {
     var 
    outval = new String();
     for (
    0document.styleSheets.lengthi++) {
      for (
    0document.styleSheets[i].rules.lengthj++) {
       
    curSelector document.styleSheets[i].rules[j];
       if (
    curSelector.selectorText == selector) {
        
    curSelector.style[attribute] = value;
       }
      }
     }
     return 
    outval;
    }
    function 
    hideBanner(){ 
     if(
    window.opener){ 
       
    setCSSattribute("#banner1","display","none");
       
    setCSSattribute("#banner2","display","none");
     } 
    }
    window.onload hideBanner;
    </SCRIPT>
     
    </HEAD> 
    - Pip
    ---------------------------------------------------------------------------------
    Nothing takes the taste out of peanut butter quite like unrequited love.

  5. #5
    SitePoint Evangelist pip's Avatar
    Join Date
    Jun 2001
    Location
    Cape Town, South Africa
    Posts
    548
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Let me know if it works.
    - Pip
    ---------------------------------------------------------------------------------
    Nothing takes the taste out of peanut butter quite like unrequited love.


Bookmarks

Posting Permissions

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