Iframve v2 - localhost

by TokenEx Support

HTML

<p id="data"></p>

<!--update the src of this iframe to the HTPURL you obtained from the https://test-htp.tokenex.com/api/v1 call -->
<iframe id="tokenExIframe" width="30%" height="20%" scrolling="no" frameborder="0" src="http://localhost:31036/iframe/v2/d357b16b1a0f4f2ebae616add449aa10"></iframe>
<br/>

<button type="button" id="bthTokenize">Tokenize</button>
<button type="button" id="bthValidate">Validate</button>
<button type="button" id="btnReset">Reset</button>
<button type="button" id="btnPretty">Enable Pretty</button>
<button type="button" id="btnFocus">focus</button>
<button type="button" id="btnblur">blur</button>

JavaScript

document.getElementById('bthValidate').onclick = validate;
document.getElementById('bthTokenize').onclick = tokenize;
document.getElementById('btnReset').onclick = reset;
document.getElementById('btnPretty').onclick = pretty;
document.getElementById('btnFocus').onclick = focus;
document.getElementById('btnblur').onclick = blur;
var iframe = document.getElementById("tokenExIframe");

//add event listener for postmessage
if (window.addEventListener) {
  addEventListener("message", listener, false)
} else {
  attachEvent("onmessage", listener)
}

//send the tokenize command to the iframe
function pretty() {
  iframe.contentWindow.postMessage('enablePrettyFormat', '*');
}
function focus() {
  iframe.contentWindow.postMessage('focus', '*');
}
function blur() {
  iframe.contentWindow.postMessage('blur', '*');
}
//enable pretty formated pan
function tokenize() {
  iframe.contentWindow.postMessage('tokenize', '*');
}
//send the validate command to the iframe
function validate() {
  
  iframe.contentWindow.postMessage('validate', '*');
}
//send the reset command to the iframe
function reset() {
  var iframe = document.getElementById("tokenExIframe");
  iframe.contentWindow.postMessage('reset', '*');
}

//handle the message from the iframe
function listener(event) {
  if (event.origin === 'http://localhost:31036') {
    var message = JSON.parse(event.data);
    switch (message.event) {
      case 'focus':
        if (message.data.value) {
          //field has focus
        } else {
          //field does not have focus(blur)
        }
      case 'change':
        // field changed
        break;
      case 'validation':
        if (!message.data.isValid) {
          //field failed validation
        } else {
          //validation valid!
        }
        break;
      case 'post':
        if (!message.data.success) {
          // use message.data.error 
        } else {
          //get token! message.data.token
        }
        break;
    }
    log("iframe message:" +...