
解决jQuery get加载弹窗后获取弹窗自身URL地址的问题
使用jQuery的$.get()方法加载弹窗页面时,直接获取URL会返回加载弹窗的页面地址,而非弹窗本身的地址。 为了解决这个问题,我们可以通过将弹窗URL中的参数作为全局变量来传递。由于window.eval()的特性,弹窗加载的JS文件会在弹窗页面所属的全局环境中执行,因此只需将参数存储为全局变量即可在弹窗内访问。
以下是一个改进的代码示例,演示如何将URL参数存储为全局变量,并在弹窗加载的JS文件中访问:
<code class="javascript">$.get('./addOrEdit.html?id=100', function (data) {
// 将URL参数存储为全局变量
window.id = 100; // 根据实际URL参数修改
let 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'});
let 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');
// 在弹窗加载后,访问全局变量获取URL参数
console.log("弹窗ID:", window.id); // 此处可以根据需要使用window.id
// 根据需要在此处添加获取完整URL的逻辑,例如:
// let currentUrl = window.location.href;
// console.log("弹窗完整URL:", currentUrl);
}
});</code>通过将id参数存储在window.id全局变量中,addOrEdit.html页面加载的JS文件就可以访问到这个参数值,从而间接获取弹窗的URL信息。 请注意将window.id = 100;中的100替换为实际的URL参数值,并根据需要添加获取完整URL的逻辑。 这种方法避免了直接依赖window.location.href获取URL的局限性。
以上就是jQuery get加载弹窗后,如何获取弹窗自身的URL地址?的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号