Open a new tab in the background?

前端 未结 5 1528
闹比i
闹比i 2020-11-22 10:07

Using javascript, I want to open a new page in a different tab, but remain focused on the current tab. I know I can do it like this:

open(\'http://example.co         


        
5条回答
  •  梦谈多话
    2020-11-22 10:36

    Here is a complete example for navigating valid URL on a new tab with focused.

    HTML:

    Enter Url:   

    CSS:

    .panel{
      font-size:14px;
    }
    .panel input{
      border:1px solid #333;
    }
    

    JAVASCRIPT:

    function isValidURL(url) {
        var RegExp = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/;
    
        if (RegExp.test(url)) {
            return true;
        } else {
            return false;
        }
    }
    
    function openURL() {
        var url = document.getElementById("txturl").value.trim();
        if (isValidURL(url)) {
            var myWindow = window.open(url, '_blank');
            myWindow.focus();
            document.getElementById("txturl").value = '';
        } else {
            alert("Please enter valid URL..!");
            return false;
        }
    }
    

    I have also created a bin with the solution on http://codebins.com/codes/home/4ldqpbw

提交回复
热议问题