我有个页面弹窗,是在 index.html
页面加载的 js 代码里通过 jquery get 方法实现的,大概代码如下:
$.get('./addOrEdit.html?id=100', function (data) {
var info = data, oppoBox = $('#oppoBox'), title = $.trim(obj.attr('atitle'));
if (info.length) {
$('#oppoInfo').html(info);
$('#oppoBg').css({'height': $(document).outerHeight() + 'px'});
oppoBox.css({'width': $(document).outerWidth() * 11 / 18 + 'px'});
var top = $(window).height() / 18, height = $(window).height() - top * 2;
oppoBox.css({
'left': ($(document).outerWidth() - oppoBox.outerWidth()) / 2 + 'px',
'top': top + 'px',
'height': height + 'px',
'overflow': 'hidden'
});
$('#oppoTitle').text(title);
$('#oppoInfo').css({
'height': height - parseInt($('#toolBar').css('lineHeight')) - parseInt($('#oppoInfo').css('paddingTop')) - parseInt($('#oppoInfo').css('paddingBottom')) + 'px'
});
$('#oppoBg,#oppoBox').removeClass('hide');
}
});
现在我期望是在 addOrEdit.html?id=100
页面加载的 js 文件获取到当前的 id
的参数值,但是现在获取到的当前页面地址一直是 index.html
,请问大神这个该怎么办?小弟跪谢。
请大佬不要告诉直接写死 100
就行了,我这里为了方便看直接写成了 addOrEdit.html?id=100
,实际的页面这个 url
是个变量表示的。
addOrEdit.html?id=100 页面加载的 js 文件为什么能够执行,是因为调用了 html() 方法,该方法底层是使用 window.eval() 来执行脚本的,window.eval 是一种 eval 的间接调用方式,会在全局环境下执行代码,也就是 js 文件执行的环境就是当前页面 index.html 下的全局环境。所以只需要把 id 值存储为一个全局变量就可以被获取了。