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
onclickare bad practice. They’re an obsolete, cumbersome, and unintuitive way to listen for events. Always useaddEventListenerinstead.URLAPI and itssearchParamsproperty, which correctly encodes parameters:const url = new URL("/tryLogin", location); url.searchParams.set("id", id); url.searchParams.set("password", password); location = url;. TheURLconstructor expects an absolute URL as the first argument, or a relative one with an absolute base as the second argument.