2012-05-06 56 views
3

我正试图通过下面的href链接实现2件事情。首先,我想启动一个弹出式窗口。完成。接下来,我想弹出窗口来显示一个iframe。这很容易完成,以及,直到我意识到我需要通过href链接文本作为我的iframe src中的参数。弹出窗口中的动态iframe

所以举例来说,在我的弹出窗口的iframe不会加载,除非其src="http://localhost:8080/test/document.html?OnSale"

我想不通,为什么在我的HTML网页正文中的document.write不会打印出动态的iframe我试着去创造我FOO()在href链接功能...

<div id="blanket" style="display:none;"></div> 
    <div id="popUpDiv" style="display:none;"> 
     <a href="#" onclick="popup('popUpDiv')"> 
      <img align="right" src="http://localhost:8080/test/img/close_img.png"> 
     </a> 
<script type="text/javascript"> 
    function foo(obj) 
    { 
     test1 = "http://localhost:8080/test/document.html?"+obj.text; 
     document.write('<iframe height="450" allowTransparency="true" frameborder="0" scrolling="yes" style="width:100%;" src="'+test1+'" type= "text/javascript"></iframe>'); 
    } 
</div> 

<a href="#" onclick="popup('popUpDiv');foo(this);">OnSale</a> 

编辑: 这里是我的全部HTML页面。所有东西都在tomcat7 w/win7和firefox上本地运行。

<html> 
<head> 
    <script type="text/javascript" src="http://localhost:8080/test/css-popup/css-pop.js"></script> 
    <link href="http://localhost:8080/test/css-popup/styles.css" rel="stylesheet" type="text/css" /> 
</head> 
<body> 
<div id="blanket" style="display:none;"></div> 
<div id="popUpDiv" style="display:none;"> 
    <a href="#" onclick="popup('popUpDiv')"> 
     <img align="right" src="http://localhost:8080/test/css-popup/x.png"> 
    </a> 
    <script type="text/javascript"> 
     function foo(obj){ 
      test1 = "http://localhost:8080/test/document.html?"+obj.innerHTML; 
      document.write('<iframe height="450" allowTransparency="true" frameborder="0" scrolling="yes" style="width:100%;" src="'+test1+'" type= "text/javascript"></iframe>'); 

     } 
    </script> 
</div> 

<a href="#" onclick="popup('popUpDiv');foo(this);">OnSale</a> 
</body> 
</html> 

回答

3

text是不是所有的浏览器标准,尽量innerHTML,而不是说,

function foo(obj){ 
    test1 = "http://localhost:8080/test/document.html?"+obj.innerHTML; 
    document.write('<iframe height="450" allowTransparency="true" frameborder="0" scrolling="yes" style="width:100%;" src="'+test1+'" type= "text/javascript"></iframe>'); 
} 

你都做了你的整个代码后进行更新,

据我了解,你想打开一个弹出窗口,并在其中显示一个动态创建的iframe。但是document.write适用于你当前的窗口。所以你必须首先处理你的弹出窗口。然后改变那个内容。

试试这个,

<html> 
<head> 
<script type="text/javascript" src="http://localhost:8080/test/css-popup/css-pop.js"></script> 
<link href="http://localhost:8080/test/css-popup/styles.css" rel="stylesheet" type="text/css" /> 
</head> 
<body> 


<div id="blanket" style="display:none;"></div> 
    <div id="popUpDiv" style="display:none;"> 
     <a href="#" onclick="popup('popUpDiv')"> 
      <img align="right" src="http://localhost:8080/test/css-popup/x.png"> 
     </a> 
<script type="text/javascript"> 
    var popUpWindow; 
    function popup(n) { 
     popUpWindow = window.open(n); 
    } 
       function foo(obj){ 
       test1 = "http://localhost:8080/test/document.html?"+obj.innerHTML; 
       popUpWindow.document.write('<iframe height="450" allowTransparency="true" frameborder="0" scrolling="yes" style="width:100%;" src="'+test1+'" type= "text/javascript"></iframe>'); 

       } 
     </script> 
</div> 

     <a href="#" onclick="popup('popUpDiv');foo(this);">OnSale</a> 

</body> 
</html>​ 

而且here is working live DEMO

+0

我感谢帮助ocanal。 innerHTML也没有做到这一点,但这很好的了解文字不是标准的 – Chris

+0

@Chris,再看看我更新的答案。 – ocanal

+0

ocanal,谢谢!这完全打开了iframe,但它在Firefox中打开为新选项卡,而不是弹出窗口。有没有什么办法可以弹出window.open(n)而不是新标签? – Chris