diff --git a/include/emp/web/Canvas.hpp b/include/emp/web/Canvas.hpp index 6d8174511..3ae3c6f36 100644 --- a/include/emp/web/Canvas.hpp +++ b/include/emp/web/Canvas.hpp @@ -76,7 +76,7 @@ namespace web { EM_ASM({ var cname = UTF8ToString($0); var canvas = document.getElementById(cname); - emp_i.ctx = canvas.getContext('2d'); + if (canvas) emp_i.ctx = canvas.getContext('2d'); }, id.c_str()); #endif } diff --git a/include/emp/web/Div.hpp b/include/emp/web/Div.hpp index f266aecd7..0d9eace92 100644 --- a/include/emp/web/Div.hpp +++ b/include/emp/web/Div.hpp @@ -185,9 +185,11 @@ namespace web { parent_id = UTF8ToString($0); child_id = UTF8ToString($1); let parent = document.getElementById(parent_id); - let span = document.createElement('span'); - span.id = child_id; - parent.appendChild(span); + if (parent) { + let span = document.createElement('span'); + span.id = child_id; + parent.appendChild(span); + } }, id.c_str(), in.GetID().c_str()); // Now that the new widget has some place to hook in, activate it! diff --git a/include/emp/web/Input.hpp b/include/emp/web/Input.hpp index 6255709ea..f96fd5d98 100644 --- a/include/emp/web/Input.hpp +++ b/include/emp/web/Input.hpp @@ -131,6 +131,7 @@ namespace web { function modifyInputs() { var input_el = document.getElementById(UTF8ToString($0)); + if (!input_el) return; input_el.addEventListener("input", modifyOffset); // the following taken from http://stackoverflow.com/questions/2856513/trigger-onchange-event-manually if ("fireEvent" in input_el) { diff --git a/include/emp/web/Listeners.hpp b/include/emp/web/Listeners.hpp index 6819dcb3a..357c98eb1 100644 --- a/include/emp/web/Listeners.hpp +++ b/include/emp/web/Listeners.hpp @@ -113,7 +113,8 @@ namespace web { MAIN_THREAD_EM_ASM({ var id = UTF8ToString($0); var name = UTF8ToString($1); - document.getElementById(id).addEventListener(name, function(evt) { + var element = document.getElementById(id); + if (element) element.addEventListener(name, function(evt) { emp.Callback($2, evt); }); }, widget_id.c_str(), event_name.c_str(), fun_id); diff --git a/include/emp/web/Table.hpp b/include/emp/web/Table.hpp index 3abff2fc9..b9eb9e307 100644 --- a/include/emp/web/Table.hpp +++ b/include/emp/web/Table.hpp @@ -221,9 +221,11 @@ namespace web { parent_id = UTF8ToString($0); child_id = UTF8ToString($1); let parent = document.getElementById(parent_id); - let span = document.createElement('span'); - span.id = child_id; - parent.appendChild(span); + if (parent) { + let span = document.createElement('span'); + span.id = child_id; + parent.appendChild(span); + } }, cell_id.c_str(), in.GetID().c_str()); // Now that the new widget has some place to hook in, activate it! diff --git a/include/emp/web/TextFeed.hpp b/include/emp/web/TextFeed.hpp index 10862d6af..73ae1c779 100644 --- a/include/emp/web/TextFeed.hpp +++ b/include/emp/web/TextFeed.hpp @@ -135,7 +135,7 @@ namespace web { MAIN_THREAD_ASYNC_EM_ASM({ var elementId = UTF8ToString($0); // Convert UTF-8 encoded string to JavaScript string var targetElement = document.getElementById(elementId); - targetElement.removeChild(targetElement.firstChild); + if (targetElement) targetElement.removeChild(targetElement.firstChild); }, id.c_str() ); } diff --git a/include/emp/web/Widget.hpp b/include/emp/web/Widget.hpp index a058978d4..1131145ad 100644 --- a/include/emp/web/Widget.hpp +++ b/include/emp/web/Widget.hpp @@ -384,7 +384,8 @@ namespace web { MAIN_THREAD_EM_ASM({ var widget_id = UTF8ToString($0); var out_html = UTF8ToString($1); - document.getElementById(widget_id).outerHTML = out_html; + var element = document.getElementById(widget_id); + if (element) element.outerHTML = out_html; }, id.c_str(), ss.str().c_str()); // If active update style, trigger JS, and recurse to children! @@ -498,7 +499,9 @@ namespace web { emp_assert(GetID() != ""); // Must have a name! return MAIN_THREAD_EM_ASM_DOUBLE({ var id = UTF8ToString($0); - return document.getElementById(id).clientWidth; + var element = document.getElementById(id); + if (!element) return -1.0; // Check if element exists + return element.clientWidth; }, GetID().c_str()); } double Widget::GetHeight(){ @@ -506,7 +509,9 @@ namespace web { emp_assert(GetID() != ""); // Must have a name! return MAIN_THREAD_EM_ASM_DOUBLE({ var id = UTF8ToString($0); - return document.getElementById(id).clientHeight; + var element = document.getElementById(id); + if (!element) return -1.0; // Check if element exists + return element.clientHeight; }, GetID().c_str()); } double Widget::GetInnerWidth(){ @@ -544,7 +549,9 @@ namespace web { emp_assert(GetID() != ""); // Must have a name! return MAIN_THREAD_EM_ASM_DOUBLE({ var id = UTF8ToString($0); - return document.getElementById(id).offsetWidth; + var element = document.getElementById(id); + if (!element) return 0.0; // Check if element exists + return element.offsetWidth; }, GetID().c_str()); } double Widget::GetOuterHeight(){ @@ -552,7 +559,9 @@ namespace web { emp_assert(GetID() != ""); // Must have a name! return MAIN_THREAD_EM_ASM_DOUBLE({ var id = UTF8ToString($0); - return document.getElementById(id).offsetHeight; + var element = document.getElementById(id); + if (!element) return 0.0; // Check if element exists + return element.offsetHeight; }, GetID().c_str()); }