问题标签 [base-tag]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
5 回答
14667 浏览

javascript - React Router's HashRouter redirects to tag url

I have non-SPA server-side application with React application that is limited to current page, /some/static/page. The application has <base href="/"> in <head> on all pages and relies on it, this cannot be changed.

Here is basic example with React 16, React Router 4 and <HashRouter>:

All routes can be disabled for testing purposes, but this doesn't change the behaviour.

Here is create-react-app project that shows the problem. The steps to replicate it are:

  • npm i
  • npm start
  • navigate to http://localhost:3000/some/static/page

HashRouter is clearly affected by <base>. It redirects from /some/static/page to /#/ on initialization, while I expect it to be /some/static/page#/ or /some/static/page/#/ (works as intended only in IE 11).

There's a quick splash of Root component before it redirects to /#/.

It redirects to /foo/#/ in case of <base href="/foo">, and it redirects to /some/static/page/#/ when <base> tag is removed.

The problem affects Chrome and Firefox (recent versions) but not Internet Explorer (IE 11).

Why is <HashRouter> affected by <base>? It's used here exactly because it isn't supposed to affect location path, only hash.

How can this be fixed?

0 投票
3 回答
1645 浏览

html - html标签的空href是什么意思?

假设我的页面 URL 是:http://example.com/path/to/dir/index.html

我通过以下方式代理此页面:http://proxyserver.com/path/to/dir/index.html。同样,我希望页面中的所有相对 URL 都由proxyserver.com而不是example.com. 什么应该是正确的<base>href 值?

我想要页面上的相对 URL,例如

  • newfile.html解决http://proxyserver.com/path/to/dir/newfile.html
  • /newfile.html解决http://proxyserver.com/newfile.html
  • #hash解决http://proxyserver.com/path/to/dir/file.html#hash

在页面中设置<base href="" />正确地完成了这项工作,但它有一些含义吗?跨浏览器是否有不同的解释?空href值实际上是什么意思?它适用于所有框架,如 Angular 吗?

我听说<base>标签对于 Angular 应用程序初始化是强制性的,因此删除<base>标签可能不起作用。

注意:该网站可能已经包含一些<base>我总是想覆盖的标签。

我也尝试过<base href="/" />,但它会解析相对 URL

  • newfile.htmlhttp://proxyserver.com/newfile.html
  • #hashhttp://proxyserver.com/#hash 是错误的。

非常感谢任何帮助。

0 投票
1 回答
3556 浏览

html - 具有根相对 url 的HTML5标记

请帮助我了解<base>标签的 href 属性的功能。

我有一个网站托管在http://goodsite.org/bob. 其他网站托管在http://goodsite.org/others.

我计划将我的应用程序移至http://bettersite.org/bob.

为了支持这一点,我的标题中有一个基本网址。 <base href="/bob/">.

我的链接看起来像这样:<a href="thatonething.img">That one thing</a>

正如希望的那样,我看到了预期的链接(例如"http://goodsite.org/bob/thatonething.img"

问题:

<base>标签是否普遍支持根相对网址(即href="/bob/")?

这似乎在我的测试中有效,但我在阅读规范时遇到了更多麻烦。更一般地说,我想知道我是否滥用了这个标签。

0 投票
1 回答
158 浏览

javascript - 如何让 testcafe 尊重 ` ` 标签

我的应用程序在某个时候将<base>标签注入网站,然后创建一个<script>带有相对的新标签src

例如,设置<base href="http://localhost:8080/chapters/de/auth/">和注入<script src="build.js" type="text/javascript" />导致浏览器从http://localhost:8080/chapters/de/auth/build.js.

在 testcafe 中运行相同的应用程序时,结果略有不同。插入的基是 <base href-hammerhead-stored-value="http://localhost:8080/chapters/de/auth/" href="http://localhost:1337/Lj6CFUfNF/http://localhost:8080/chapters/de/auth/">和脚本是<script src-hammerhead-stored-value="build.js" src="http://localhost:1337/Lj6CFUfNF!s!utf-8/http://localhost:8080/account/build.js" type="text/javascript" />

突然之前的相对URLbuild.js变成了和http://localhost:8080/account/build.js里面设置的不一致<base>

知道如何解决这个问题吗?

我在跑步

测试咖啡馆:1.1.0