2009-04-14 66 views
16

只要用户单击不在同一个域中的iframe中的页面上的链接,就需要得到通知。我知道xss限制,但是我需要知道的是在iframe中提供的当前页面。有没有办法做到这一点,而不违反xss规则?使用JQuery获取Iframe的当前src

回答

8

如果不是跨网站脚本限制,这应该工作。不幸的是,我不知道如何在不违反这些限制的情况下获取网址。

<html> 
    <head> 
     <script type="text/javascript"> 
      function GetIFrameUrl() 
      { 
       alert('url = ' + document.frames['frame1'].location.href); 
      } 
     </script> 
    </head> 
    <body> 
     <a href="#" onclick="GetIFrameUrl();">Find the iFrame URL</a> 
     <iframe name="frame1" src="http://www.google.com" width="100%" height="400"></iframe> 
    </body> 
</html> 
+0

这是否viloate XSS规则是什么? – Micah 2009-04-14 18:25:14

+3

是的,它的确如此。我不相信有一种方法可以在不违反规则的情况下做你所需要的。 – joshuapoehls 2009-04-15 05:40:49

32

要获得使用jQuery的iframe的位置:

alert($('iframeId').contents().get(0).location.href); 
4

您可以轻松地用香草JS做到这一点。

  • 查询与特定的名称,ID类或任何
  • 一个元素的文档获取属性源为元素

    //Get by Id 
    var frame_src = document.getElementById('myIframe').src 
    //Get by tag name - get the first 
    var frame_src = document.getElementsByName('frmExternal')[0].src 
    //Alert 
    alert(frame_src) 
    
0
<script type="text/javascript"> // if window open in iframe then redirect to main site 
     if(window.top.location != window.self.location) 
     { 
      alert(window.self.location); 
      // top.window.location.href = window.self.location; 
     } 
    </script>