Using <base href>The answer was to use <base href>. This tells the browser what URL relative links are relative to. So if I use: <base href="http://CoverYourASP.com/">
<a href="Hello.asp">Say hello</a> |
The link would always point to http://CoverYourASP.com/Hello.asp, no matter where this page was. Configuring <base href>Of course, hard-coding http://CoverYourASP.com/ into my code wouldn't help you or I very much. The <base href> needs to change when it's on the development server - in my case I want it to be http://localhost/cya/. This is accomplished with some code in utils/Init.asp, shown below: var sServer = '' + Request.ServerVariables ( 'SERVER_NAME' );
// if running securely, use https://
var sRoot = '' + Request.ServerVariables ( 'HTTPS' );
var sBaseServer = 'http' + ( sRoot == 'on' ? 's' : '' ) + '://' + sServer;
// if running on non-standard port, use it
var nPort = Request.ServerVariables ( 'SERVER_PORT' ) - 0;
if ( nPort != 80 )
sBaseServer += ':' + nPort;
// test which URL I am running from
for ( var i=0; i<sURLTest.length; i++ )
{
if ( -1 != sBaseServer.indexOf ( sURLTest [ i ] ) )
{
// apply the offset if one given
if ( sURLOffset [ i ].length )
sBaseServer += '/' + sURLOffset [ i ];
break;
}
}
Out ( '<base href="' + sBaseServer + '/" target="_top">' );
|
To start with I get the server name from Request.ServerVariables. Giving my servers as an example, I get either "CoverYourASP.com" or "localhost" from this. I then test to see if the page was requested with SSL security, and if so use https:// instead of http:// If the page was accessed using a different port (not the default port 80) then that is appended too. Part 3: The sURLTest array... |