{"id":370,"date":"2025-10-02T07:04:54","date_gmt":"2025-10-02T07:04:54","guid":{"rendered":"https:\/\/iotnoob.com\/wordpress\/?p=370"},"modified":"2025-10-02T07:04:55","modified_gmt":"2025-10-02T07:04:55","slug":"event-preventdefault","status":"publish","type":"post","link":"https:\/\/iotnoob.com\/wordpress\/2025\/10\/02\/event-preventdefault\/","title":{"rendered":"event.preventDefault();"},"content":{"rendered":"\n<p>\u0e40\u0e27\u0e25\u0e32\u0e40\u0e23\u0e32\u0e40\u0e02\u0e35\u0e22\u0e19 event handling \u0e1a\u0e32\u0e07\u0e17\u0e35\u0e40\u0e23\u0e32\u0e01\u0e47\u0e15\u0e49\u0e2d\u0e07\u0e01\u0e32\u0e23\u0e44\u0e21\u0e48\u0e43\u0e2b\u0e49\u0e21\u0e31\u0e19\u0e17\u0e33\u0e07\u0e32\u0e19\u0e43\u0e19\u0e23\u0e39\u0e1b\u0e41\u0e1a\u0e1a\u0e1b\u0e23\u0e01\u0e15\u0e34\u0e1a\u0e32\u0e07\u0e2d\u0e22\u0e48\u0e32\u0e07 \u0e40\u0e0a\u0e48\u0e19 \u0e40\u0e23\u0e32\u0e21\u0e35 link \u0e43\u0e2b\u0e49\u0e01\u0e14 \u0e41\u0e15\u0e48\u0e40\u0e23\u0e32\u0e40\u0e1e\u0e34\u0e48\u0e21 onClick \u0e27\u0e48\u0e32\u0e16\u0e49\u0e32 user click link \u0e41\u0e25\u0e49\u0e27 \u0e41\u0e17\u0e19\u0e17\u0e35\u0e48\u0e08\u0e30\u0e43\u0e2b\u0e49\u0e21\u0e31\u0e19\u0e27\u0e34\u0e48\u0e07\u0e44\u0e1b\u0e17\u0e35\u0e48 link \u0e19\u0e31\u0e49\u0e19\u0e46\u0e40\u0e25\u0e22 \u0e01\u0e47\u0e43\u0e2b\u0e49\u0e21\u0e31\u0e19\u0e02\u0e36\u0e49\u0e19 alert \u0e2b\u0e23\u0e37\u0e2d dialog box \u0e43\u0e2b\u0e49 user confirm \u0e01\u0e48\u0e2d\u0e19 \u0e41\u0e25\u0e49\u0e27\u0e04\u0e48\u0e2d\u0e22\u0e44\u0e1b\u0e15\u0e32\u0e21 link &#8211; \u0e17\u0e35\u0e19\u0e35\u0e49\u0e16\u0e49\u0e32 user click cancel \u0e2b\u0e23\u0e37\u0e2d\u0e22\u0e01\u0e40\u0e25\u0e34\u0e01 \u0e40\u0e23\u0e32\u0e15\u0e49\u0e2d\u0e07\u0e01\u0e32\u0e23\u0e43\u0e2b\u0e49\u0e21\u0e31\u0e19\u0e22\u0e01\u0e40\u0e25\u0e34\u0e01 \u0e44\u0e21\u0e48\u0e44\u0e1b\u0e15\u0e32\u0e21 link \u0e19\u0e31\u0e49\u0e19<\/p>\n\n\n\n<p>\u0e2a\u0e34\u0e48\u0e07\u0e17\u0e35\u0e48\u0e08\u0e30\u0e0a\u0e48\u0e27\u0e22\u0e40\u0e23\u0e32\u0e04\u0e37\u0e2d event.preventDefault();<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;a href=\"https:\/\/www.example.com\" id=\"myLink\">Go to Example.com&lt;\/a>\n\n&lt;script>\ndocument.getElementById('myLink').onclick = function(event) {\n    var userResponse = prompt(\"Are you sure you want to go to Example.com? Type 'yes' to proceed.\");\n\n    if (userResponse !== 'yes') {\n        event.preventDefault(); \/\/ Stop the link from navigating\n        alert(\"Navigation cancelled.\");\n    } else {\n        alert(\"Proceeding to Example.com!\");\n        \/\/ The link will navigate as usual if preventDefault() is not called\n    }\n};\n&lt;\/script><\/pre>\n\n\n\n<p>\u0e2d\u0e31\u0e19\u0e19\u0e35\u0e49\u0e40\u0e23\u0e32\u0e43\u0e0a\u0e49 prompt \u0e21\u0e31\u0e19\u0e08\u0e30\u0e2b\u0e22\u0e38\u0e14\u0e01\u0e32\u0e23\u0e17\u0e33\u0e07\u0e32\u0e19\u0e02\u0e2d\u0e07 script \u0e08\u0e19\u0e01\u0e27\u0e48\u0e32\u0e40\u0e23\u0e32\u0e08\u0e30\u0e15\u0e2d\u0e1a prompt \u0e41\u0e25\u0e49\u0e27\u0e04\u0e48\u0e2d\u0e22\u0e44\u0e1b\u0e15\u0e48\u0e2d \u0e14\u0e31\u0e07\u0e19\u0e31\u0e49\u0e19 \u0e40\u0e23\u0e32\u0e43\u0e2a\u0e48 event.preventDefault(); \u0e2b\u0e25\u0e31\u0e07\u0e08\u0e32\u0e01 confirm \u0e41\u0e25\u0e49\u0e27\u0e01\u0e47\u0e44\u0e14\u0e49<\/p>\n\n\n\n<p>\u0e41\u0e15\u0e48\u0e16\u0e49\u0e32\u0e40\u0e23\u0e32\u0e40\u0e23\u0e35\u0e22\u0e01\u0e43\u0e0a\u0e49 library \u0e2d\u0e22\u0e48\u0e32\u0e07 SweetAlert2 \u0e16\u0e49\u0e32\u0e40\u0e23\u0e32\u0e44\u0e21\u0e48\u0e2a\u0e31\u0e48\u0e07 preventDefault \u0e15\u0e31\u0e49\u0e07\u0e41\u0e15\u0e48\u0e15\u0e49\u0e19 \u0e21\u0e31\u0e19\u0e08\u0e30\u0e43\u0e0a\u0e49\u0e07\u0e32\u0e19\u0e44\u0e21\u0e48\u0e44\u0e14\u0e49 &#8211; \u0e14\u0e31\u0e07\u0e19\u0e31\u0e49\u0e19 \u0e40\u0e23\u0e32\u0e15\u0e49\u0e2d\u0e07\u0e17\u0e33 event.preventDefault(); \u0e15\u0e31\u0e49\u0e48\u0e07\u0e41\u0e15\u0e48 onClick \u0e17\u0e33\u0e07\u0e32\u0e19 &#8211; \u0e08\u0e32\u0e01\u0e19\u0e31\u0e49\u0e19\u0e07\u0e32\u0e19\u0e17\u0e35\u0e48\u0e08\u0e30\u0e15\u0e49\u0e2d\u0e07 reload \u0e44\u0e1b\u0e2b\u0e19\u0e49\u0e32\u0e2d\u0e37\u0e48\u0e19\u0e01\u0e47\u0e15\u0e49\u0e2d\u0e07\u0e43\u0e2b\u0e49 user \u0e01\u0e33\u0e2b\u0e19\u0e14\u0e43\u0e2b\u0e49\u0e43\u0e2b\u0e21\u0e48 (\u0e40\u0e1e\u0e23\u0e32\u0e30\u0e02\u0e2d\u0e07\u0e40\u0e14\u0e34\u0e21\u0e22\u0e01\u0e40\u0e25\u0e34\u0e01\u0e44\u0e1b\u0e41\u0e25\u0e49\u0e27)<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;link rel=\"stylesheet\" href=\"https:\/\/cdn.jsdelivr.net\/npm\/sweetalert2@11\/dist\/sweetalert2.min.css\">\n&lt;script src=\"https:\/\/cdn.jsdelivr.net\/npm\/sweetalert2@11\/dist\/sweetalert2.all.min.js\">&lt;\/script>\n\n&lt;a href=\"https:\/\/www.google.com\" class=\"confirm-link\">Go to Google&lt;\/a>\n&lt;a href=\"https:\/\/www.facebook.com\" class=\"confirm-link\">Go to Facebook&lt;\/a>\n&lt;a href=\"\/some-other-page.html\" class=\"confirm-link\">Go to Another Page&lt;\/a>\n\n&lt;script>\ndocument.querySelectorAll('.confirm-link').forEach(link => {\n    link.addEventListener('click', function(event) {\n        event.preventDefault(); \/\/ Prevent the default navigation immediately\n\n        const targetUrl = this.href; \/\/ Get the URL from the href attribute\n\n        Swal.fire({\n            title: 'Are you sure?',\n            text: `You are about to navigate to ${targetUrl}.`,\n            icon: 'warning',\n            showCancelButton: true,\n            confirmButtonColor: '#3085d6',\n            cancelButtonColor: '#d33',\n            confirmButtonText: 'Yes, proceed!',\n            cancelButtonText: 'No, stay here'\n        }).then((result) => {\n            if (result.isConfirmed) {\n                \/\/ If confirmed, manually navigate to the URL\n                window.location.href = targetUrl;\n            } else {\n                \/\/ User clicked \"Cancel\" or dismissed the alert\n                Swal.fire(\n                    'Cancelled',\n                    'Your navigation was cancelled.',\n                    'info'\n                );\n            }\n        });\n    });\n});\n&lt;\/script><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u0e40\u0e27\u0e25\u0e32\u0e40\u0e23\u0e32\u0e40\u0e02\u0e35\u0e22\u0e19 event handling \u0e1a\u0e32\u0e07\u0e17\u0e35\u0e40\u0e23\u0e32\u0e01\u0e47\u0e15\u0e49\u0e2d\u0e07\u0e01\u0e32\u0e23\u0e44\u0e21\u0e48\u0e43\u0e2b\u0e49\u0e21\u0e31\u0e19\u0e17\u0e33\u0e07\u0e32\u0e19\u0e43\u0e19\u0e23\u0e39\u0e1b\u0e41\u0e1a\u0e1a\u0e1b\u0e23\u0e01\u0e15\u0e34\u0e1a\u0e32\u0e07\u0e2d\u0e22\u0e48\u0e32\u0e07 \u0e40\u0e0a\u0e48\u0e19 \u0e40\u0e23\u0e32\u0e21\u0e35 link \u0e43\u0e2b\u0e49\u0e01\u0e14 \u0e41\u0e15\u0e48\u0e40\u0e23\u0e32\u0e40\u0e1e\u0e34\u0e48\u0e21 onClick \u0e27\u0e48\u0e32\u0e16\u0e49\u0e32 user click link \u0e41\u0e25\u0e49\u0e27&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-370","post","type-post","status-publish","format-standard","hentry","category-programming"],"_links":{"self":[{"href":"https:\/\/iotnoob.com\/wordpress\/wp-json\/wp\/v2\/posts\/370","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/iotnoob.com\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/iotnoob.com\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/iotnoob.com\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/iotnoob.com\/wordpress\/wp-json\/wp\/v2\/comments?post=370"}],"version-history":[{"count":1,"href":"https:\/\/iotnoob.com\/wordpress\/wp-json\/wp\/v2\/posts\/370\/revisions"}],"predecessor-version":[{"id":371,"href":"https:\/\/iotnoob.com\/wordpress\/wp-json\/wp\/v2\/posts\/370\/revisions\/371"}],"wp:attachment":[{"href":"https:\/\/iotnoob.com\/wordpress\/wp-json\/wp\/v2\/media?parent=370"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/iotnoob.com\/wordpress\/wp-json\/wp\/v2\/categories?post=370"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/iotnoob.com\/wordpress\/wp-json\/wp\/v2\/tags?post=370"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}