pascal b
pascal b

Reputation: 371

javascript menu in single file with current document location telling what menu in file to show

I would like to know if it's possible to store a menu in a single javascript file and each consequent links as document location will read a specific menu set in the single page as below

menu start home as it should show on home page

document.write('<ul>');
    document.write('<li>');
    document.write('<a href="index.html">Home</a>');
    document.write('</li><li>');
    document.write('<br /><li>');
    document.write('<a href="link1.html">LINK1</a>');
    document.write('</li><br><li>');
    document.write('<a href="link2.html">LINK2</a>');
    document.write('</li><br><li>');
    document.write('<a href="link3.html">LINK3</a>');
    document.write('</li>
document.write('</ul>');

menu 1 as it should show on page link1

document.write('<ul>');
    document.write('<li>');
    document.write('<a href="index.html">HOME</a>');
    document.write('</li><li>');
    document.write('<br /><li>');
    document.write('<a href="link1.html">LINK1</a>');
    document.write('</li><br><li>');
    document.write('<a href="link1.1.html">link1.1</a>');
    document.write('</li><br><li>');
    document.write('<a href="link1.2.html">link1.2</a>');
    document.write('</li><br><li>');
    document.write('<a href="link1.3.html">link1.3</a>');
    document.write('</li><br><li>');
    document.write('<a href="link2.html">LINK2</a>');
    document.write('</li><br><li>');
    document.write('<a href="link3.html">LINK3</a>');
    document.write('</li>
document.write('</ul>');

menu 2 as it should show on page link2

document.write('<ul>');
    document.write('<li>');
    document.write('<a href="index.html">HOME</a>');
    document.write('</li><li>');
    document.write('<br /><li>');
    document.write('<a href="link1.html">LINK1</a>');
    document.write('</li><br><li>');
    document.write('<a href="link2.html">LINK2</a>');
    document.write('</li><br><li>');
    document.write('<a href="link2.1.html">link2.1</a>');
    document.write('</li><br><li>');
    document.write('<a href="link2.2.html">link2.2</a>');
    document.write('</li><br><li>');
    document.write('<a href="link2.3.html">link2.3</a>');
    document.write('</li><br><li>');
    document.write('<a href="link3.html">LINK3</a>');
    document.write('</li>
document.write('</ul>');

menu 3 as it should show on page link3

document.write('<ul>');
    document.write('<li>');
    document.write('<a href="index.html">HOME</a>');
    document.write('</li><li>');
    document.write('<br /><li>');
    document.write('<a href="link1.html">LINK1</a>');
    document.write('</li><br><li>');
    document.write('<a href="link2.html">LINK2</a>');
    document.write('</li><br><li>');
    document.write('<a href="link3.html">LINK3</a>');
    document.write('</li><br><li>');
    document.write('<a href="link3.1.html">link3.1</a>');
    document.write('</li><br><li>');
    document.write('<a href="link3.2.html">link3.2</a>');
    document.write('</li><br><li>');
    document.write('<a href="link3.3.html">link3.2</a>');
    document.write('</li>
document.write('</ul>');

The html file

I think menu 1, 2 and 3 in the javascript page should have a dedicated name so that when the current document location is link3.html or link3.1.html ... javascript will find and show menu 3. Is that possible in a simple way?

Thanks for any help on this.

Pascal

Upvotes: 0

Views: 125

Answers (2)

pascal b
pascal b

Reputation: 371

Okay, got it myself:

if(document.URL.indexOf("index.html") != -1){...}

does the trick!

Upvotes: 0

durbnpoisn
durbnpoisn

Reputation: 4669

You need to isolate the name of the page you wish to find, then set whatever menu you wish to show. This answer shows a pretty good example how to sniff the URL to get the page name

Upvotes: 1

Related Questions