2015-08-29 30 views
-1

我有一個問題,希望這裏的任何人都能幫助我!javaScript - 在新選項卡中彈出窗口

有人能幫我編輯這個JavaScript代碼嗎?

問題是當我進入到有這段代碼的頁面時,彈出窗口自動啓動。是否可以通過創建一個啓動按鈕並在新選項卡中暫停彈出按鈕來手動執行此操作?謝謝!

<script type="text/javascript"> 
var timer = '<?=$timer;?>'; 
view_go = true; 
function start() { 
    if (view_go) { 
     if (timer > 1) { 
      timer = timer - 1; 
      $("#seconds").html(timer); 
      setTimeout("start();", 1000); 
     } else { 
      $("#seconds").html("0"); 
      var response = '<?=$site2['id'];?>'; 
      var userid = "<?=$data['id'];?>"; 
      $.ajax({ 
       type: "POST", 
       url: "earn-traffic-completed.php", 
       data: "site=" + response + "&user=" + userid, 
       success: function(msg) { 
        refreshpage(); 
       } 
      }); 
     } 
    } 
} 
view_go = true; 
function play() { 
    if (!view_go) { 
     view_go = true; 
     start(); 
    } 
    return false; 
} 
function pause() { 
    if (view_go) { 
     view_go = false; 
    } 
    return false; 
} 
var myWindow; 

function openWin() { 
    myWindow = window.open("<?=$url;?>", "<?=$site['name'];?>", "toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=yes, copyhistory=no, width=800,height=600, top=100, left=400,"); 
} 

openWin(); 

var timing = setInterval(function() { 
    if(myWindow.closed) { 
     clearInterval(timing); 
     window.location = '<?=$site['url'];?>/index.php'; 
    } 
}, 1000); 

setTimeout(function() { 
    if (!myWindow || myWindow.closed || myWindow.closed === "undefined" || myWindow === "undefined" || parseInt(myWindow.innerWidth, 10) === 0 || myWindow.document.documentElement.clientWidth !== 800 || myWindow.document.documentElement.clientHeight !== 600) { 
     alert("ERROR! - Please enable popups and then refresh the page."); 
     pause(); 
    } 
}, 1000); 
function closeWin() { 
    if (myWindow) { 
     myWindow.close(); 
     pause(); 
    } 
} 
checkWin(); 
function checkWin() { 
    if (!myWindow) { 
     alert('ERROR! - Please enable popups and then refresh the page.'); 
     pause(); 
     setTimeout("checkWin();", 10); 
    } 
    if (myWindow.closed) { 
     pause(); 
     setTimeout("checkWin();", 10); 
    } else { 
     setTimeout("checkWin();", 10); 
    } 
} 
start(); 
     function refreshpage() 
     { 

      window.location = document.location.href; 
     } 
     </script> 

回答

0

在你的代碼中刪除的呼叫,您有openWin函數聲明剛過openWin();

將按鈕添加到您的HTML,例如:

<button id="openWinButton">Open Window</button> 

添加到位調用以下JavaScript來openWin()它增加點擊事件偵聽器的按鈕,當按鈕被點擊的處理函數調用你openWin功能

document.getElementById('openWinButton').addEventListener('click',function(e) { 
    e.preventDefault(); // prevent the default action of the triggering element 
    openWin(); // call your method to open the window 
}); 
+0

請參閱下面給出的答案...看起來是一樣的.. – Lal

+0

計時器停止,即使我點擊按鈕,打開彈出,倒計時不行。請檢查我的JavaScript並告訴我如何添加按鈕以暫停並在新選項卡中打開彈出窗口。非常感謝! –

+0

請問,有什麼方法可以在新選項卡中打開它? –

相關問題