Skip to content

Commit d434ec0

Browse files
committed
Trim input value and add exception handling for invalid URL
1 parent c4313a1 commit d434ec0

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

src/assets/js/script.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ document.addEventListener("DOMContentLoaded", function (e) {
88
console.log("captured cleanly!");
99

1010
let form = {};
11-
form.query = document.getElementById("query").value;
11+
form.query = document.getElementById("query").value.trim();
1212
form.commercial = document.getElementById("commercial").checked;
1313
form.modify = document.getElementById("modify").checked;
14-
selectedEngine = document.querySelector(
14+
let selectedEngine = document.querySelector(
1515
'input[name="search-engine"]:checked',
1616
);
1717
form.searchEngine = selectedEngine.value;
@@ -24,8 +24,15 @@ document.addEventListener("DOMContentLoaded", function (e) {
2424

2525
// navigate behavior
2626
function navigateTo(link) {
27-
if (link) {
28-
window.open(link, "_blank");
27+
try {
28+
// Validate the URL using the URL constructor
29+
new URL(link); // If the URL is invalid, it will throw an error
30+
if (link) {
31+
window.open(link, "_blank");// If valid, open the link in a new tab
32+
}
33+
34+
} catch (error) {
35+
console.error("Error occurred: ", error);
2936
}
3037
}
3138

0 commit comments

Comments
 (0)