如何通过单击按钮在同一选项卡上打开新网页

how to open a new web page on the same tab by clicking a button?

本文关键字:新网页 网页 按钮 单击 何通过 选项      更新时间:2023-09-26

我的网页上有一个按钮。它的代码是:

/* code of the other forms  */
<form method="get"  onsubmit="return validation()" > 
     <input type="submit" id="x" value="Search"  onmouseover="mover(this)" onmouseout="mout(this)" /> 
</form>

当我点击按钮时,应该验证其他表单的值,并在同一选项卡上打开新的网页。我对验证部分没有问题。问题是新的网页在一个新的选项卡中打开,这不是我想要的。到目前为止,代码是:

 function validation(){
    var x=document.forms["flyrics"]["lyrics"].value;
    if (x==null || x=="")
    {
        alert("Search without Lyrics?");
        return false;
    }
    var y=document.forms["fartist"]["artist"].value;
    if (y==null || y=="")
    {
        alert("Search without Artist Name?");
        return false;
    }
            window.open("songList.html", "_self");      
} 

在最后一行中,它是在同一个选项卡上打开新页面的代码。但每次我单击按钮时,都会打开一个新的选项卡。如何更正?我也尝试过使用以下代码。但我不知道为什么他们都没能在同一个标签上打开新页面

 location.href = "songList";
 window.location="songList.html";
 window.open("songList.html",  "currentWindow", "");

实际问题在哪里?需要帮助来修复它。我只需要使用javascript和html来完成这项工作。

我认为您想要实现的是在提交表单之前首先验证搜索?您必须在表单选项卡中添加动作属性,然后移动return validation()以提交按钮。

看看下面的例子:

这将允许您在同一选项卡上加载songList.html

function validation() {
    var x=document.forms["flyrics"]["lyrics"].value;
    if (x==null || x=="")
    {
        alert("Search without Lyrics?");
        return false;
    }
    var y=document.forms["fartist"]["artist"].value;
    if (y==null || y=="")
    {
        alert("Search without Artist Name?");
        return false;
    }
} 
<!DOCTYPE html>
<body>
<form id="flyrics">
        <input type="text" id="lyrics" value="">
</form>
<form id="fartist">
        <input type="text" id="artist" value="">
</form>
/* code of the other forms  */
<form id="songList" action="songList.html">
        <input type="submit" id="x" value="Search" onclick="return validation()"/> 
</form>
</body>

如果你只做怎么办

        window.open("songList.html");  

尝试使用window.location.assign:

window.location.assign("songList.html");

试试这个

window.location.replace("songList.html");

这对我来说很好!