Contents |
The CFBASE tag creates an absolute URL that includes the Java web application's context path. This absolute URL can then be used as a base for resolving relative URLs on a CFML page.
<cfbase target="optional target">
| Attribute | Req/Opt | Default | Description |
|---|---|---|---|
| target | optional | N/A | The target to be applied to all relative URLs on the page. |
CFBASE is used to address the problem of relative URLs on CFML pages that begin with a slash (/). Since OpenBD is typically deployed as a Java web application with a context path, relative URLs on CFML pages that begin with / (e.g. hyperlinks, the src attribute of <img> tags, etc.) are interpreted by the browser as being relative to the server root, not the context path. This results in the incorrect resolution of relative URLs and the loss of the convenience of using relative URLs that begin with /. (Note that when using relative URLs that begin with a / on backend code this problem does not arise because they will always be resolved as relative to the context path.)
By using CFBASE, URLs that are relative to the context path can be created on CFML pages without using / at the beginning of the URL. For example, the following <img> tag will resolve as relative to the context path because the CFBASE tag has been placed in the <head> section of the CFML page:
<html>
<head>
<cfbase>
</head>
<body>
<img src="images/myimage.jpg">
</body>
</html>
If the context path in this case is "myapp", then the resolved image tag would be as follows:
<img src="/myapp/images/myimage.jpg">
The optional TARGET attribute will globally assign a target in the exact same manner as the HTML <base> tag's TARGET attribute. (See http://www.w3schools.com/TAGS/tag_base.asp for more information.)
If global application of URLs relative to the context path is not desired, the CGI.CONTEXT_PATH variable may be used to prepend specific URLs with the appropriate context path:
<img src="#CGI.CONTEXT_PATH#/images/myimage.jpg">
Apply the context path to all URLs within a CFML page and set the target to _blank:
<html>
<head>
<cfbase target="_blank">
</head>
<body>
<!-- this image URL will have the context path prepended and will have a target of _blank -->
<img src="images/myimage.jpg">
</body>
</html>
Prepend the context path to a specific URL:
<cfoutput> <img src="#CGI.CONTEXT_PATH#/images/myimage.jpg"> </cfoutput>