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

2
  • Inline event handlers like onclick are bad practice. They’re an obsolete, cumbersome, and unintuitive way to listen for events. Always use addEventListener instead. Commented Dec 3, 2022 at 11:06
  • Instead of concatenating query parameters manually, please use the URL API 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. Commented Dec 3, 2022 at 11:06

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.