如何引用主域,而无需硬编码它的名字? [英] How to refer to main domain without hard-coding its name?

查看:101
本文介绍了如何引用主域,而无需硬编码它的名字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个域名:TestSite.com。我创建若干子域这个网站,并把它们称作first.TestSite.com,second.TestSite.com等。

I have a domain name: TestSite.com. I create several subdomains for this site and refer to them as first.TestSite.com, second.TestSite.com, etc.

我如何参考TestSite.com的相对,而不必硬code它的名字在first.TestSite.com一个html或aspx文件?我的意思是(使用文件夹为例),如果我有一个文件夹,测试网站和子文件夹中第一个

How do I refer to TestSite.com relatively without having to hard code its name in an html or aspx file in first.TestSite.com? What I mean is (using folders as example) if I have a folder TestSite and a sub folder first

测试网站/第一个

,然后从第一我可以通过

, then from first I can refer to its parent TestSite folder by

../

我应该使用什么从first.TestSite.com指TestSite.com?谢谢你。

What do I use to refer to TestSite.com from first.TestSite.com? Thanks.

推荐答案

有没有办法使用纯相对链接。你必须把它作为一个字符串处理程序。

There's no way using pure relative links. You have to program it as a string manipulation.

是这样的:

var host = location.host;
var lastPeriod = host.lastIndexOf(".");
var remainder = host.substring(0, lastPeriod);
var afterSecondLastPeriod = remainder.lastIndexOf('.') + 1
var baseDomain = host.substring(afterSecondLastPeriod);
console.log(baseDomain);

编辑:较短的版本使用正则表达式:

Shorter version using regex:

var baseDomain = host.match(/[^.]*\.[^.]*$/)[0]

这是通用的,所以它总是返回的最后一部分。不管它是否 a.TestSite.com baTestSite.com 等,它将返回 TestSite.com

This is general, so it will always return the last part. Regardless of whether it's a.TestSite.com, b.a.TestSite.com, etc. it will return TestSite.com.

您必须修改它,如果这个假设是不正确的。

You will have to modify it if this assumption is not correct.

这篇关于如何引用主域,而无需硬编码它的名字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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