Trying to parse some data that is included within a tag in the HTML using JavaScript. Specifically I’d like to retrieve the value for ’email’ as in the script below, with the expected output being "[email protected]"
<script type = 'text/javascript" nonce>
var _hsq = window._hsq = window._hsq || [];
_hsq.push(["identify",{
email: "[email protected]",
firstname: "yourfirstname",
lastname: "yourlastname",
}]);
</script>
Tried using document.querySelector and document.getElementsByTagName to isolate the HTML element, but as there is no unique ID or class for the script tag I’m unable to isolate the specific script in question. I also tried reading from the "_hsq" variable but it didn’t return anything.
2
Answers
You can get the last element of
_hsq
with_hsq.slice(-1)
, then pull out the email with something like_hsq.slice(-1)[0][1].email
.The data you’re looking for is being attached to the
window
object, so you can retrieve it from there. You don’t need to be looking in the DOM.(It’s not the cleanest data format ever invented; if that’s code you’re able to change, you might be better off with a simple object or flat array of objects, instead of an array of arrays each containing a name and an object.)