javascript 自由trial.js

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javascript 自由trial.js相关的知识,希望对你有一定的参考价值。

window.addEventListener('load', function() {
  var REFERRER_COOKIE = '_rbn_referral',
    noop = function() {};

  var $freeTrialForm = document.querySelector('.hbspt-form > form');

  if ($freeTrialForm) {
    console.log(`We have the free trial form`);

    $freeTrialForm.addEventListener('hsvalidatedsubmit', function(event) {
      console.log(`We are attempting to submit`);

      var calendar,
        company,
        email,
        contactSubmit,
        context,
        name,
        payload,
        referrer,
        rooms,
        phone,
        _this = this,
        goog_report_conversion = window.goog_report_conversion || noop;

      ga = window.ga || noop;
      ga('send', 'event', 'Free Trial', 'Clicked Submit', 'Office interest');

      email = $freeTrialForm.querySelector('input[name="email"]').value;
      phone = $freeTrialForm.querySelector('input[name="phone"]').value;
      name =
        $freeTrialForm.querySelector('input[name="firstname"]').value +
        ' ' +
        $freeTrialForm.querySelector('input[name="lastname"]').value;
      calendar = $freeTrialForm.querySelector('select[name="calendar_system"]')
        .value;
      company = $freeTrialForm.querySelector('input[name="company"]').value;
      rooms = $freeTrialForm.querySelector('select[name="number_of_rooms"]')
        .value;
      demoInterest = $freeTrialForm.querySelector(
        'input[name="form_next_step"]'
      ).checked;
      referrer = Cookies.get(REFERRER_COOKIE);

      payload = new FormData();

      // @TODO [ct] convert into formdata
      payload.append('name', name);
      payload.append('calendar', calendar);
      payload.append('company', company);
      payload.append('phone', phone);
      payload.append('email', email);
      payload.append('rooms', rooms);
      payload.append('demoInterest', demoInterest);
      payload.append('referrer', referrer);

      post('/slackbot-free-trial', payload)
        .then(function(response) {
          goog_report_conversion();
          ga(
            'send',
            'event',
            'Free Trial',
            'Submission Received',
            'Office interest'
          );

          window.location =
            'https://dashboard.robinpowered.com/register?name=' +
            encodeURIComponent(name) +
            '&email=' +
            encodeURIComponent(email);
        })
        .catch(function(error) {
          console.error(error);
        })
        .then(function(response) {
          document.querySelector('.free-trial-redirect').style.display =
            'block';
        });
    });
  }
});

function post(url, payload) {
  return new Promise(function(resolve, reject) {
    var xhr = new XMLHttpRequest();
    xhr.open('POST', url);

    xhr.onload = function() {
      // This is called even on 404 etc
      // so check the status
      if (xhr.status == 200) {
        // Resolve the promise with the response text
        resolve();
      } else {
        // Otherwise reject with the status text
        // which will hopefully be a meaningful error
        reject(Error(xhr.statusText));
      }
    };

    // Handle network errors
    xhr.onerror = function() {
      reject(Error('Network Error'));
    };

    // Make the request
    xhr.send(payload);
  });
}

以上是关于javascript 自由trial.js的主要内容,如果未能解决你的问题,请参考以下文章

html+javascript实现广告窗自由浮动

浏览器关闭或禁用javascript,实现自由复制

JavaScript模拟自由落体

深入理解javascript原型和闭包(14)——从自由变量到作用域链

通过JavaScript自由切换iframe

JavaScript 闭包