简体   繁体   English

如何为 Google Analytics JS 代码等用户创建外部 javascript 代码?

[英]How can I create an external javascript code for users like the Google Analytics JS Code?

I'm starting a project where I need to create an external javascript code for users to input into their website (like they do with the Google Analytics Snippet).我正在开始一个项目,我需要创建一个外部 javascript 代码供用户输入他们的网站(就像他们使用 Google Analytics Snippet 一样)。 I know a little bit of javascript by way of JQuery, but I'm not sure how I would begin.我通过 JQuery 了解一点 javascript,但我不确定我将如何开始。

I'm nervous I'm using the wrong terminology to find what I'm looking for.我很紧张我使用错误的术语来查找我正在寻找的内容。

From a technical standpoint, this is accomplished the same was as including a static JS file in your own website - through the use of a script tag.从技术角度来看,这与在您自己的网站中包含 static JS 文件一样 - 通过使用脚本标签。 The only difference being that the source for your JS file is on a different domain than your website.唯一的区别是您的 JS 文件的来源与您的网站位于不同的域中。

As for the actual contents of the JS file, that is much harder to give advice on since your question is so oblique.至于 JS 文件的实际内容,由于您的问题是如此倾斜,因此很难给出建议。 The only thing I can suggest is that我唯一能建议的是

1) You cannot depend on the site including any dependent libraries, such as jQuery 1)您不能依赖包含任何依赖库的站点,例如 jQuery

2) Pollute the global namespace as little as possible. 2) 尽可能少地污染全局命名空间。 Ideally it will only be a single object in the global namespace, with all your code living underneath.理想情况下,它只是全局命名空间中的一个 object,所有代码都在下面。

Just provide them a js file to include in theur headers.只需为他们提供一个 js 文件以包含在他们的标题中。 Be sure to not modify the global scop because you dont know what they are doing.确保不要修改全局范围,因为您不知道他们在做什么。 Usually, this is ensured using a closure:通常,这是使用闭包来确保的:

(function(){
    // Put your code here

    window.yourLibName = someObject;
})();

The only code they can use in the global scope is yourLibName, everything else si kept within your colsure.他们可以在全局 scope 中使用的唯一代码是 yourLibName,其他所有内容都保留在您的规定范围内。

Then, you can provide them a code sample to call your tool from their webpage.然后,您可以向他们提供代码示例,以便从他们的网页中调用您的工具。 Something like:就像是:

<script type="text/javascript"><!--
yourLibName(someParameters);
//-->
</script>

This method will lead to something clean and usable on most websites.这种方法将在大多数网站上产生干净且可用的东西。

Also, avoid using libraries like jQuery in such a solution, as it has a risk of messing up the client's javascript, especially if he is using a different version of jQuery or another lib like mootoools.此外,避免在这样的解决方案中使用 jQuery 之类的库,因为它有弄乱客户的 javascript 的风险,特别是如果他使用的是不同版本的 jQuery 或其他库,如 mooto。

Some basic principle一些基本原则

  1. make your code as small as possible.使您的代码尽可能小。

  2. Do not depend on library like Jquery.不要依赖像 Jquery 这样的库。 It would make conflict on third party's site easily.很容易在第三方网站上产生冲突。

  3. make your script load asynchronous使您的脚本异步加载

  4. leave some interface so others can do sth.留下一些界面,以便其他人可以做某事。 custom setting upon your code.根据您的代码进行自定义设置。

  5. Do not add too many global variables.不要添加过多的全局变量。 Try to use namespace to write your code.尝试使用命名空间来编写代码。

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

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