JavaScript 全局封装
- 只有路径 (适用于带http之类的服务):
- 需要在<head>首先引入
- function getRootPath() {
var curPath = window.location.href;
//获取主机地址之后的目录,如: /a/b/c.html
var pathName = window.location.pathname;
// a/b/c.html
var a = pathName.substring(1);
var b = a.indexOf('/');
// a
var c = pathName.substring(1, b + 1);
var d = curPath.indexOf(pathName);
var pos = d + 1 + b;
//http://127.0.0.1:8020/a
var url = window.location.href.substring(0, pos);
var pre='/js/bootstrap-3.3.7/';
return url+pre;
}
//http://127.0.0.1:8020/%E5%95%86%E5%9F%8E/js/bootstrap-3.3.7/bootstrapglobal.js
alert(getRootPath()+"js/jquery.min.js");
document.write('<link href="' + getRootPath() + 'css/bootstrap.min.css" rel="stylesheet" type="text/css"/>');
document.write('<script src="' + getRootPath() + 'js/jquery.min.js"></script>');
document.write('<script src="' + getRootPath() + 'js/bootstrap.min.js"></script>')
- 指定路径(本地会读取 错误):
- 页面 globalData.setUserInfo(msg.uid); globalData.getCurUid();
原文地址:https://www.cnblogs.com/ruiyuan/p/11246306.html