简体   繁体   English

如何防止javascript和css搞砸了?

[英]How to prevent javascript and css messing up?

My company has a website that serves the same app to several clients so every client has his own html templates and we can't edit anything of that. 我的公司有一个网站,为几个客户提供相同的应用程序,因此每个客户都有自己的html模板,我们无法编辑任何内容。

We developed a beutiful Web 2.0 app with lots of jQuery that works great, it's a web toy fully enjoyable but we have to integrate it with the horrible and buggy stuff from the clients (like a code sandwich) and as it always gave us problems of all kind we decided to use an iFrame for our app and a little js to refresh its height (the parent and the iFrame content come from the same domain). 我们开发了一个漂亮的Web 2.0应用程序,其中包含大量jQuery,效果很好,它是一个非常有趣的网络玩具,但我们必须将它与来自客户端的可怕和错误的东西(如代码三明治)整合在一起,因为它总是给我们带来问题。所有类型我们决定使用iFrame为我们的应用程序和一些小js刷新其高度(父和iFrame内容来自相同的域)。 The iFrame save us from all the mess and weirdness but it's sad to use something like that. 内嵌框架拯救我们从所有的混乱和怪异,但它的悲伤使用类似的东西。

Do you know a better solution for this? 你知道更好的解决方案吗?

If your aim is to be independent of third-party CSS and JS, you must indeed use a separate document such as an iframe. 如果您的目标是独立于第三方CSS和JS,则必须使用单独的文档,例如iframe。 The customer's code doesn't have to be 'bad' for you to get unexpected interactions between the content you're using and the stylesheet rules and actions they're working with. 客户的代码对于您在使用的内容与他们正在使用的样式表规则和操作之间获得意外交互并不一定是“不好”。

Anyhow it would be rude for you as a guest script of someone else's page to be dragging an large, intrusive framework like jQuery into their window. 无论如何,对于你作为其他人页面的客户脚本来说,将jQuery这样的大型侵入式框架拖到他们的窗口中会很粗鲁。 Even if you used noConflict there are still many possible interactions if the script on their page doesn't expect to see it there. 即使您使用noConflict ,如果其页面上的脚本不希望在那里看到它,仍然存在许多可能的交互。

好吧,我们终于在一个匿名函数中解决了这个封装的Javascript代码(就像一些像Google这样的大家伙一样),对于CSS我们使用了很多子选择器,在某些情况下还使用了“!important”规则。

With regards to CSS you could use a "reset" stylesheet like http://meyerweb.com/eric/tools/css/reset/ . 关于CSS,您可以使用“重置”样式表,如http://meyerweb.com/eric/tools/css/reset/

If all the content from the webapp is inside some tag then apply the reset stylesheet (modified to your needs with font-family, font-size etc.). 如果来自webapp的所有内容都在某个标签内,则应用重置样式表(根据您的需要使用font-family,font-size等进行修改)。 Then your customers font-family,font-size,margins etc. don't have an effect to your webapp. 那么您的客户font-family,font-size,margins等对您的webapp没有影响。

To get every CSS property you need you could inspect your webapp (in your enviroment, not the clients) with Chrome, and see the Computed style with Show inherited on. 要获得您需要的每个CSS属性,您可以使用Chrome检查您的webapp(在您的环境中,而不是客户端),并查看Show inherited on的Computed style There you can see the properties the customers might set but there is a lot of the properties which you don't need. 在那里,您可以看到客户可能设置的属性,但有许多您不需要的属性。 And some may even be wrong - such as dimensions so watch out and combine it with the non-computed style. 有些甚至可能是错误的 - 例如尺寸,所以要小心,并将其与非计算风格相结合。

And ensure you call everything you do with your explicit content tag like below so you don't mess up the customers html/css: 并确保您使用下面的显式内容标记调用您所做的一切,这样您就不会搞砸客户html / css:

#YourWebAppMainTag a {}

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

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