Uncaught TypeError: Cannot read property 'safa

2020-07-11 08:21发布

I have this jjavascript to resize iframes:

 $(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:

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

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

Thanks

4条回答
劫难
2楼-- · 2020-07-11 08:51

jquery recommends against $.browser... use $.support instead..

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

查看更多
劳资没心,怎么记你
3楼-- · 2020-07-11 09:00

You could try checking the userAgent string:

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

Detect Safari using jQuery

查看更多
淡お忘
4楼-- · 2020-07-11 09:01

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)

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

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

        return is_safari || doc.compatMode == 'BackCompat' ?
查看更多
等我变得足够好
5楼-- · 2020-07-11 09:03

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

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

查看更多
登录 后发表回答