当外部JavaScript或CSS资源无法加载时检测并记录日志 [英] Detect and log when external JavaScript or CSS resources fail to load

查看:209
本文介绍了当外部JavaScript或CSS资源无法加载时检测并记录日志的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有多个< head> 引用外部js和css资源。大多数情况下,这些都是用于第三方分析等。有时(无意识),这些资源无法加载,通常导致浏览器超时。当外部JavaScript或CSS资源无法加载时,是否可以检测并登录服务器?

I have multiple <head> references to external js and css resources. Mostly, these are for things like third party analytics, etc. From time to time (anecdotally), these resources fail to load, often resulting in browser timeouts. Is it possible to detect and log on the server when external JavaScript or CSS resources fail to load?

我正在考虑一些类型的延迟加载机制,将调用一个特殊的URL来记录此失败。有任何建议吗?

I was considering some type of lazy loading mechanism that when, upon failure, a special URL would be called to log this failure. Any suggestions out there?

我认为会发生什么:


  1. 用户已成功访问我们的页面和服务器端进程并提供页面

  1. The user hits our page and the server side processes successfully and serves the page

在客户端,HTML标头尝试连接到我们的第三方集成合作伙伴,通常由以http://www.someothercompany.com ...开头的javascript包含。

On the client side, the HTML header tries to connect to our 3rd party integration partners, usually by a javascript include that starts with "http://www.someothercompany.com...".

另一家公司无法处理我们的负载或处理不当的正常运行时间,因此连接失败。

The other company cannot handle our load or has shitty up-time, and so the connection fails.

用户看到的通用IE页面未找到,而不是我们的服务器中的页面。

The user sees a generic IE Page Not Found, not one from our server.

即使我的网站启动,一切正常运行,只是因为这一个呼叫到第三方服务器失败,一个在HTML页眉,我们得到一个完整的失败启动。

So even though my site was up and everything else is running fine, just because this one call out to the third party servers failed, one in the HTML page header, we get a whole failure to launch.

推荐答案

如果你的应用程序/页面依赖于JS,你可以加载JS的内容,我知道它是混乱。当使用JS加载这些文件时,您可以使用回调,只允许加载内容的功能,而不必担心未加载的内容。

If your app/page is dependent on JS, you can load the content with JS, I know it's confusing. When loading these with JS, you can have callbacks that allow you to only have the functionality of the loaded content and not have to worry about what you didn't load.

var script = document.createElement("script");
script.type = "text/javascript";
script.src = 'http://domain.com/somefile.js';
script.onload = CallBackForAfterFileLoaded;
document.body.appendChild(script);
function CallBackForAfterFileLoaded (e) {
//Do your magic here...
}

我通常有一个更复杂的JS和文件数组的依赖彼此,如果他们不加载,那么我有一个错误状态。

I usually have this be a bit more complex by having arrays of JS and files that are dependent on each other, and if they don't load then I have an error state.

我忘了提及,显然我只是展示如何创建一个JS标签,你必须为你想加载的其他类型的文件创建自己的方法。

I forgot to mention, obviously I am just showing how to create a JS tag, you would have to create your own method for the other types of files you want to load.

希望也许会帮助,欢呼

这篇关于当外部JavaScript或CSS资源无法加载时检测并记录日志的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆