一、背景
遇到一个嵌入第三方APP的需求。页面间的跳转需调用特定方法进行跳转,跳转地址要求完整路径的。顾自己整理下。
比如:完整地址http://192.168.155.32:8020/testdemo/pages/home/index.html
二、代码参考
JS
// 获取当前http路径
var obj = window.location;
var patehName = obj.pathname;
console.log(patehName);// /testdemo/pages/home/index.html
// h5
var contextPath = '',
url = '';
//普通浏览器
contextPath = patehName.substr(0, patehName.lastIndexOf("/pages/") + 1);
console.log(contextPath);// /testdemo/
var basePath = obj.protocol + "//" + obj.host + contextPath + 'pages/';
console.log(basePath);// http://192.168.155.32:8020/testdemo/pages/
则最终地址:
var url = basePath + 'home/index.html ';