简体   繁体   English

使用javascript的cdn前缀相对路径

[英]Prefix relative paths for cdn with javascript

I have a simple website with relative references to css files and javascript files in the header. 我有一个简单的网站,标题中包含对css文件和javascript文件的相对引用。 Is there a way to expand these to absolute url's and prefix them with "cdn." 有没有一种方法可以将它们扩展为绝对URL,并以“ cdn”作为前缀。 automatically on page load? 自动加载页面?

Here is part of my current head: 这是我目前的头脑的一部分:

<link rel="stylesheet" type="text/css" href="css/default.css">
<link rel="stylesheet" type="text/css" href="css/the-tooltip.css" />
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script src="js/utf8.js" type="text/javascript"></script>
<script src="js/sha1.js" type="text/javascript"></script>
<script src="js/validatious.js" type="text/javascript"></script>

What i need in the end is 我到底需要什么

<link rel="stylesheet" type="text/css" href="http://cdn.mydomain.com/css/default.css">
<link rel="stylesheet" type="text/css" href="http://cdn.mydomain.com/css/the-tooltip.css" />
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script src="http://cdn.mydomain.com/js/utf8.js" type="text/javascript"></script>
<script src="http://cdn.mydomain.com/js/sha1.js" type="text/javascript"></script>
<script src="http://cdn.mydomain.com/js/validatious.js" type="text/javascript"></script>

Here is how you can add prefix to all script elements: 您可以通过以下方法为所有脚本元素添加前缀:

function appendPrefix(prefix) {
  var scripts = document.getElementsByTagName('script'),
      links =  document.getElementsByTagName('link'),
      foreach = Array.prototype.forEach;
  foreach.call(scripts, function (s) {
    if (s.src && (/(http|https)/).test(s.src)) {
       s.src = prefix + s.src;
    }
  });
  foreach.call(links, function (l) {
    if (s.src && (/(http|https)/).test(l.href)) {
       l.href = prefix + l.href;
    }
  });
}

appendPrefix('http://stackoverflow.com/');

The script is pure JavaScript and works for both script and link tags. 该脚本是纯JavaScript,适用于scriptlink标记。 It will append the prefix only to those link and script tags which does not start with http:// . 它将仅将前缀附加到不以http://开头的那些linkscript标记。

You can do redirect requests /css/* or /js/* to cdn.example.com/css/* or cdn.example.com/js/* in configuration files of web server (eg, .htaccess for Apache-server) and browser get necessary files. 您可以在Web服务器的配置文件(例如,Apache服务器的.htaccess) cdn.example.com/css/*请求/css/*/js/*重定向到cdn.example.com/css/*cdn.example.com/js/* 。浏览器将获取必要的文件。 This solution will make the load on your web server (not as much as send css or js files), so it is best to write path to cdn manually. 此解决方案将使您的Web服务器上的负载增加(不如发送CSS或JS文件那么多),因此最好手动将路径写入cdn。

It appears like JS isn't the best solution. 看来JS不是最佳解决方案。 I solved the problem using PHP on the serverside instead. 我在服务器端使用PHP解决了问题。 I defined a global $prefix variable and appended that to a dynamically generated hostname and path to get the absolute URL and still maintain portability of the script. 我定义了一个全局$ prefix变量,并将其附加到动态生成的主机名和路径上,以获取绝对URL并仍保持脚本的可移植性。

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

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