const url = 'https://gateway.dev.cdlsxd.cn/h5/?param=activity-swtest#/' function getUrlPathName() { // const url = window.location.href let code = 'activity' if (url.indexOf('#/') !== -1) { let strR = '' const str = url.indexOf('#/') !== -1 ? url.split('#/')[1] : url.split('/')[url.split('/').length - 1] if (str.indexOf('/') === -1) { strR = str || '' } else { strR = str.split('/')[0] } if (strR.indexOf('-') === -1) { code = 'activity' } else { code = strR.split('-')[0] strR = strR.split('-')[1] } return { codeS: code, activity_code: strR } } else if (url.indexOf('?') !== -1) { let strR = '' strR = url.split('=')[1] code = strR.split('-')[0] return { codeS: code, activity_code: strR.split('-')[1] } } else { const str = url.split('/') const strP = str[str.length - 1] let strR = '' if (strP.indexOf('-') === -1) { strR = strP code = 'activity' } else { code = strP.split('-')[0] strR = strP.split('-')[1] } return { codeS: code, activity_code: strR } } } console.log(getUrlPathName(url))