javaScript - 新选项卡中的弹出窗口

时间:2015-08-29 17:02:14

标签: javascript html

我有一个问题,希望这里有人能帮助我!

有人可以帮我编辑这个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>

1 个答案:

答案 0 :(得分:0)

在您的代码中,删除在声明openWin();函数后刚刚发送的openWin调用。

在html中添加一个按钮,例如:

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

添加以下javascript代替对openWin()的调用,这会向按钮添加click事件监听器,当按钮单击处理函数调用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
});