请问在通过 jquery get 加载的页面内如何获取当前的 url 地址?

作者站长头像
站长
· 阅读数 21

我有个页面弹窗,是在 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 是个变量表示的。

回复
1个回答
avatar
test
2024-06-23

addOrEdit.html?id=100 页面加载的 js 文件为什么能够执行,是因为调用了 html() 方法,该方法底层是使用 window.eval() 来执行脚本的,window.eval 是一种 eval 的间接调用方式,会在全局环境下执行代码,也就是 js 文件执行的环境就是当前页面 index.html 下的全局环境。所以只需要把 id 值存储为一个全局变量就可以被获取了。

回复
likes
适合作为回答的
  • 经过验证的有效解决办法
  • 自己的经验指引,对解决问题有帮助
  • 遵循 Markdown 语法排版,代码语义正确
不该作为回答的
  • 询问内容细节或回复楼层
  • 与题目无关的内容
  • “赞”“顶”“同问”“看手册”“解决了没”等毫无意义的内容