1

Hey guys still still trying to get this to work I want the pages to open in the same window rather than opening a new window?? any suggestions without changing my entire code

function openPage() {
        var x = document.getElementById("search").value;
             x = x.replace(/\b\w/g, (firstLetter) => firstLetter.toUpperCase());

            if (x === "Aaron Carter") { 
                  window.open("files/aaroncarter.html";
            }

            if (x === "Albert Einstein") {
                window.open ("files/alberteinstein.html");
            }
            
             else {
                window.open ("files/error.html" , "_self")
            }
            
            }
 <form action="" style="margin-bottom: 40px;">
                <div class="input-group">
                  <input type="search" id="search" autocomplete="off" onchange="openPage()"  class="form-control rounded-pill rounded-end-0 border-0 ps-4" placeholder="Search... e.g. Stan Laurel">
                  <button type="button" id="button" onclick="openPage()" value="Chercher" class="btn btn-primary rounded-pill rounded-start-0 shadow-none">
                    <i class="fa fa-search"></i>
                  </button>
                </div>
              </form>  
numbskull
  • 23
  • 5
  • Unless it's your intention to always show the error page unless "Aaron Carter" is the regEx modified version of the search term, you want to change the 2nd `if` to an `else if` - just like the answer which appeared as I was typing this suggests. – enhzflep Mar 16 '23 at 16:37

2 Answers2

2

The target parameter to window.open should reference an existing named window. _self means the current window and since window.open() would normally open a new window, _self points to that newly opened window. You could specify a name for the original window and then reference that.

But, the simplest solution is don't use window.open() and just change the window.location.href to the desired URL.

 location.href = "files/error.html";
Scott Marcus
  • 64,069
  • 6
  • 49
  • 71
2

Instead of using window.open use window.location.href

I think this is related to your question.

function openPage(event) {
  event.preventDefault();
  var x = document.getElementById("search").value;
  x = x.replace(/\b\w/g, (firstLetter) => firstLetter.toUpperCase());

  if (x === "Aaron Carter") {
    window.location.href = "files/aaroncarter.html";
  } else if (x === "Albert Einstein") {
    window.location.href = "files/alberteinstein.html";
  } else {
    window.location.href = "files/error.html";
  }
}
<form action="" style="margin-bottom: 40px;">
  <div class="input-group">
    <input type="search" id="search" autocomplete="off" class="form-control rounded-pill rounded-end-0 border-0 ps-4" placeholder="Search... e.g. Stan Laurel">
    <button type="submit" id="button" onclick="openPage(event)" value="Chercher" class="btn btn-primary rounded-pill rounded-start-0 shadow-none">
      <i class="fa fa-search"></i>
    </button>
  </div>
</form>
EJF
  • 134
  • 7
  • How is this different from my answer which was already posted before yours? – Scott Marcus Mar 16 '23 at 16:37
  • It's not. I did not see your answer before posting mine. It seems we both worked on it and arrived at the same solution independently. – EJF Mar 16 '23 at 16:45