0

I'm using this button in HTML. Below is a description of the button, followed by a description of "functionclick().

<button type="submit" class="btn btn-primary" ONCLICK="functionclick()">확인</button>
<script type="text/javascript">
  function functionclick() {
    let id =  document.getElementById('exampleDropdownFormEmail1').value;
    let password = document.getElementById('exampleDropdownFormPassword1').value;

    if (id == "" || password == "") {
      alert("회원 정보를 입력하세요");
      history.back();
    } else{
      var path= "/tryLogin?id=" + id + "&password=" + password;
      const url = new URL(path);
      window.location.href = url;
    }
  }

</script>
Uncaught TypeError: Failed to construct 'URL': Invalid URL
    at functionclick (login:23:23)
    at HTMLButtonElement.onclick (login:83:81)
functionclick @ login:23
onclick @ login:83

I don't know much about JavaScript grammar. So I have a lot of difficulties in doing simple things... I need your help.

The result I expected is to send a 'GET' request to the server while moving the page to url in "var path". But It doesn't worked and popped up this error where the chrome debugger

Sebastian Simon
  • 18,263
  • 7
  • 55
  • 75
참직김
  • 3
  • 2
  • Inline event handlers like `onclick` are [bad practice](/q/11737873/4642212). They’re an [obsolete, cumbersome, and unintuitive](/a/43459991/4642212) way to listen for events. Always [use `addEventListener`](//developer.mozilla.org/en/docs/Learn/JavaScript/Building_blocks/Events#inline_event_handlers_%E2%80%94_dont_use_these) instead. – Sebastian Simon Dec 03 '22 at 11:06
  • Instead of concatenating query parameters manually, please use the [`URL` API](//developer.mozilla.org/en/docs/Web/API/URL) and its `searchParams` property, which _correctly encodes_ parameters: `const url = new URL("/tryLogin", location); url.searchParams.set("id", id); url.searchParams.set("password", password); location = url;`. The `URL` constructor expects an _absolute_ URL as the first argument, or a relative one with an _absolute base_ as the second argument. – Sebastian Simon Dec 03 '22 at 11:06

0 Answers0