skip to Main Content

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


  1. 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.

    Login or Signup to reply.
  2. 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.

    var _hsq = window._hsq = window._hsq || [];
    
    _hsq.push(["identify",{
        email: "[email protected]",
        firstname: "yourfirstname",
        lastname: "yourlastname",
    }]);
    
    
    console.log(window._hsq[window._hsq.length-1][1].email)

    (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.)

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search