Skip to main content

Healthcare Systems Article Category

Medical Factors Which make it The most effective  just by Sejal Kakadiya  Medicine and health agencies with now consentrate on top quality, charge together with great satisfaction health of their solutions. These are typically that support beams which a lot of these agencies redefine the direction they trade. The following really results in thrilled clients.  How come Serious Treatment Direction Can be a Trend Inside Medicine and health Sector  just by Steven Orange  A whole lot of North american healthcare systems' options are generally about that direction together with procedure with serious circumstances. Direction with serious health conditions which include diabetes, excessive circulation demand, cardiovascular disease, together with hypothyroidism can be a vital component of easiest treatment healthcare provider's succeed. Inside standard product, that general practitioner spots someone on an automobile accident with treatment, inspects that condition ...

How to Create Table of Contents Using JavaScript


You can create dynamic table of contents for any HTML document using JavaScript which can show the list of headings from h1 to h6 with links to the headings and make easier to navigate through the document.

In this post I am going to describe about the steps to Create Table of Contents Using JavaScript and about the JavaScript codes required to create dynamic table of contents.

To create this set of JavaScript codes for creating table of contents, at first you have to know the different JavaScript concepts like selecting elements, document traversal, setting element attributes, setting innerHTML property, creating nodes and inserting then into the document.


Steps to Create Table of Contents Using JavaScript


At first create window.onload function that runs automatically when the document finishes loading as given below.

window.onload=function(){

function getSelectedText(){
if (window.getSelection)
return window.getSelection().toString()+"
"+document.URL;
else if (document.selection)
return document.selection.createRange().text+"
"+document.URL;
}
var toc=document.getElementById("TOC");
if(!toc) {
toc=document.createElement("div");
toc.id="TOC";
document.body.insertBefore(toc, document.body.firstChild);
}

When the above function runs, it first looks for a document element with an id of "TOC". If there is no such element it creates on at the start of the document.

Add the following codes to the function to find all <h1> through <h6> tags and sets them as headings.

var headings;
if (document.querySelectorAll)
headings=document.querySelectorAll("h1, h2, h3, h4, h5, h6");
else
headings=findHeadings(document.body, []);

Then recursively traverse the document body looking for headings using function: findHeadings(root, sects){  }

Initialize an array: var sectionNumber=[0,0,0,0,0,0]; that keeps track of section numbers and add section numbers to each section numbers to each section heading and wrap the headings in named anchors so that the TOC can link to them.

Write CSS code to style created table of contents. Where all entries have a class "TOCEntry" and the section headings from <h1> to <h6> have class name "TOCLevel1" to "TOCLevel6" and the section number inserted into headings have class "TOCSectNum".

Full CSS Code To Style Table of Contents


 Here is a full CSS code within <style> </style> tag to style table of contents.

<style>
#TOC {border:solid black 1px; margin:10px; padding:10px;}
.TOCEntry{font-family:sans-serief;}
.TOCEntry a{text-decoration:none;}
.TOCLevel1{font-size:17pt; font-weight:bold;}
.TOCLevel2{font-size:16pt; font-weight:bold;}
.TOCLevel3{font-size:15pt; font-weight:bold;}
.TOCLevel4{font-size:14pt; margin-left:.25in;}
.TOCSectNum{display:none;}
</style>

Full JavaScript Code To Create Table of Contents



Here is a full JavaScript Code To Create Table of Contents within <script> </script> tag.

<script>
window.onload=function(){

function getSelectedText(){
if (window.getSelection)
return window.getSelection().toString()+"<br/>"+document.URL;
else if (document.selection)
return document.selection.createRange().text+"<br/>"+document.URL;
}

var toc=document.getElementById("TOC");
if(!toc) {
toc=document.createElement("div");
toc.id="TOC";
document.body.insertBefore(toc, document.body.firstChild);
}
var headings;
if (document.querySelectorAll)
headings=document.querySelectorAll("h1, h2, h3, h4, h5, h6");
else
headings=findHeadings(document.body, []);

function findHeadings(root, sects){
for(var c=root.firstChild; c!=null; c=c.nextSibling){
if (c.nodeType!==1) continue;
if (c.tagName.length==2 && c.tagName.charAt(0)=="H")
sects.push(c);
else
findHeadings(c, sects);
}
return sects;
}

var sectionNumbers=[0,0,0,0,0,0];

for(var h=0; h<headings.length; h++) {
var heading=headings[h];

if(heading.parentNode==toc) continue;

var level=parseInt(heading.tagName.charAt(1));
if (isNaN(level)||level<1||level>6) continue;

sectionNumbers[level-1]++;
for(var i=level; i<6; i++) sectionNumbers[i]=0;

var sectionNumber=sectionNumbers.slice(0, level).join(".");

var span=document.createElement("span");
span.className="TOCSectNum";
span.innerHTML=sectionNumber;
heading.insertBefore(span, heading.firstChild);
heading.id="TOC"+sectionNumber;
var anchor=document.createElement("a");
heading.parentNode.insertBefore(anchor, heading);
anchor.appendChild(heading);

var link=document.createElement("a");
link.href="#TOC"+sectionNumber;
link.innerHTML=heading.innerHTML;

var entry=document.createElement("div");
entry.className="TOCEntry TOCLevel" + level;
entry.appendChild(link);

toc.appendChild(entry);
}
};

</script>

You can see the preview of Table of contents generated by JavaScript code above. If you have any problem or difficulties while implementing the above JavaScript code, you are welcomed to mention on the comment session at the bottom this post.



Related Search Terms

Related Posts:

Comments

Popular posts from this blog

Solved MCQ of Computer Security set -1

1. In computer security, ……………………. means that computer system assets can be modified only by authorized parities. A) Confidentiality B) Integrity C) Availability D) Authenticity 2. In computer security, …………………….. means that the information in a computer system only be accessible for reading by authorized parities. A) Confidentiality B) Integrity C) Availability D) Authenticity 3. The type of threats on the security of a computer system or network are …………………….. i) Interruption                   ii) Interception                  iii) Modification iv) Creation                         v) Fabrication A) i, ii, iii and iv only B) ii, iii, iv and v only C) i, ii, iii and v only D) All i, ii, iii, iv and v 4. Whi...

Solved MCQ for IT Officer Exam Part-2

SWIFT Logo (Photo credit: Wikipedia ) 1. SWIFT stands for: A) Society for Worldwide Interbank Financial Telecommunication B) Society for Worldwide International Financial Telecommunication C) Society for Worldwide Interbank Financial Transmission D) Society for Worldwide Interbank Financial Transfer 2. Which one is the correct statement? A) XML is a technology used only on the internet. B) XML is a programming language. C) XML is highly suitable data exchange between two different systems. D) XML is scripting language. 3. Decision support systems usually A) serve managers interested in weekly, monthly and yearly results, not day-to-day activities. B) help managers make decisions that are unique, rapidly changing and not easily specified in advance. C) provide managers with a generalized computing and telecommunications capacity that can be applied to a changing array of problems. D) perform and record the daily routine transactions necessary to the conduct of business. 4. Identifying...

Solved MCQ on Fundamental of C Language set-7

1) 'C' allows a three way transfer of control with the help of A. Unary Operator B. Relational Operator C. Ternary Operator D. Comparison Operator 2) Operators have hierarchy. It is used to know which operator .... A. is most important B. is used first C. is faster D. operators on large numbers 3) The statement that transfers control to the beginning of the loop is called .. A. break statement B. exit statement C. continue statement D. goto statement 4) C programming language was developed by .. A. Dennis Ritche B. Ken Thompson C. Bill Gates D. Peter Norton 5) The value that follows the keyword CASE may only be A. constants B. variable C. number D. semicolon 6) In a C language 'a' represents ... A. a digit B. an integer C. a character D. a word 7) The statement which is used to terminate the control from the loop is A. break B. continue C. goto D. exit 8) The continue command cannot be used with .... A. for B. switch C. do D. while 9) A self contained block of statement...