我正在使用Google Minify成功压缩js文件。
我需要能够传递一个变量(如$_GET
变量)。
<script type="text/javascript" src="includes/min/?g=site_searchresults_js"></script>
有没有办法用上面的代码传递变量?
答案 0 :(得分:0)
this article中描述了在将脚本添加到文档中时确定传递的查询字符串参数的过程。它归结为这种代码:
var scripts = document.getElementsByTagName('script');
var myScript = scripts[ scripts.length - 1 ];
var queryString = myScript.src.replace(/^[^\?]+\??/,'');
var params = parseQuery( queryString );
function parseQuery ( query ) {
var Params = new Object ();
if ( ! query ) return Params; // return empty object
var Pairs = query.split(/[;&]/);
for ( var i = 0; i < Pairs.length; i++ ) {
var KeyVal = Pairs[i].split('=');
if ( ! KeyVal || KeyVal.length != 2 ) continue;
var key = unescape( KeyVal[0] );
var val = unescape( KeyVal[1] );
val = val.replace(/\+/g, ' ');
Params[key] = val;
}
return Params;
}