如何动态地设置基本标签? [英] How to dynamically set the base tag?

查看:172
本文介绍了如何动态地设置基本标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在使用的骨干路由,mod_rewrite的,requirejs。该应用程序是一个文件夹中,而不是在Web根,所以相对的文件夹引用所需要的图片,CSS和JS文件(如果我们可以用绝对的文件夹中的文件将加载)。

We are using backbone routing, mod_rewrite, requirejs. The app is in a folder, not on the web root, so relative folder references are required for images,css, and js files (if we could use absolute folders the files would load).

在访问与结尾的斜线没有正确的js和css文件加载的路线,除非有合适的碱标签在标题中。像这样:

When accessing a route with a trailing slash none of the js and css files loads correctly unless there is an appropriate base tag set in the header. Like so:

<base href="//localhost/myapp/" /> 

此解决方案。问题是,我们需要variablize基地标记,以便我们能有code的开发和生产的版本。但要加载的变量不会工作的js文件没有基本标记。

This solution works. The problem is we need to variablize the base tag so that we can have dev and production versions of the code. But to load a js file with the variable wont work without a base tag.

只是要确保我做的标准修复骨干。修复可选斜杠(/):

Just to be sure I did the standard fixes for backbone. Fix optional slash (/):

routes: {
  'faq(/)':'jumpToText',
  'register(/)':'jumpToForm',
},

和历史设置root

Backbone.history.start({pushState: true, root: "//localhost/myapp/");

这个问题似乎是一个无法解决的问题的mod_rewrite。所以最终的想法是动态设置基地标记。

The problem appears to be an unresolvable mod_rewrite issue. So the final thought is to dynamically set the base tag.

推荐答案

我们最终使用的JavaScript解析出location.href的值。这个包裹code在头一个脚本标签:

We ultimately used JavaScript to parse the value out of location.href . Wrap this code in a script tag in the head:

document.write("<base href="+'//'+document.location.host +'/'+ location.href.split('/')[3]+'/'+" />");

而在routes.js(解析出来的URI)

And did the same in routes.js (parsing out the uri)

Backbone.history.start({pushState: true, root: "/"+location.href.split('/')[3]});

这篇关于如何动态地设置基本标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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