简体   繁体   English

JavaScript try catch语句

[英]Javascript try catch statement

I'm analyzing some code on a website and I came across the following anonymous function followed by a try catch statement. 我正在分析网站上的一些代码,遇到了以下匿名函数,后接try catch语句。 I'm just wondering what the try catch statement is doing at the end there. 我只是想知道try catch语句到底在做什么。 Is it pre-loading the url so thats it loads more quickly then the anonymous function goes? 它是否预加载了url,这样它才能比匿名函数运行得更快? Also, whats the point is it's not catching any errors. 另外,关键是它没有捕获任何错误。

(function() {
        var fired = false;
        bsnPop.add("http://www.someurl.com", {
            under: !noPopunder,
            newTab: false,
            forceUnder: true,
            shouldFire: function() {
                return !fired;
            },
            cookieExpires: -1,
            afterOpen: function(url) {
                createCookie();
                fired = true;
                doSecondPop();
            }
        });
    })();
    try {
        var hint = document.createElement("link");
        hint.rel = "dns-prefetch";
        hint.href = "http://www.someurl.com";
        document.head.appendChild(hint);
        var hint = document.createElement("link");
        hint.rel = "preconnect";
        hint.href = "http://www.someurl.com";
        document.head.appendChild(hint);
    } catch (e) {}

With reference to the link types list on MDN, "dns-prefetch" and "preconnect" are listed as experimental. 参考MDN上的链接类型列表,“ dns-prefetch”和“ preconnect”被列为实验性的。 They do not appear in the list of "rel" values for link types of link elements in HTML5 它们不会出现在HTML5中链接元素的链接类型的“相关”值列表中

So the code is using experimental technology on the web which might throw an error in some browsers. 因此,代码使用的是网络上的实验技术,这可能会在某些浏览器中引发错误。 To prevent stopping the application and logging an exception on the console, the code is placed in a try block with a catch block that ignores the error. 为了防止停止应用程序并在控制台上记录异常,将代码放置在try块中,而catch块将忽略该错误。


In answer to question details, the anonymous function in the IIFE is invoked and passes an object containing parameters and callbacks in a call to bsnPop.add . 在回答问题的细节,在IIFE匿名函数被调用,并传递包含于呼叫参数和回调对象bsnPop.add It does not appear to create a popup window at this stage. 在此阶段,它似乎没有创建弹出窗口。

Next code within the try block attempts to speed up access to the web site by requesting DNS lookup of the website's name in advance, and to open a connection to the site before attempting to retrieve content. try块中的下一个代码try通过请求DNS名称预先进行DNS查找来加快对网站的访问,并在尝试检索内容之前打开与该网站的连接。

The code is placed in the try block to accommodate the possibility of a browser throwing an exception if the requested operations are not supported. 如果不支持请求的操作,则将代码放置在try块中,以容纳浏览器抛出异常的可能性。 The application does not consider lack of support an error and wants to continue anyway. 该应用程序不认为缺少支持是错误,并且无论如何都希望继续。

The end result is that if dns-prefetch or preconnect are supported the browser can take the hint and perform the operations. 最终结果是,如果支持dns-prefetchpreconnect ,则浏览器可以获取提示并执行操作。 If they are not supported any error generated is ignored and code continues at the next statement - connecting to the website later will have to proceed at normal speed. 如果不支持它们,则将忽略所生成的任何错误,并在下一条语句中继续执行代码-稍后连接到网站将必须以正常速度进行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM