0

I have a popup window working with the following code, but I need to amend it to apply a CSS class to either the HTML tag or the Body tag, so I can style the popup window differently than the normal site.

Here's the code I'm currently using:

    <script language="javascript" type="text/javascript">
<!--
/****************************************************
     Author: Eric King
     Url: http://redrival.com/eak/index.shtml
     This script is free to use as long as this info is left in
     Featured on Dynamic Drive script library (http://www.dynamicdrive.com)
****************************************************/
var win=null;
function NewWindow(mypage,myname,w,h,scroll,pos){
if(pos=="random"){LeftPosition=(screen.width)?Math.floor(Math.random()*(screen.width-w)):100;TopPosition=(screen.height)?Math.floor(Math.random()*((screen.height-h)-75)):100;}
if(pos=="center"){LeftPosition=(screen.width)?(screen.width-w)/2:100;TopPosition=(screen.height)?(screen.height-h)/2:100;}
else if((pos!="center" && pos!="random") || pos==null){LeftPosition=0;TopPosition=20}
settings='width='+w+',height='+h+',top='+TopPosition+',left='+LeftPosition+',scrollbars='+scroll+',location=no,directories=no,status=no,menubar=no,toolbar=no,resizable=no';
win=window.open(mypage,myname,settings);
}
// -->
</script>

I tried to add a line right after the win=window.open line that looked like this:

win.document.getElementById('body').className += " popup";

Unfortunately it didn't work. Any ideas on how I can make it so if a window is popped up using this javascript function it'll apply a CSS class to the window?

The HTML call to the function was with this code, if it matters.

<li><a href="<?php echo get_permalink(); ?>" onclick="NewWindow(this.href,'broadcastplayer','600','200','no','center');return false" onfocus="this.blur()">Play in pop up</a>&nbsp;|&nbsp;</li>

Alternatively I've tried using this jquery version of a popup window (http://swip.codylindley.com/popupWindowDemo.html), but haven't figured out how to do it this way either. Thank you so much for your help, this has been killing me all day!

4

2 回答 2

0

如您所见,您正在尝试使用id='body'. 但是我认为您的标记看起来像 <body> ... </body>。如果这是真的,那么你不能使用 JS 函数 getElementById 因为你在 body 元素上没有 ID :)

解决方案是<body id='body'> ... </body>

您也可以使用不同的 JS 函数来选择主体。 var body = document.getElementsByTagName( "body" );

于 2013-05-29T04:38:15.530 回答
0

win.document.getElementById('body')

将查找与提供的 ID 匹配的 DOM 元素,在这种情况下,没有 ID 为 body 的元素。

尝试用win.document.getElementsByTagName('body')替换它。这应该给你body标签,然后你可以将CSS类应用于它。

由于您的文档中只有一个正文,您可以直接将类分配给弹出 HTML 本身中的正文标记。

于 2013-05-29T04:53:11.767 回答