1.index.html文件meta标签添加属性
<meta name="viewport" content="width=device-width,initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=no" viewport-fit=cover >
2.提前main.html处理逻辑再跳转到index.html页
<script>function timestamp(url) {const getTimestamp = new Date().getTime()if (url.indexOf('?') > -1) {url = url + '×tamp=' + getTimestamp + '&loginType=' + loginType} else {url = url + '?timestamp=' + getTimestamp + '&loginType=' + loginType}return url}const getUrlParam = name => {const reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)')const value = window.location.search.substr(1).match(reg)if (value != null) {return decodeURIComponent(value[2])}return null}const loginType = getUrlParam('loginType')const url = './index.html'const newUrl = timestamp(url, loginType)window.open(newUrl, '_self')
</script>
3.动态加载文件
3.1 封装动态加载、移除文件方法
export function loadCSS(url, isCache = true) {let element = document.createElement('link')element.setAttribute('rel', 'stylesheet')element.setAttribute('type', 'text/css')if (isCache) {element.setAttribute('href', url + '?t=' + new Date().getTime())} else {element.setAttribute('href', url)}document.getElementsByTagName('head')[0].append(element)
}
export function loadJS(jsUrl, callback, isCache = true) {const script = document.createElement('script')const head = document.headscript.type = 'text/JavaScript'if (isCache) {script.src = jsUrl + '?t=' + new Date().getTime()} else {script.src = jsUrl}if (script.addEventListener) {script.addEventListener('load', callback, false)}document.getElementsByTagName('head')[0].append(script)
}
export const removeFileJSandCSS = (filename, filetype) => {const targetElement = filetype === 'js' ? 'script' : filetype === 'css' ? 'link' : 'none'const targetAttr = filetype === 'js' ? 'src' : filetype === 'css' ? 'href' : 'none'const allSuspects = document.getElementsByTagName(targetElement)for (let i = allSuspects.length; i >= 0; i--) {if (allSuspects[i] &&allSuspects[i].getAttribute(targetAttr) != null &&allSuspects[i].getAttribute(targetAttr).indexOf(filename) != -1)allSuspects[i].parentNode.removeChild(allSuspects[i])}
}
3.2 页面引入
removeFileJSandCSS('nationality.js', 'js')
loadCSS('./jQuery/jquery-ui.min.css')
loadJS('./jQuery/jquery-ui.min.js', () => {})