How to stop js execution if post parameter is not received?
-
The default js for all pages of the site has a chat refresh function.
function updatePosts() { $.post(dle_root + "engine/modules/chat/scripts/ajaxLoad.php", { ub_id: ub_id }, function(s) { var e; JSON.parse(s).forEach(function(s) { Number(s.id) > Number(last_message_id) && (session_user == s.username ? $("#messages-wrapping").append(load_self_message(s.message, s.username)) : $("#messages-wrapping").append(load_message(s.message, s.username))), e = s.id }), last_message_id = e }) } $(document).ready(function() { $.post(dle_root + "engine/modules/chat/scripts/ajaxLoad.php", { ub_id: ub_id }, function(s) { JSON.parse(s).forEach(function(s) { session_user == s.username ? $("#messages-wrapping").append(load_self_message(s.message, s.username)) : $("#messages-wrapping").append(load_message(s.message, s.username)), last_message_id = s.id }) }) }), setInterval(updatePosts, 2000);
On pages where the chat is not provided, I get a permanent error in the console every 2 seconds:
Uncaught ReferenceError: ub_id is not defined
How to check for ub_id in js and not execute the script if ub_id is not defined?
Please help.
Thank you!JavaScript Olivia Krueger, Feb 15, 2020 -
I figured it out, did it like this:
if (typeof ub_id! == 'undefined') {/ * ... * /}
Anonymous
1 Answers
Your Answer
To place the code, please use CodePen or similar tool. Thanks you!