简体   繁体   中英

Trying to open a new tab with JS with a function

Im trying to call a function in an html to a javascript file with a button and I think the function is going through because the alert is working but the 2nd part of the js file isnt working (This is the part where the javascript opens the new tab.) Please help.

This is my html file. Don't mind the class="submit_button" thing. I just cut of my styling. And the rest of my popup.html file.

<div>
    <form>
        <input class="URL_Textbox" id="WebsiteName" name="WebsiteURL_Name" placeholder="URL" >
        <input
            type="button"
            class="submit_button"
            onClick="submitFunction()"
            value="Go"
          >
    </form>
</div>

This is my javascript file

function submitFunction() {
   alert("1234567890")
   location.href("https://www."+document.getElementById('WebsiteURL_Name').value,"_self")
}

Thanks for taking the time to respond and read my question!

You need to use location.href this way:

location.href = 'someURL';

About Window.location property (MDN)

 function submitFunction() { location.href = "https://www." + document.getElementById('WebsiteName').value; } 
 <div> <form> <input class="URL_Textbox" id="WebsiteName" name="WebsiteURL_Name" placeholder="stackoverflow.com" > <input type="button" class="submit_button" onClick="submitFunction()" value="Go" > </form> </div> 

The id should be WebsiteName . I suppose, you want to open a new browser window with the inserted value. That can be done with window.open(...) .

 function submitFunction() { alert("1234567890") window.open("https://www." + document.getElementById('WebsiteName').value, "_self") } 
 <div> <form> <input class="URL_Textbox" id="WebsiteName" name="WebsiteURL_Name" placeholder="URL"> <input type="button" class="submit_button" onClick="submitFunction()" value="Go"> </form> </div> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM