javascript - how to repeat the left menubar in each html page -


var navigation  = new array();  // navigation.  // ==================== navigation ==================== // navigation[0] = '<div id="menu">'; navigation[1] = '<ul>'; navigation[2] = '<li><a href="../about_us.htm">aboutus</a></li>'; navigation[3] = '</ul>'; navigation[4] = '</div><!-- close tab navigation -->';  function show(i) {    (x in i)    {        document.write(i[x] + '\n')    } } 

i have created above function in javascript menubar need call menubar in html file.

i recommend use document.createelement instead of using document write, , array of string.

secondly recommend initialize arrays using [] literal instead of using array construct.
this:

var arr = [1,2,3]; //like var arr2 = new array(1,2,3,4); // not recommended 

the code below should work want do.

var navigation = document.createelement('div'); navigation.id = 'menu'; navigation.innerhtml = '<ul><li>' +   '<li><a href="../about_us.htm">aboutus</a></li>' + '</ul></li>'; document.body.appendchild(navigation); 

there few ways can call html:

  1. wrap in <script></script> tag @ bottom of page
  2. put in function set call onload
  3. add other trigger in html.

personally recommend putting in function , adding onload event.

javascript:

window.onload = appendnavigation; function appendnavigation() {   var navigation = document.createelement('div');   navigation.id = 'menu';   navigation.innerhtml = '<ul><li>' +    '<li><a href="../about_us.htm">aboutus</a></li>' + '</ul></li>';   document.body.appendchild(navigation); } 

html:

<!doctype html> <html>  <head>...</head>  <body>    <script src="/script.js"></script>  </body> </html> 

if want working example @ jsfiddle: https://jsfiddle.net/d1r43w33/

although should aware when used outside of jsfiddle, must include script link in html.


Comments

Popular posts from this blog

Command prompt result in label. Python 2.7 -

javascript - How do I use URL parameters to change link href on page? -

amazon web services - AWS Route53 Trying To Get Site To Resolve To www -