javascript HTML 基本 URL 和链接

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4374478/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 11:12:41  来源:igfitidea点击:

HTML Base URL and links

phpjavascriptjqueryhtmlurl

提问by CRISHK Corporation

I have the following problem:

我有以下问题:

The URL is http://www.myhomeurl.com/application1/and the base is:

URL 是http://www.myhomeurl.com/application1/,基础是:

<base href="http://www.myhomeurl.com/"/>

All resources like images, css and javascript will be at:

所有资源,如图像、css 和 javascript 都将位于:

http://www.myhomeurl.com/css/myfile.css
http://www.myhomeurl.com/js/myscript.js
http://www.myhomeurl.com/images/img.jpg

BUT, the link will be at "application1", for example:

但是,链接将位于“application1”,例如:

http://www.myhomeurl.com/application1/page1.html
http://www.myhomeurl.com/application1/page2.html
http://www.myhomeurl.com/application1/page3.html

The question is: How to apply base URL for resources (like css, js, etc) and apply the base/application1 for page links?

问题是:如何为资源(如 css、js 等)应用 base URL 并为页面链接应用 base/application1?

Here is a problem when I have:

这是一个问题,当我有:

<a href="page1.html">Click me!</a>

When the user clicks this the page will going to:

当用户点击它时,页面将转到:

http://www.myhomeurl.com/page1.html

http://www.myhomeurl.com/page1.html

and not to:

而不是:

http://www.myhomeurl.com/application1/page1.html

http://www.myhomeurl.com/application1/page1.html

回答by George Marshall

You could use a base tag and change it so all urls can be relative to the applications base.

您可以使用基本标记并对其进行更改,以便所有 url 都可以与应用程序基础相关。

<!DOCTYPE HTML>
<html>
  <head>
    <base href="http://www.myhomeurl.com/application1/" />
  </head>
  <body>
    <!-- content -->
  </body>
</html>

Additional information can be found at http://www.w3schools.com/tags/tag_base.asp

更多信息可以在http://www.w3schools.com/tags/tag_base.asp找到

edit:w3c spec on base tag http://www.w3.org/TR/html4/struct/links.html#h-12.4also illustrates how to pull images from other locations.

编辑:基本标签上的 w3c 规范http://www.w3.org/TR/html4/struct/links.html#h-12.4还说明了如何从其他位置提取图像。

回答by danieelito

Change like this on your resources

像这样改变你的资源

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

Not like

不喜欢

<link href="http://www.myhomeurl.com/css/myfile.css" rel="stylesheet" type="text/css" />

And your base it looks like this

你的基地看起来像这样

<base href="http://www.myhomeurl.com/application1/"/>

回答by Pradeep Singh

Use this varriable in link HTTP_SERVER

在链接 HTTP_SERVER 中使用此变量

define('HTTP_SERVER', 'http://www.myhomeurl.com/application1/');

<a href="<?php echo HTTP_SERVER?>myurlpage/page1.html">Click me!</a>

回答by kobe

you should have something like this

你应该有这样的东西

root 

   root/css
   root/js

   root/files/html1.htm
   root/files/htmml2.htm

root will be nothing but /// your website name

root 将只是 /// 您的网站名称

http://www.myhomeurl.com

回答by Shakti Singh

Define two constant one for your css, js resources and one for links and use them across the application:-

为您的 css、js 资源定义两个常量,一个为链接定义一个常量,并在整个应用程序中使用它们:-

define('RESOURCE_URL','http://www.myhomeurl.com/');
define('LINK_URL','http://www.myhomeurl.com/application1');

so that if any change in base url you can look up to these constants only.

这样,如果基本 url 发生任何变化,您只能查找这些常量。

回答by Erenor Paz

I'm a little late for this question, but I can see none of the answers really do what (I think) you need.

我回答这个问题有点晚了,但我看不出所有答案都能真正满足(我认为)您的需要。

I suggest you to use (as others have already said) a Base Url like this:

我建议您使用(正如其他人已经说过的)这样的 Base Url:

<base href="http://www.myhomeurl.com/application1/" />

This way your links will work in the intended way. Then, when you add resources, you only have to go up to the parent directory, like the following:

这样,您的链接将按预期方式工作。然后,当你添加资源时,你只需要上到父目录,如下所示:

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

Or

或者

<style type="text/css">
  @import url(../css/myfile.css);
</style>

Note the ../, telling the browser to exit from "application1" directory, going up one step in the file structure.

注意../, 告诉浏览器退出“application1”目录,在文件结构中向上一步。

Hope this helps :)

希望这可以帮助 :)

回答by WGroleau

I would start all link herd with /and the top directory of the URI. That makes them relative to the domain. Then just put the protocol and domain in the base.

我会用 / 和 URI 的顶级目录开始所有链接群。这使它们与域相关。然后只需将协议和域放在基础中。

回答by Michael Freeman Swag and Beats

when using base url you should not put www.in it.

使用基本网址时,您不应将www.其放入。

Next if your url is mysite.comand your app is located at mysite.com/appthen you set your base to be mysite.com/appwhen you use your example info.htmlit will look like mysite.com/app/info.html.

接下来,如果您的 url 是mysite.com并且您的应用程序位于,mysite.com/app那么您mysite.com/app在使用示例info.html时将base 设置为mysite.com/app/info.html.

You can also use ../app/info.htmland it will look like mysite.com/app/info.html

你也可以使用../app/info.html它看起来像mysite.com/app/info.html