Skip to content

Instantly share code, notes, and snippets.

@iMagesh
Last active August 29, 2015 14:17

Revisions

  1. iMagesh revised this gist Mar 24, 2015. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions testcl.js
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,7 @@
    var pageUrl = "http://something.com/gsm";
    var interactionId = "email-form"; //id of the product

    document.getElementByClass(interactionId).onsubmit = function AfterFunction(e){
    document.getElementsByClassName(interactionId).onsubmit = function AfterFunction(e){
    var xmlhttp;

    if (window.XMLHttpRequest) {
    @@ -15,7 +15,7 @@ document.getElementByClass(interactionId).onsubmit = function AfterFunction(e){
    xmlhttp.onreadystatechange = function() {
    if (xmlhttp.readyState == 4 ) {
    if(xmlhttp.status == 200){
    alert(JSON.stringify(customform(document.getElementByClass(interactionId))));
    alert(JSON.stringify(customform(document.getElementsByClassName(interactionId))));
    document.getElementById("myDiv").innerHTML = xmlhttp.responseText;
    }
    else if(xmlhttp.status == 400) {
  2. iMagesh revised this gist Mar 24, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion testcl.js
    Original file line number Diff line number Diff line change
    @@ -15,7 +15,7 @@ document.getElementByClass(interactionId).onsubmit = function AfterFunction(e){
    xmlhttp.onreadystatechange = function() {
    if (xmlhttp.readyState == 4 ) {
    if(xmlhttp.status == 200){
    alert(JSON.stringify(customform(document.getElementById(interactionId))));
    alert(JSON.stringify(customform(document.getElementByClass(interactionId))));
    document.getElementById("myDiv").innerHTML = xmlhttp.responseText;
    }
    else if(xmlhttp.status == 400) {
  3. iMagesh revised this gist Mar 24, 2015. 1 changed file with 2 additions and 3 deletions.
    5 changes: 2 additions & 3 deletions testcl.js
    Original file line number Diff line number Diff line change
    @@ -1,8 +1,7 @@

    var pageUrl = "http://something.com/gsm";
    var interactionId = "formSubmit"; //id of the product
    var interactionId = "email-form"; //id of the product

    document.getElementById(interactionId).onsubmit = function AfterFunction(e){
    document.getElementByClass(interactionId).onsubmit = function AfterFunction(e){
    var xmlhttp;

    if (window.XMLHttpRequest) {
  4. iMagesh revised this gist Mar 24, 2015. 1 changed file with 28 additions and 2 deletions.
    30 changes: 28 additions & 2 deletions testcl.js
    Original file line number Diff line number Diff line change
    @@ -1,8 +1,34 @@

    var pageUrl = "http://something.com/gsm";
    var interactionId = "formSubmit"; //id of the product

    document.getElementById(interactionId).onsubmit = function AfterFunction(e){
    alert(JSON.stringify(customform(document.getElementById(interactionId))));
    var xmlhttp;

    if (window.XMLHttpRequest) {
    // code for IE7+, Firefox, Chrome, Opera, Safari
    xmlhttp = new XMLHttpRequest();
    } else {
    // code for IE6, IE5
    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }

    xmlhttp.onreadystatechange = function() {
    if (xmlhttp.readyState == 4 ) {
    if(xmlhttp.status == 200){
    alert(JSON.stringify(customform(document.getElementById(interactionId))));
    document.getElementById("myDiv").innerHTML = xmlhttp.responseText;
    }
    else if(xmlhttp.status == 400) {
    alert('There was an error 400')
    }
    else {
    alert('something else other than 200 was returned')
    }
    }
    }

    //alert(JSON.stringify(customform(document.getElementById(interactionId))));
    }

    function customform(form) {
    @@ -70,4 +96,4 @@ function customform(form) {
    }
    }
    return q.join('&');
    }
    }
  5. iMagesh created this gist Mar 24, 2015.
    73 changes: 73 additions & 0 deletions testcl.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,73 @@
    var pageUrl = "http://something.com/gsm";
    var interactionId = "formSubmit"; //id of the product

    document.getElementById(interactionId).onsubmit = function AfterFunction(e){
    alert(JSON.stringify(customform(document.getElementById(interactionId))));
    }

    function customform(form) {
    var i, j, len, jLen, formElement, q = [];
    function addNameValue(name, value) {
    q.push(encodeURI(name) + '=' + encodeURI(value));
    }
    if (!form || !form.nodeName || form.nodeName.toLowerCase() !== 'form') {
    throw 'You must supply a form element';
    }
    for (i = 0, len = form.elements.length; i < len; i++) {
    formElement = form.elements[i];
    if (formElement.name === '' || formElement.disabled) {
    continue;
    }
    switch (formElement.nodeName.toLowerCase()) {
    case 'input':
    switch (formElement.type) {
    case 'text':
    case 'hidden':
    case 'password':
    case 'button': // Not submitted when submitting form manually, though jQuery does serialize this and it can be an HTML4 successful control
    case 'submit':
    addNameValue(formElement.name, formElement.value);
    break;
    case 'checkbox':
    case 'radio':
    if (formElement.checked) {
    addNameValue(formElement.name, formElement.value);
    }
    break;
    case 'file':
    // addNameValue(formElement.name, formElement.value); // Will work and part of HTML4 "successful controls", but not used in jQuery
    break;
    case 'reset':
    break;
    }
    break;
    case 'textarea':
    addNameValue(formElement.name, formElement.value);
    break;
    case 'select':
    switch (formElement.type) {
    case 'select-one':
    addNameValue(formElement.name, formElement.value);
    break;
    case 'select-multiple':
    for (j = 0, jLen = formElement.options.length; j < jLen; j++) {
    if (formElement.options[j].selected) {
    addNameValue(formElement.name, formElement.options[j].value);
    }
    }
    break;
    }
    break;
    case 'button': // jQuery does not submit these, though it is an HTML4 successful control
    switch (formElement.type) {
    case 'reset':
    case 'submit':
    case 'button':
    addNameValue(formElement.name, formElement.value);
    break;
    }
    break;
    }
    }
    return q.join('&');
    }