JavaScript QuickTips
PURPOSE
To show that I do actually use Javascript sometimes. Although I may not be as succesful in convincing someone because I am not updating this often!
Iterating over Objects
Copy the below code in console of your browser:
Check if Object is Empty
Copy the below code in browser console:
Log Form Data
You cannot directly log a formData object. However, you can use the entries property to iterate over formdata values. Copy the below code in browser:
How to check whether a substring exists in an array elements
You can use the includes
method
Comparing version values in Javascript
MutationObserver
- The user's email is given to me through a separate process
- I needed to auto fill email this email in Pre Chat form for Kommunicate Chat
- KommunicateChat loads externally as an iframe
First, we observe the whole body to get iframe
- we create a mutation observer object
- we observe the whole document body and set the childList tracking true
- we track only those mutations where a node is added
- once Kommunicate widget is found, we stop the observer and start tracking the widget itself
Then, we observe the whole iframe to get the email element
- we create a new mutation observer
- we start tracking with childList & subtree true
- we track only those mutations where a node is added
- if email element is found, we set the value