Search code examples
javascripthyperlinkwindow.open

How can I trigger window.open to a specific dynamic URL?


I would like to open a new tab with sent condition, my code now:

<span>Type <b>Link</b>: <input id="myInput" value=""> and click <b>Enter</b> 
<button style="display: none;" id="myButton" onclick="myFunction2()">Go!</button>

[...]

<script>
var websitelink = '"https://www.' + myInput + '.com"';
function myFunction2() {
    window.open(websitelink);
}

but all it does is opening a new blank tab. Any idea?

Thanks.


Solution

  • You have more quotes than you need in your websitelink

    It should work with this:

    var websitelink = "https://www." + myValue + ".com";
    

    Here it's a snippet:

    The code in a website should work but the snippet will not work because the allow-popups permission is not set in the iframe

    Blocked opening 'https://www.google.es.com/' in a new window because the request was made in a sandboxed frame whose 'allow-popups' permission is not set.

    function myFunction2() {
        var myInput = document.getElementById("myInput").value;
        var websitelink = "https://www." + myInput + ".com";
        window.open(websitelink);
    }
    <span>Type <b>Link</b>: <input id="myInput" value=""> and click <b>Enter</b> 
    <button style="" id="myButton" onclick="myFunction2()">Go!</button>