简体   繁体   English

如何使用Javascript仅为一个特定页面加载CSS文件?

[英]How to Javascript to load a CSS file only for one specific page?

I am working on a shopping cart on Netsuite coded with nested tables... I know it sucks. 我正在Netsuite上的购物车上用嵌套表编码,我知道这很糟糕。

Because I only have access to fields to customize the website I need a JS snippet to be able to load a specific CSS file for the shopping cart (to avoid styling the rest of the website) 因为我只能访问用于自定义网站的字段,所以我需要一个JS代码段才能为购物车加载特定的CSS文件(以避免样式化网站的其余部分)

I have never done that before, here is how I would start: 我以前从未做过,这是我的开始方式:

<script type="text/javascript">
var path = window.location.pathname;
if(path.indexOf('/Cart') !=-1 {
  // load css file
}
</script>

What code would do that for me? 什么代码可以为我做到?

Aside from closing your if(... with a ) , this should get you going: 除了用if(...来关闭if(... ) ,还可以帮助您:

var styles = document.createElement("link");
styles.rel = "stylesheet";
styles.type = "text/css";
styles.href = "file.css";  // your file's URL
document.getElementsByTagName("head")[0].appendChild(styles);

While testing the url does work there is a better way to add tab and category specific markup. 虽然测试url确实有效,但是有一种更好的方法来添加选项卡和类别特定的标记。

In your website theme in the "Additions to Head" section add a tag <TAB_ADDONS> and add that to the site builder Tags but with a blank body 在您的网站主题的“头部添加”部分中,添加标签<TAB_ADDONS>并将其添加到网站构建器标签中,但正文为空白

Then go to your list of tabs and find the shopping cart tab. 然后转到选项卡列表,找到“购物车”选项卡。 Edit it and select Tag Overrides find TAB_ADDONS and enter: 编辑它,然后选择“标记替代”以找到TAB_ADDONS并输入:

<link rel="stylesheet" type="text/css" href="file.css">

you'd use the root relative url for the href. 您将使用href的根相对URL。

This keeps your theme much cleaner and allows arbitrary code inclusions. 这样可以使您的主题更加整洁,并允许包含任意代码。

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

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