简体   繁体   中英

Navigation Menu Links

I've recently started restructuring a website, which currently uses frames and framesets.

I have now adopted a typical structure.html :

<body>
<div>
    <div id="headerNavigation"></div>
    <div id="PageContent"></div>
    <div id="footer"></div>
</div>
</body>
</html>

With the following jQuery scripts to populate the DIVs:

<head>
<script>
$.get('PageHeader.html', processHeader, "html").fail(function() { alert("There is a problem loading the header. Please re-try"); });
function processHeader(file_data)
{
    $("#headerNavigation").html(file_data);
}

$.get('TermsConditions.html', processContent, "html").fail(function() { alert("There is a problem loading a resource. Please re-try"); });
function processContent(file_data)
{
    $("#PageContent").html(file_data);
}

$.get('Footer.html', processFooter, "html").fail(function() { alert("There is a problem loading a resource. Please re-try"); });
function processFooter(file_data)
{
    $("#Footer").html(file_data);
}
</script>
</head>

This works great when i specify the Content page, however, i need some kind of jQuery function that forwards/populates the DIV with the correct content when clicking on an item in my Navigation menu - index.html :

<html>
<head>
<!-- jQuery code for drop down nav menus  -->
<script type="text/javascript"> 
    $(document).ready(function(){        
        $('nav li ul').hide().removeClass('fallback');
        $('nav li').hover(
            function () {
                $('ul', this).stop().slideDown(300);
            },
            function () {
                $('ul', this).stop().slideUp(300);
            }
        );       
    });
</script>

</head>
<body>

<nav class="site-nav">
<ul class="menu-nav wrap menu menu--hor">
    <li class="data-sources"><a href="#" title="">Date Sources <span class="icon-caret-down"></span> </a>       
        <li>
            <a href="frameset.html?Information.html">Information</a>
            <ul class="fallback">
            <li><a href="frameset.html?ovens.html">Ovens</a></li>
                <li><a href="frameset.html?fridges.html">Fridges</a></li>
                <li><a href="frameset.html?tables.html">Tables</a></li>
                <li><a href="frameset.html?costing.html">Costing</a></li>
                <li><a href="frameset.html?install.html">Installation</a></li>
            </ul>
        </li>
        <li>
            <a href="frameset.html?legal.html">Legal Info</a>
            <ul class="fallback">
                <li><a href="frameset.html?terms.html">Terms & Conditions</a></li>
                <li><a href="frameset.html?data.html">Data Protection</a></li>                  
            </ul>
        </li>
        <li>
            <a href="frameset.html?contact/html">Contact Us</a>
            <ul class="fallback">
            <li><a href="javascript:openHelpWind('contact')">Contact Details</a></li>
                <li><a href="frameset.html?comments.html">Your Comments</a></li>
                <li><a href="frameset.html?addinfos.html">Additional Info</a></li>
            </ul>
        </li>
        <li>
            <a href="#">Need Help?</a>
            <ul class="fallback">
            <li><a href="javascript:parent.PageHeader.openHelpWind(helpContext)">Need Help</a></li>
                <li><a href="externalsite">FAQs</a></li>
                <li><a href="externalsite">Ask our Experts</a></li>
            </ul>
        </li>
        <li>
            <a href="frameset.html?cookies.html">Cookies</a>
        </li>
    </li>
</ul>
</nav>
</body>
</html>

Any ideas how how i can create a function to do this would be greatly appreciated.

Thanks

Update 21-06-2013 Basic Site Skeleton:

Index.html

<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport" content="width=device-width">

        <script src="javascript/modernizr-2.6.2.min.js"></script>

<title>Welcome</title>
<link id="productStyle" type="text/css" href="css/style.css" rel="stylesheet">

<script type="text/javascript" src="javascript/jquery-1.9.1.min.js"></script>
<script type="text/javascript" src="javascript/jquery-ui-1.10.1.custom.min.js"></script>

<!-- JavaScript functions to enable dropdown navigation-->
<script type="text/javascript"> 
    $(document).ready(function(){        
        $('nav li ul').hide().removeClass('fallback');
        $('nav li').hover(
            function () {
                $('ul', this).stop().slideDown(300);
            },
            function () {
                $('ul', this).stop().slideUp(300);
            }
        );       
    });
</script>

    <script type="text/javascript">

    function gofor(a) {
    $.get(a, processContent, 'html').fail(function() { alert('There is a problem loading a resource. Please re-try');});
    }

    function processContent(file_data)
    {
        $("#PageContent").html(file_data);
    }

</script>

</head>
<body>

<!-- MENU START -->
<nav class="site-nav">
<ul class="menu-nav ">
    <li class="menu--home"><a href="index.html " title="home"><span class="icon-home"></span></a></li>      
        <li>
        <a href="frameset.html?Information.html">Information</a>
        <ul class="fallback">

            <li><a onclick="gofor('information.html')">Information (go for)</a></li>
    <li><a onclick="gofor('openaccount.html')"> New Customer (Go For)</a>

            <li><a onClick="gofor('information.html')">Information Go For onClick</a></li>
    <li><a href="javascript:gofor('information.html')">Information Go For HREF</a></li>


            <li><a href="frameset.html?ovens.html">Ovens</a></li>
            <li><a href="frameset.html?fridges.html">Fridges</a></li>
            <li><a href="frameset.html?tables.html">Tables</a></li>
            <li><a href="frameset.html?costing.html">Costing</a></li>
            <li><a href="frameset.html?install.html">Installation</a></li>
        </ul>
    </li>
    <li>
        <a href="frameset.html?legal.html">Legal Info</a>
        <ul class="fallback">
                     <li><a href="frameset.html?terms.html">Terms & Conditions</a></li>
                 <li><a href="frameset.html?data.html">Data Protection</a></li>                  
                       </ul>
        </li>
        <li>
            <a href="#">Contact Us</a>
            <ul class="fallback">
      <li><a href="javascript:openHelpWind('contact')">Contact Details</a></li>
                <li><a href="frameset.html?comments.html">Your Comments</a></li>
                <li><a href="frameset.html?addingds.html">Adding Directors Searches</a></li>
            </ul>
        </li>
    </li>
</ul> <!-- END menu-nav -->
</nav> <!-- END nav.site-nav -->
<!-- MENU END -->

<section>
<div class="page-heading">
    <h1 class="wrap">Welcome to Tables, Ladders and Chairs </h1>
</div>
<div>

Other welcome content

</div>
</section>
<footer class="site-footer" role="contentinfo">
<div class="wrap">
    <small>2013 TL&C’s. </small>
</div>
</footer>

Frameset.html:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN">
<html lang="en">
<head>
<script type="text/javascript" src="javascript/jquery-1.9.1.min.js"></script>

<script>
$.get('PageHeader.html', processHeader, "html").fail(function() { alert("There is a problem loading a resource. Please re-try"); });

function processHeader (file_data)
{
    $("#header").html(file_data);
}
</script>

</head>
<body>
<div>
    <div id="header"></div>
    <div id="PageContent"></div>
    <div id="footer"></div>
</div>
</body>
</html>

</body>
</html>

PageHeader.html:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<script src="javascript/modernizr-2.6.2.min.js"></script>

<title>Page Header</title>

<link id="productStyle" type="text/css" href="css/style.css" rel="stylesheet">

<script type="text/javascript" src="javascript/jquery-1.9.1.min.js"></script>
<script type="text/javascript" src="javascript/jquery-ui-1.10.1.custom.min.js"></script>

<!-- JavaScript functions to enable dropdown navigation-->
<script type="text/javascript"> 
    $(document).ready(function() {       
        $('nav li ul').hide().removeClass('fallback');
        $('nav li').hover(
            function () {
                $('ul', this).stop().slideDown(300);
            },
            function () {
                $('ul', this).stop().slideUp(300);
        }
        );       
    });
</script>

    <script type="text/javascript">

    function gofor(a) {
    $.get(a, processContent, 'html').fail(function() { alert('There is a problem loading a resource. Please re-try');});
    }

    function processContent(file_data)
    {
        $("#PageContent").html(file_data);
    }

</script>


<!-- MENU START -->
<nav class="site-nav">
<ul class="menu-nav ">
    <li class="menu--home"><a href="index.html " title="home"><span class="icon-home"></span></a></li>      
        <li>
        <a href="frameset.html?Information.html">Information</a>
        <ul class="fallback">

  <li><a onclick="gofor('information.html')">Information (go for)</a></li>
         <li><a onclick="gofor('openaccount.html')"> New Customer (Go For)</a>

            <li><a href="frameset.html?ovens.html">Ovens</a></li>
            <li><a href="frameset.html?fridges.html">Fridges</a></li>
            <li><a href="frameset.html?tables.html">Tables</a></li>
            <li><a href="frameset.html?costing.html">Costing</a></li>
            <li><a href="frameset.html?install.html">Installation</a></li>
        </ul>
    </li>
    <li>
        <a href="frameset.html?legal.html">Legal Info</a>
        <ul class="fallback">
                     <li><a href="frameset.html?terms.html">Terms & Conditions</a></li>
                 <li><a href="frameset.html?data.html">Data Protection</a></li>                  
                       </ul>
        </li>
        <li>
            <a href="#">Contact Us</a>
            <ul class="fallback">
      <li><a href="javascript:openHelpWind('contact')">Contact Details</a></li>
                <li><a href="frameset.html?comments.html">Your Comments</a></li>
                <li><a href="frameset.html?addingds.html">Adding Directors Searches</a></li>
            </ul>
        </li>
    </li>
</ul> <!-- END menu-nav -->
</nav> <!-- END nav.site-nav -->
<!-- MENU END -->

Information.html

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html lang="en">

<head>
<title>Information</title>

<link type="text/css" rel="stylesheet" href="css/style.css">
       <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">

</head>

<body>
<form>

    Information about Tables Ladders and Chairs etc.

</form>
<br>
</body>
</html>

Thanks

You can, for each link in the menu, add a call to a function like this:

<a onclick="$.get('Information.html', processContent, 'html')" >Information</a>

As you see, you will reuse the same function used to load the content the first time:

$.get('Information.html', processContent, 'html')

In this way,you have just called the same 'processContent' but for a different page to load. You can simplify the onclick event more by adding a third function which will be called by the onclick(or href) and then, will call the function to load the content, like this:

<a onclick="gofor('Information.html')">Information</a>

or this one:

<a href="javascript:gofor('Information.html')">Information</a>

and then add this simple function:

function gofor(page){    
    $.get(page, processContent, 'html').fail(function() { alert('There is a problem loading a resource. Please re-try');});
}

Obviously you can't use this function to load external content, because it's forbidden by the browsers

Your index page should be something like this, please pay attention to the div with id "pageContent" in it the pages will be loaded and to the function you need to have to load contents in your page.

<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport" content="width=device-width">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://modernizr.com/i/js/modernizr.com-custom-2.6.1-01.js"></script>
<title>Welcome</title>
<link id="productStyle" type="text/css" href="css/style.css" rel="stylesheet">
<!-- JavaScript functions to enable dropdown navigation-->
<script type="text/javascript"> 
    $(document).ready(function(){        
        $('nav li ul').hide().removeClass('fallback');
        $('nav li').hover(
            function () {
                $('ul', this).stop().slideDown(300);
            },
            function () {
                $('ul', this).stop().slideUp(300);
            }
        );       
    });
</script>

    <script type="text/javascript">

function gofor(a) {
$.get(a, processContent, 'html').fail(function() { alert('There is a problem loading a resource. Please re-try');});
    }

function processContent(file_data)
    {
        $("#PageContent").html(file_data);
    }

</script>

</head>
<body>

<!-- MENU START -->
<nav class="site-nav">
<ul class="menu-nav ">
    <li class="menu--home"><a href="javascript:gofor('index.html')" title="home"><span class="icon-home"></span></a></li>      
        <li>
        <a href="javascript:gofor('Information.html')">Information</a>
        <ul class="fallback">

  <li><a href="javascript:gofor('information.html')">Information (go for)</a></li>
         <li><a href="gofor('openaccount.html')"> New Customer (Go For)</a>

            <li><a href="javascript:gofor('ovens.html')">Ovens</a></li>
            <li><a href="javascript:gofor('fridges.html')">Fridges</a></li>
            <li><a href="javascript:gofor('tables.html')">Tables</a></li>
            <li><a href="javascript:gofor('costing.html')">Costing</a></li>
            <li><a href="javascript:gofor('install.html')">Installation</a></li>
        </ul>
    </li>
    <li>
        <a href="javascript:gofor('legal.html')">Legal Info</a>
        <ul class="fallback">
                     <li><a href="javascript:gofor('terms.html')">Terms & Conditions</a></li>
                 <li><a href="javascript:gofor('data.html')">Data Protection</a></li>                  
                       </ul>
        </li>
        <li>
            <a href="#">Contact Us</a>
            <ul class="fallback">
      <li><a href="javascript:openHelpWind('contact')">Contact Details</a></li>
                <li><a href="javascript:gofor('comments.html')">Your Comments</a></li>
                <li><a href="javascript:gofor('addingds.html')">Adding Directors Searches</a></li>
            </ul>
        </li>
    </li>
</ul> <!-- END menu-nav -->
</nav> <!-- END nav.site-nav -->
<!-- MENU END -->

<section>
<div class="page-heading">
    <h1 class="wrap">Welcome to Tables, Ladders and Chairs </h1>
</div>
<div>

Other welcome content

</div>
<div id="PageContent">
</div>
<footer class="site-footer" role="contentinfo">
    <div class="wrap">
        <small>2013 TL&C’s. </small>
    </div>
</footer>

There is no need for you to load all those elements via javascript. In fact I would argue that it's bad practice. You're making unnecessary calls that will lead to longer loading times for your visitors. The standard practice is to load your initial content and then use javascript as needed to respond to user commands.

I understand what you're trying to do with splitting up the content into separate partials (Header, footer, etc..) but this isn't usually how it's done. There are some template engines that would do exactly what you're trying to achieve without requiring extra ajax calls to pull in regular html content. Here are some basic examples of separating out reusable content:

PHP (Server side):

<?php include 'pageheader.html'; ?>
<div>My main content</div>
<?php include 'footer.html'; ?>

Handlebars.js (Client side)

{{>pageheader}}
<div>My main content</div>
{{>footer}}

Having said that, if you absolutely must stick with the current set up, then you would just need to parse out the url from the a tag and send that to your processContent function. Here is an example of how you can do that using jquery:

$('li').on('click', function (e) {
    var url = $(this).find(">:first-child").attr('href');
    $.get(url, processContent, "html").fail(function() { alert("There is a problem loading a resource. Please re-try"); });
});

The above code basically just binds to the click event of every li and grabs the a tag's href value before passing that value to your processContent function

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM