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.
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
TestSite/first
, then from first I can refer to its parent TestSite folder by
../
What do I use to refer to TestSite.com from first.TestSite.com? Thanks.
It's called a subdomain.
a) A hard-coded reference is a URL that contains the instance name in the URL (e.g. na1.salesforce.com). Replace these hard-coded references with generic, non-instance specific or relative URLs (e.g. login.salesforce.com or <mydomain>.
It ties the security of your site to the security of the other site. Example: Consider the following script tag. <script src="http://www.example.com/js/fancyWidget.js"></script>
DNS names can contain only alphabetical characters (A-Z), numeric characters (0-9), the minus sign (-), and the period (.). Period characters are allowed only when they are used to delimit the components of domain style names.
There's no way using pure relative links. You have to program it as a string manipulation.
Something like:
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);
EDIT: Shorter version using regex:
var baseDomain = host.match(/[^.]*\.[^.]*$/)[0]
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.
If you want to keep your relative links, you can use the base
element.
[The base element's href] attribute specifies an absolute URI that acts as the base URI for resolving relative URIs.
So, add a base
element, specify the href
you'd like, and all the relative URIs on the page now use the URI you've specified as the base URI.
<head>
<base href="http://testsite.com" />
</head>
That's all you need. However, if you'd like to make things a little cleaner, you can pull that URI from the web.config using System.Configuration.ConfigurationManager.AppSettings
. Here's an aspx snippet:
<head>
<base href="<%= ConfigurationManager.AppSettings["rootdomain"] %>" />
</head>
And the web.config:
<configuration>
<appSettings>
<add name="rootdomain" value="http://testsite.com" />
</appSettings>
</configuration>
This method allows you to affect many elements from one and its value can be driven from the web.config.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With