document.addEventListener("DOMContentLoaded", function () { //以下变量统一使用var,禁用let,兼容IE var customerVisitUrl = "/api/customerVisit/"; var customerVisitData = {}; var requestPostType = "POST"; function ajax(method, url, data, success) { var xhr = new XMLHttpRequest(); xhr.open(method, url, true); xhr.setRequestHeader("Content-Type", "application/json"); xhr.onreadystatechange = function () { if (xhr.readyState === 4) { if (xhr.status === 200) { success(xhr.responseText); } else { console.log("Error: " + xhr.status); } } }; xhr.send(data); } // 获取当前网页的协议+域名 function getPageBaseUrl() { var baseURL = ""; if (!window.location.origin) { // 兼容IE,IE11版本下location.origin为undefined window.location.origin = window.location.protocol + "//" + window.location.hostname + (window.location.port ? ":" + window.location.port : ""); } else { baseURL = window.location.origin; } return baseURL; } if ( navigator.userAgent.match(/Mobi/i) || navigator.userAgent.match(/Android/i) || navigator.userAgent.match(/iPhone/i) ) { customerVisitData.device_port = "2"; } else { customerVisitData.device_port = "1"; } customerVisitData.url = window.location.href; customerVisitData.domain = getPageBaseUrl() + "/"; customerVisitData.referrer_url = document.referrer; ajax( requestPostType, customerVisitUrl, JSON.stringify(customerVisitData), function (data) {} ); });