Difference between revisions of "Resources/Using HTML CSS and Javascript"

Line 5: Line 5:
 
<h1>Using HTML, CSS, and Javascript on Your Team Wiki</h1>
 
<h1>Using HTML, CSS, and Javascript on Your Team Wiki</h1>
 
<p>All the iGEM web sites are built on top of MediaWiki, which allows groups of people to create and edit pages easily. <br>
 
<p>All the iGEM web sites are built on top of MediaWiki, which allows groups of people to create and edit pages easily. <br>
In this page you will find: <a href="#HTML_versus_MediaWiki">HTML versus MediaWiki markup</a>, <a href="link_and_script"> how to use link and script tags to load CSS and Javascript</a>,  </p>
+
In this page you will find: <a href="#HTML_versus_MediaWiki">HTML versus MediaWiki markup</a>, <a href="link_and_script"> how to use link and script tags to load CSS and Javascript</a>, <a href="#inline_CSS_and_JS"> how to use inline CSS and Javascript </a>,  </p>
 
</div>
 
</div>
  
Line 12: Line 12:
 
<div class="clear extra_space" id="HTML_versus_MediaWiki"></div>
 
<div class="clear extra_space" id="HTML_versus_MediaWiki"></div>
 
<div class="line_divider"></div>
 
<div class="line_divider"></div>
 
+
<div class="clear extra_space"></div>
 +
 
<div class="column half_size">
 
<div class="column half_size">
 
<h3>HTML versus MediaWiki markup</h3>
 
<h3>HTML versus MediaWiki markup</h3>
Line 102: Line 103:
 
<div class="highlight gray">
 
<div class="highlight gray">
 
<p>&#60;html> <br><br>
 
<p>&#60;html> <br><br>
&#60;link rel="stylesheet" type="text/css" <br>href="https://2019.igem.org/wiki/index.php?title=<b>Template:TeamName/CSS</b>&action=raw&ctype=text/css" /><br><br>
+
&#60;link rel="stylesheet" type="text/css" href="https://2019.igem.org/wiki/index.php?title<b>Template:TeamName/CSS</b>&action=raw&ctype=text/css" /><br><br>
  
 
&#60;script type="text/javascript" src="https://2019.igem.org/wiki/index.php?title=<b>Template:TeamName/JS</b>&action=raw&ctype=text/javascript"&#60;/script><br><br>
 
&#60;script type="text/javascript" src="https://2019.igem.org/wiki/index.php?title=<b>Template:TeamName/JS</b>&action=raw&ctype=text/javascript"&#60;/script><br><br>
Line 118: Line 119:
 
<!-------------------------------------------------------------------------------------------------------------------->
 
<!-------------------------------------------------------------------------------------------------------------------->
  
 +
<div class="clear extra_space"></div>
 +
<div class="line_divider"></div>
 +
<div class="clear extra_space" id="inline_CSS_and_JS"></div>
  
<div class="clear"></div>
 
  
<div class="column half_size">
+
<div class="column half_size">
<pre>
+
<h3>How to make "inline" CSS and Javascript</h3>
&lt;html&gt;
+
&lt;style type="text/css"&gt;
+
h2 {
+
color: red;
+
}
+
  
/* ... more CSS here ... */
+
<p>You can include CSS or Javascript within <code>&lt;style&gt;</code> or <code>&lt;script&gt;</code> tags in your HTML. It is customary to put CSS styling at the beginning of your page (before the main content), and Javascript code at the beginning or the end.</p>
&lt;/style&gt;
+
 +
<div class="clear extra_space"></div>
 +
 +
<div class="highlight gray post_item">
 +
    <div class="details">
 +
        <span class="icon note"></span>
 +
        <div class="title"> Note </div>
 +
</div>
 +
<p>If you use inline Javascript, <b>you cannot use the &amp;&amp; operator</b>. When the page is rendered, any &amp; will be encoded to &amp;amp;. For short scripts, you can get around this problem by using nested <code>if</code> statements instead of &amp;&amp;. However, you should use &lt;script&gt; tags to include any substantial scripts.</p>
 +
</div>
 +
 +
</div>
  
&lt;script type="text/javascript"&gt;
 
var foo = 3;
 
// ... more Javascript here ...
 
&lt;/script&gt;
 
  
&lt;!-- Main content of the page starts here --&gt;
+
<div class="column half_size">
 +
<div class="highlight gray post_item">
  
&lt;h2&gt;This text will be red&lt;/h2&gt;
+
<p> &#60;html><br>
 +
&#60;style type="text/css"> <br>
 +
/*Make text red and bold*/ <br>
 +
.red_text { <br>
 +
color: #f42434; <br>
 +
font-weight:bold; <br>
 +
}<br>
 +
&#60;/style><br>
 +
 +
&#60;script type="text/javascript"><br>
 +
$(document).ready(function() { <br>
 +
$("#alert").addClass("red_text"); <br>
 +
});</br>
 +
&#60;/script><br>
 +
&#60;/html><br>
 +
 +
</p>
  
&lt;p&gt;This text will be the normal text color&lt;/p&gt;
+
</div>
 +
</div>
  
&lt;!-- Main content of the page ends here --&gt;
+
<!-------------------------------------------------------------------------------------------------------------------->
 
+
&lt;script type="text/javascript"&gt;
+
var a = 1;
+
var b = "apple";
+
/// ... more Javascript here ...
+
&lt;/script&gt;
+
</pre>
+
</div>
+
  
 +
<div class="clear extra_space"></div>
 +
<div class="line_divider"></div>
 +
<div class="clear extra_space" id="inline_CSS_and_JS"></div>
  
 
<div class="column half_size">
 
<h3>How to make "inline" CSS and Javascript</h3>
 
 
<p>You can include CSS or Javascript within <code>&lt;style&gt;</code> or <code>&lt;script&gt;</code> tags in your HTML. It is customary to put CSS styling at the beginning of your page (before the main content), and Javascript code at the beginning or the end.</p>
 
</div>
 
 
<div class="column half_size">
 
<h5>NOTE </h5>
 
<p>If you use inline Javascript, <b>you cannot use the &amp;&amp; operator</b>. When the page is rendered, any &amp; will be encoded to &amp;amp;. For short scripts, you can get around this problem by using nested <code>if</code> statements instead of &amp;&amp;. However, you should use &lt;script&gt; tags to include any substantial scripts.</p>
 
</div>
 
 
<div class="clear"></div>
 
 
 
<!-------------------------------------------------------------------------------------------------------------------->
 
  
  

Revision as of 21:30, 10 January 2019

Loading...

Using HTML, CSS, and Javascript on Your Team Wiki

All the iGEM web sites are built on top of MediaWiki, which allows groups of people to create and edit pages easily.
In this page you will find: HTML versus MediaWiki markup, how to use link and script tags to load CSS and Javascript, how to use inline CSS and Javascript ,

HTML versus MediaWiki markup

All the iGEM web sites are built on top of MediaWiki ⇗, which allows groups of people to create and edit pages easily.

MediaWiki has its own markup ⇗ for formatting pages, creating links, etc. However, using HTML allows more creativity and flexibility. iGEM's websites allow any HTML tags. Simply enclose your HTML inside <html> tags within the page text.

You can mix wiki markup and HTML on the same page. Just make sure all the HTML is inside <html> tags, and all the wiki markup is outside <html> tags.

Example:

This example uses makes text bold and italic, first using html and then wiki markup.

<html>
<p> <b> <i> Example text </i> </b> </p>
</html>

''''' Example text '''''

Note

Using HTML in MediaWiki pages will cause the final page (as fully rendered by MediaWiki) to be noncompliant with strict HTML formatting specifications. This is basically inevitable, and is an acceptable trade-off for the added flexibility in wiki design that you get by using HTML. Most modern web browsers will interpret pages in "quirks" mode, which tries to "do the right thing" with HTML that is not strictly compliant.

Using <link> and <script> tags to include CSS and Javascript

To use this method, first, create a page in your team name space. In this example we will use: "Template:TeamName/CSS" for style and "Template:TeamName/JS" for code. Below are examples of css styling and javascript code.

2019.igem.org/Template:TeamName/CSS

/*Make text red and bold*/
.red_text {
color: #f42434;
font-weight:bold;
}

2019.igem.org/Template:TeamName/JS

$(document).ready(function() {

$("#alert").addClass("red_text");

});

Make sure to enter your enter your CSS or Javascript code without any surrounding <html> tags.

This method for calling code is also used on the iGEM websites, for example in: 2019.igem.org/Sitemap. The style can be found in: HQ:Sitemap.css and the javascript is in: HQ:Sitemap.js

Once the pages are ready, include these pages using <link> tags for CSS, and <script> tags for Javascript:

<html>

<link rel="stylesheet" type="text/css" href="https://2019.igem.org/wiki/index.php?titleTemplate:TeamName/CSS&action=raw&ctype=text/css" />

<script type="text/javascript" src="https://2019.igem.org/wiki/index.php?title=Template:TeamName/JS&action=raw&ctype=text/javascript"</script>

</html>

Strictly speaking, this is an incorrect use of the <link> tag. But as stated previously, using HTML on MediaWiki will already result in not-quite-compliant pages that still render correctly in most browsers. It is not really worthwhile to worry about this.

How to make "inline" CSS and Javascript

You can include CSS or Javascript within <style> or <script> tags in your HTML. It is customary to put CSS styling at the beginning of your page (before the main content), and Javascript code at the beginning or the end.

Note

If you use inline Javascript, you cannot use the && operator. When the page is rendered, any & will be encoded to &amp;. For short scripts, you can get around this problem by using nested if statements instead of &&. However, you should use <script> tags to include any substantial scripts.

<html>
<style type="text/css">
/*Make text red and bold*/
.red_text {
color: #f42434;
font-weight:bold;
}
</style>
<script type="text/javascript">
$(document).ready(function() {
$("#alert").addClass("red_text");
});
</script>
</html>

How to use templates including CSS and Javascript

If you want to use the same styles or scripts on several different pages, it is useful to write them in one place and then "include" them on your individual pages. One way you can do this is to use MediaWiki's Template function.

Create a page whose name starts with "Template:YourTeamName". Inside <html> tags, put the material you want to include:



2019.igem.org/Template:YourTeamName/CSS
<html>
<style type="text/css">
h2 {
color: red;
}
</style>
</html>

You can include any HTML content this way, not just CSS and Javascript. This is useful for creating things like a common header, menu bar, and footer for the pages on your wiki.

If you edit a page, there will be a list of which templates are used on that page, below the Save/Preview buttons.



You can also see which pages use your template. Go to the template, and then look in the top menu for "Wiki Tools" → "What Links Here". This is useful if you want to change or delete a template -- you can check if any important pages are using it.

Then, use MediaWiki's syntax to include the template on another page, outside the <html> tags:



2019.igem.org/Team:YourTeamName/Project
{{YourTeamName/CSS}}
<html>

<h2>Our Project</h2>
<p>Beginning of the page about our project.</p>

</html>
{{YourTeamName/Templae_in_the_middle}}
<html>

<p>End of the page about our project.</p>

</html>
{{YourTeamName/Javascript}}
Note

If you make a template containing Javascript, you cannot use the && operator. When the page is included via the {{ ... }} syntax, any & will be encoded to &amp;. For short scripts, you can get around this problem by using nested if statements instead of &&. However, you should use <script> tags to include any substantial scripts.