簡體   English   中英

如何使用javascript防止彈出窗口?

[英]How to prevent popup using javascript?

無論如何我可以使用javascript來阻止來自另一個網站(iframe)的彈出窗口?

示例:我有一個網站,它 iframe 到其他幾個網站。 其中之一有這樣的彈出腳本:

<script type="text/javascript">
var popunder=new Array()
popunder[0]="http://www.target.com"

//Specify the width and height of new popunder window (in pixels).
var width = '700'; 
var height = '450';

//these are obvious variables. set "yes" or "no".
var p = 'scrollbars=yes, resizable=yes, toolbar=yes,' + 'menubar=yes, status=yes, location=yes, left=85, top=20, height=' + height + ',width=' + width;

// Load new PopUnder only once per browser session? (0=no, 1=yes)
// Putting 0 will cause the Popunder to load every time page is loaded
// Specifying 1 will cause it to load only once per session
var one_time=0

// That's it! Don't edit the code below unless you're really good. :-P //

function get_cookie(Name) {
  var search = Name + "="
  var returnvalue = "";
  if (document.cookie.length > 0) {
    offset = document.cookie.indexOf(search)
    if (offset != -1) { // if the cookie exists
      offset += search.length
      //set the index of beginning value
      end = document.cookie.indexOf(";", offset);

    if (end == -1) // set the index of the end of cookie value
         end = document.cookie.length;
         returnvalue = unescape(document.cookie.substring(offset, end))
      }
   }
  return returnvalue;
}

function loadornot(){
if (get_cookie('popunder')==''){
load_pop_power()
document.cookie="popunder=yes"
}
}

function load_pop_power(){
win2 = window.open(popunder[Math.floor(Math.random()*(popunder.length))], "bw", p)
win2.blur()
window.focus()
}

if (one_time==0)
load_pop_power()
else
loadornot()
</script>

前提是無法阻止此彈出窗口,並且用戶在 Firefox 或 IE 上的安全設置較低。

我有 ff。 網站上的 iframe: (iframe.php)

<iframe src="http://friend.com/pop.php"></iframe>

我應該在 iframe.php 頁面上做什么以防止彈出?

這是不可能的。 wdm 是對的。 但這里提供了更詳細的答案。

如何阻止來自 iframe 的彈出窗口?

我很確定你想要做的事情是不可能的。

兩種選擇...

  1. 避免使用具有彈出窗口的 iframe 網站。
  2. 詢問他們是否會刪除彈出窗口或為您創建一個特殊的登錄頁面。 如果您以某種方式與他們有關聯,他們可能會幫助您。

不,我不同意其他人的觀點,html5 有一個沙箱屬性,它控制通過 iframe 看到的內容或可以執行的操作。

只需在您的 Iframe 中添加以下屬性,它就會阻止彈出窗口“ sandbox="allow-scripts allow-forms allow-same-origin" "

例如<iframe src="source of your iframe" sandbox="allow-scripts allow-forms allow-same-origin">

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM