Actions

Difference between revisions of "JDocument/addStyleSheet"

From Joomla! Documentation

(Replaced JS function: MIME, example section + added real example)
m (Simplified example.)
(3 intermediate revisions by 2 users not shown)
Line 1: Line 1:
 
Adds the definition of a linked external stylesheet to the document.  Duplicates are ignored.
 
Adds the definition of a linked external stylesheet to the document.  Duplicates are ignored.
 
===Syntax===
 
===Syntax===
void addStyleSheet( $url, $type, $media, $attribs )
+
''void'' addStyleSheet( $url, $type, $media, $attribs )
  
 
where:
 
where:
Line 30: Line 30:
 
|
 
|
 
|}
 
|}
===Example===
+
===Example 1===
 
To add a link to some style sheet at a specific URL, you could use:
 
To add a link to some style sheet at a specific URL, you could use:
 
<source lang="php">
 
<source lang="php">
 
$doc =& JFactory::getDocument();
 
$doc =& JFactory::getDocument();
$doc->addStyleSheet( “http://www.example.com/css/mystylesheet.css” );
+
$doc->addStyleSheet( 'http://www.example.com/css/mystylesheet.css' );
 
</source>
 
</source>
 
+
How this is rendered depends on the document type.  If the document type is HTML then this code will produce following link in the HTML HEAD section:
Example code will produce following link:
+
<source lang="html4strict">
<source lang="javascript">
+
 
<link rel="stylesheet" href="http://www.example.com/css/mystylesheet.css" type="text/css" />
 
<link rel="stylesheet" href="http://www.example.com/css/mystylesheet.css" type="text/css" />
 
</source>
 
</source>
  
 +
===Example 2===
 +
From a template, you can add a link to a style sheet using a relative URL, like this:
 +
<source lang="php">
 +
$this->addStyleSheet( 'templates/' . $this->template . '/css/mystylesheet.css' );
 +
</source>
 +
The <code>$this</code> object will be an object of type [[JDocumentHTML]] when called from within a template.
 
===See also===
 
===See also===
 
* [http://api.joomla.org/Joomla-Framework/Document/JDocument.html#addStyleSheet JDocument->addStyleSheet on api.joomla.org]
 
* [http://api.joomla.org/Joomla-Framework/Document/JDocument.html#addStyleSheet JDocument->addStyleSheet on api.joomla.org]

Revision as of 19:01, 15 February 2009

Adds the definition of a linked external stylesheet to the document. Duplicates are ignored.

Contents

Syntax

void addStyleSheet( $url, $type, $media, $attribs )

where:

Argument Data type Description Default
$url string URL of stylesheet.
$type string MIME type of script. 'text/css'
$media string Media type that the stylesheet applies to. null
$attribs array Array of attributes.

Example 1

To add a link to some style sheet at a specific URL, you could use:

$doc =& JFactory::getDocument();
$doc->addStyleSheet( 'http://www.example.com/css/mystylesheet.css' );

How this is rendered depends on the document type. If the document type is HTML then this code will produce following link in the HTML HEAD section:

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

Example 2

From a template, you can add a link to a style sheet using a relative URL, like this:

$this->addStyleSheet( 'templates/' . $this->template . '/css/mystylesheet.css' );

The $this object will be an object of type JDocumentHTML when called from within a template.

See also