javascript执行后打开两个链接。一个在新标签中的一个当前

时间:2016-07-14 01:13:04

标签: javascript html function hyperlink onclick

我正在管理一个相当大的网站(几百页),我正在寻找一种执行javascript的方法 - 然后在当前选项卡中打开一个链接,在新选项卡中打开另一个链接。 Href链接在执行倒计时后工作正常。但是js链接被忽略了。如何在倒计时结束后让两者都开放?

HTML

    <a href="http://www.example.com/here"      onclick="startTimer(this);return false; window.open('http://www.example.com/newtab');">anchor text</a>

JS

<script>
function startTimer2(theLink2) {
    userInput = 25;
    if(userInput.length == 0){
        alert("Please enter a value");
    } else {
        var numericExpression = /^[0-9]+$/;

        function display1( notifier, str ) {
            document.getElementById(notifier).innerHTML ='Please Wait: ' + str;


        }

        function toMinuteAndSecond( x ) {
            return Math.floor(x/60) + ":" + x%60;
        }

        function setTimer( remain, actions ) {
            (function countdown() {
                display1("countdown", toMinuteAndSecond(remain));        
                actions[remain] && actions[remain]();
                (remain -= 1) >= 0 && setTimeout(countdown, 1000);
            })();

        }

        setTimer(userInput, {
            0: function () {  window.location=theLink2.href;  }
        }); 

    }
}
</script>

1 个答案:

答案 0 :(得分:0)

更改startTimer2(),因此它需要两个参数:链接和要在新窗口中打开的URL。然后在计时器用完时调用window.open()

function startTimer2(theLink2, windowURL) {
    userInput = 25;
    if(userInput.length == 0){
        alert("Please enter a value");
    } else {
        var numericExpression = /^[0-9]+$/;

        function display1( notifier, str ) {
            document.getElementById(notifier).innerHTML ='Please Wait: ' + str;


        }

        function toMinuteAndSecond( x ) {
            return Math.floor(x/60) + ":" + x%60;
        }

        function setTimer( remain, actions ) {
            (function countdown() {
                display1("countdown", toMinuteAndSecond(remain));        
                actions[remain] && actions[remain]();
                (remain -= 1) >= 0 && setTimeout(countdown, 1000);
            })();

        }

        setTimer(userInput, {
            0: function () {
                window.open(windowURL);
                window.location=theLink2.href;
            }
        }); 

    }
}

HTML:

    <a href="http://www.example.com/here" onclick="startTimer2(this, 'http://www.example.com/newtab');return false;">anchor text</a>