jQuery 未捕获的类型错误:无法读取未定义的属性 'safari'

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15011729/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 14:13:34  来源:igfitidea点击:

Uncaught TypeError: Cannot read property 'safari' of undefined

javascriptjquery

提问by user2043533

I have this jjavascript to resize iframes:

我有这个 jjavascript 来调整 iframe 的大小:

 $(function () {

            var iFrames = $('iframe');

            function iResize() {

                for (var i = 0, j = iFrames.length; i < j; i++) {
                    iFrames[i].style.height = iFrames[i].contentWindow.document.body.offsetHeight + 'px';
                }
            }

            if ($.browser.safari || $.browser.opera) {

                iFrames.load(function () {
                    setTimeout(iResize, 0);
                });

                for (var i = 0, j = iFrames.length; i < j; i++) {
                    var iSource = iFrames[i].src;
                    iFrames[i].src = '';
                    iFrames[i].src = iSource;
                }

            } else {
                iFrames.load(function () {
                    this.style.height = this.contentWindow.document.body.offsetHeight + 'px';
                });
            }

        });

In chrome, it has trouble here:

在 chrome 中,这里有问题:

 if ($.browser.safari || $.browser.opera) {

Is there any reason why I get this error? I am using the latest JQuery?

我收到此错误有什么原因吗?我正在使用最新的 JQuery?

Thanks

谢谢

回答by Michael Marr

You are probably using jQuery 1.9 or above, in which case $.browserwas officially removed after being deprecated since 1.3.

您可能正在使用 jQuery 1.9 或更高版本,在这种情况下$.browser,自 1.3 以来被弃用后已被正式删除。

You can use jQuery migratewhich will patch it, but it's better to move to a feature specific approach instead of browser specific approach. Modernizris great for this.

您可以使用jQuery migrate来修补它,但最好转向特定于功能的方法而不是特定于浏览器的方法。Modernizr非常适合这一点。

回答by Gokul Kav

jquery recommends against $.browser... use $.supportinstead..

jquery 建议不要$.browser...使用$.support代替...

if $.browser.safari(or opera or whatever your trying to access) doesn't exist it throws an error. check if its undefined

如果$.browser.safari(或歌剧或您尝试访问的任何内容)不存在,则会引发错误。检查它是否undefined

回答by Lindsay Branscombe

I noticed this issue today with a client who upgraded without telling me.

我今天在没有告诉我的情况下升级的客户注意到了这个问题。

The quick fix I issued (without using Modernizr which is probably a better way)

我发布的快速修复(不使用 Modernizr 这可能是更好的方法)

On the scrollTo.js file go to line 85 and make it this:

在 scrollTo.js 文件中,转到第 85 行并使其变为:

        var is_safari = navigator.userAgent.indexOf("Safari") > -1;

        return is_safari || doc.compatMode == 'BackCompat' ?

回答by Ulises

You could try checking the userAgent string:

您可以尝试检查 userAgent 字符串:

Chrome has both 'Chrome' and 'Safari' inside userAgent string. Safari has only 'Safari'.

Chrome 在 userAgent 字符串中同时包含“Chrome”和“Safari”。Safari 只有“Safari”。

Detect Safari using jQuery

使用 jQuery 检测 Safari