I have an MVC app that uses the _Layout page to render a header and a sidebar in every page. I am trying to use Angular's Accordion for the menus. However, as soon as I try to place a controller in my _Layout page all of my angular code stops working (I have angular in the Index.cshtml which displays via RenderBody()).
Here is my _Layout page code:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width" />
<title>#ViewBag.Title</title>
#Styles.Render("~/Content/css")
#Scripts.Render("~/bundles/modernizr")
#Scripts.Render("~/bundles/jquery")
<link href="~/Content/bootstrap.css" rel="stylesheet" />
<script src="~/Scripts/angular.js"></script>
<script src="~/Scripts/angular-sanitize.js"></script>
<script src="~/Scripts/ui-bootstrap-custom-tpls-0.13.3.js"></script>
<link href="~/Content/Layout.css" rel="stylesheet" />
<script src="~/Scripts/app/app.js"></script>
<script src="~/Scripts/app/LayoutCtrl.js"></script>
<script src="~/Scripts/app/generalsearchService.js"></script>
</head>
<body>
<header>
<div class="content-wrapper">
<div class="float-left">
<p class="site-title">
</div>
<div class="float-right">
<nav>
</nav>
</div>
</div>
</header>
<div ng-app="myModule" ng-controller="LayoutCtrl">
<nav class='sidebar sidebar-menu-collapsed'>
<a href='#' id='justify-icon'>
<span class='glyphicon glyphicon-triangle-right'>
</span>
</a>
<ul>
<li class='active'>
<a class='expandable' href='#' title='Dashboard'>
<div id="myTab" class='vertical-text collapsed-element' style="color:blue; width:300px">Expand For Menu Options
</div>
<span class='expanded-element'>Dashboard</span>
</a>
</li>
<li>
<a class='expandable' href='#' title='APIs'>
<span class="collapsed-element"></span>
<span class='expanded-element'>APIs</span>
</a>
</li>
<li>
<a class='expandable' href='#' title='Settings'>
<span class="collapsed-element"></span>
<span class='expanded-element'>Settings</span>
</a>
</li>
<li>
<a class='expandable' href='#' title='Account'>
<span class="collapsed-element"></span>
<span class='expanded-element'>Account</span>
</a>
</li>
</ul>
<a href='#' id='logout-icon' title='Logout'>
<span class="collapsed-element"></span>
<span class='glyphicon glyphicon-off'></span>
</a>
</nav>
<nav>
<accordion close-others="oneAtATime">
<accordion-group is-open="status.open">
<accordion-heading>
</accordion-heading>
</accordion-group>
</accordion>
</nav>
</div>
<div id="myBody" style="margin-top:50px; margin-left:320px; width:80%; position:absolute">
#RenderBody()
</div>
#Scripts.Render("~/bundles/jquery")
#RenderSection("scripts", required: false)
<script type="text/javascript">
(function () {
$(function () {
var collapseMyMenu, expandMyMenu, toggleGlyphCollapse, toggleGlyphExpand, hideMenuTexts, showMenuTexts;
expandMyMenu = function () {
document.getElementById('myBody').style.width = '80%';
document.getElementById('myBody').style.marginLeft = '320px';
document.getElementById('myTab').style.color = '#E6E7E8';
return $("nav.sidebar").removeClass("sidebar-menu-collapsed").addClass("sidebar-menu-expanded");
};
collapseMyMenu = function () {
document.getElementById('myBody').style.width = '99%';
document.getElementById('myBody').style.marginLeft = '70px';
document.getElementById('myTab').style.color = 'blue';
return $("nav.sidebar").removeClass("sidebar-menu-expanded").addClass("sidebar-menu-collapsed");
};
showMenuTexts = function () {
return $("nav.sidebar ul a span.expanded-element").show();
};
hideMenuTexts = function () {
return $("nav.sidebar ul a span.expanded-element").hide();
};
return $("#justify-icon").click(function (e) {
if ($(this).parent("nav.sidebar").hasClass("sidebar-menu-collapsed")) {
expandMyMenu();
showMenuTexts();
$(this).css({
color: "#000"
});
} else if ($(this).parent("nav.sidebar").hasClass("sidebar-menu-expanded")) {
collapseMyMenu();
hideMenuTexts();
$(this).css({
color: "#FFF"
});
}
$(this).find('span').toggleClass('glyphicon-triangle-right').toggleClass('glyphicon-triangle-left');
return false;
});
});
}).call(this);
</script>
</body>
</html>
here is my Angular controller for the _Layout:
angular.module('myModule').controller('LayoutCtrl', function ($scope, generalsearchService) {
getmenuItems();
function getmenuItems() {
generalsearchService.GetMenuItems()
.success(function (data) {
$scope.menuItems = data;
});
};
});
The Angular in my Index.cshtml page works fine until I add the above to the _Layout page.
Can anyone tell me what I am missing? I've been working on this for hours and just can't seem to figure it out.
Any assistance is greatly appreciated!
I found a workaround for my issue, although it is not the best solution because it will require that I put everything in one controller. The workaround is to put a div around everything in the body of my _Layout page(which includes the "#RenderBody" which renders my Index page) and include the ng-app and ng-controller in that div on the _Layout page. If anyone knows of a better solution..I'm all ears. Thanks to those who took the time to try to help thus far.
Related
I am starting completely with html and javascript and coding in general.
I want to bild a website as part of my learning. The website will be about wedding films and clips.
I want to have a side menu which menu button (three horizontal lines) in top left corner. When the menu button is 'clicked' the side menu will roll out and menu button will change to cross. When the menu button is a cross, it should have a function of hiding the side menu.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Wedding Page</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" type="text/css" media="screen" href="style.css" />
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Dancing+Script:700">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons">
<script src="main.js"></script>
</head>
<body>
<div id="side-menu" class="side-nav">
×
<ul class="fa-ul">
<li><img id="icon" src="/mnt/120AA1F00AA1D0D1/Programming/Wedding-Web-Page/Icons/youtube32.png"></img> Home</li>
<li>Book a date</li>
<li>Portfolio</li>
<li>Check</li>
</ul>
</div>
<div>
<span class="open-slide">
<a href="#" onclick="toCross(this)">
<div class="bar1"></div>
<div class="bar2"></div>
<div class="bar3"></div>
</a>
</span>
</div>
<p class="quote">One video, <br> thousands memories.</p>
</body>
</html>
The connected javascript file is here:
var nav = false;
function openSideMenu(){
document.getElementById('side-menu').style.width = '250px';
document.getElementById('main').style.marginLeft = '250px';
nav = true;
}
function closeSideMenu(){
document.getElementById('side-menu').style.width = '60px';
document.getElementById('main').style.marginLeft = '60px';
nav = false;
}
function toCross(x){
x.classList.toggle("change");
nav ? closeSideMenu() : openSideMenu();
}
I have found similar question asked here: JavaScript Toggle between 2 Functions but for some reason the code stops after the first openSideMenu() is executed and the nav variable does not change from false to true.
I am stuck here hating my life and I will be so grateful for a help!
As far as I can see you haven't provided a HTML markup containing the element #main. It seems document.getElementById('main') is what causes the code to not work:
var nav = false;
function openSideMenu() {
document.getElementById('side-menu').style.width = '250px';
//document.getElementById('main').style.marginLeft = '250px';
nav = true;
}
function closeSideMenu() {
document.getElementById('side-menu').style.width = '60px';
//document.getElementById('main').style.marginLeft = '60px';
nav = false;
}
function toCross(x) {
console.log(nav)
x.classList.toggle("change");
nav ? closeSideMenu() : openSideMenu();
}
<div id="side-menu" class="side-nav">
×
<ul class="fa-ul">
<li>
<img id="icon" src="/mnt/120AA1F00AA1D0D1/Programming/Wedding-Web-Page/Icons/youtube32.png"> Home
</li>
<li>Book a date</li>
<li>Portfolio</li>
<li>Check</li>
</ul>
</div>
<div>
<span class="open-slide">
<a href="#" onclick="toCross(this)">x
<div class="bar1"></div>
<div class="bar2"></div>
<div class="bar3"></div>
</a>
</span>
</div>
<p class="quote">One video, <br> thousands memories.</p>
I'm trying to start up an angular module, but I cannot seem to understand why it's not working. All the controllers, modules, and directives seem okay to me. I feel as if the problem is with how everything is loaded in the index.html page. Anyone have an idea what might be the problem? I've checked similar questions but no luck thus far.
index.html
<!doctype html>
<html lang="en" ng-app="application">
<head>
<meta charset="utf-8">
<title>My Portfolio</title>
<link rel="stylesheet" href="styles/main.css"/>
<link rel="stylesheet" href="bower_components/bootstrap/dist/css/bootstrap.min.css"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.8/angular.min.js"></script>
<script src="bower_components/jquery/dist/jquery.min.js"></script>
<script src="bower_components/angular-route/angular-route.min.js"></script>
<script src="bower_components/angular-loader/angular-loader.min.js"></script>
<script src="bower_components/bootstrap/dist/js/bootstrap.min.js"></script>
<script src="scripts/module.js"></script>
<script src="scripts/controllers/mainHeroController.js"></script>
<script src="scripts/directives/mainHero.directive.js"></script>
</head>
<body ng-app="webApp">
<nav id="main-navbar" class="navbar navbar-inverse navbar-fixed-top">
<div class="container-fluid">
<div class="navbar-header">
<a class="navbar-brand" href="#">My Portfolio</a>
</div>
<ul class="nav navbar-nav">
<li class="active">Home</li>
<li>Page 1</li>
<li>Page 2</li>
<li>Page 3</li>
</ul>
</div>
</nav>
<div id="main-jumbotron" class="jumbotron container-fluid">
<div class="row">
<div class="col-md-4">
<img id="profile-pic" src="images/ProfilePic.jpg"/>
</div>
<div class="col-md-8">
<h1 class="display-3">Hello world!</h1>
<p class="lead">This is a simple hero unit, a simple jumbotron-style component for calling extra attention to featured content or information.</p>
<hr class="m-y-2">
<p>It uses utility classes for typography and spacing to space content out within the larger container.</p>
<p class="lead">
<a class="btn btn-primary btn-lg" href="#" role="button">Learn more</a>
</p>
</div>
</div>
</div>
<div id="container1">
<mainHero></mainHero>
</div>
<div id="container2">
Container 2
</div>
</body>
</html>
module.js
(function()
{
'use strict'
angular
.module('webApp', [])
})();
mainHero.js
(function mainHeroDirective()
{
'use strict'
angular
.module('webApp')
.directive('mainHero', mainHero);
mainHero.$inject = [];
function mainHero()
{
var directive =
{
restrict: 'E',
controller: 'MainHeroController',
//controllerAs: 'mainHero',
scope: {},
template: 'Directive works!'
}
}
})();
mainHeroController.js
(function mainHeroController()
{
'use strict'
angular
.module('webApp')
.controller('MainHeroController', MainHeroController);
mainHeroController.$inject = [];
function MainHeroController()
{
alert('works!');
}
})()
The error is actually caused by the "ng-app" that is left at the top of your document:
<html lang="en" ng-app="application">
You can tell (kind of) from the URL in the error:
http://errors.angularjs.org/1.5.8/$injector/nomod?p0=application
There was another problem with the way your directive is defined though, because the function is not returning anything, and also a typo in your controller, because the case is not consistent.
I copied it to codepen and corrected those errors:
http://codepen.io/anon/pen/ZpbZRJ
Hope it helps!
I am trying to create a page that will replace content in a DOM element when another button is click. I am using KnockoutJS to manage binding and click events. I have a method in my ViewModel that will load some text from a text file, and replace the DOM content with the text file content. The issue is that clicks don't seem to be working:
HTML
<!DOCTYPE html>
<html>
<head>
<title>About Me</title>
<link rel="stylesheet" type="text/css" href="css/styles.css">
<script src="http://code.jquery.com/jquery-2.1.4.min.js"></script>
<script src="js/knockout-3.3.0.js"></script>
<script src="js/nav-mobile.js"></script>
<script src="js/app.js"></script>
</head>
<body>
<!-- nav bar -->
<nav>
<ul class="navbar">
<li><h1 class="title">About Me</h1></li>
<li class="nav-text" data-bind="click: setPage.bind('fam')"><p>Family</p></li>
<li class="nav-text" data-bind="click: setPage.bind('mlg')"><p>MLG</p></li>
<li class="nav-text" data-bind="click: setPage.bind('bio')"><p>Bio</p></li>
<li class="nav-text" data-bind="click: setPage.bind('int')"><p>Interests</p></li>
</ul>
</nav>
<!-- main section -->
<section>
<div class="main">
<p class="text-main"></p>
</div>
</section>
</body>
</html>
JavaScript
$(document).ready(function() {
function ViewModel() {
var self = this;
self.text = ko.observable();
self.setPage = function(page) {
$.get("../res/text/" + page + ".txt", function(data) {
console.log("Fetching " + page + ".text" );
$(".text-main").html(data);
});
}
}
ko.applyBindings(new ViewModel());
});
I was at least hoping to the something logged to the console, but no. The developer tools don't show anything. I have also tried binding like this:
data-bind="click: setPage('fam')"
but it does not work. What is wrong with my click binding?
The correct syntax for using .bind is .bind($data, 'parameter')
<li class="nav-text" data-bind="click: setPage.bind($data, 'fam')"><p>Family</p></li>
<li class="nav-text" data-bind="click: setPage.bind($data, 'mlg')"><p>MLG</p></li>
<li class="nav-text" data-bind="click: setPage.bind($data, 'bio')"><p>Bio</p></li>
<li class="nav-text" data-bind="click: setPage.bind($data, 'int')"><p>Interests</p></li>
JsFiddle
I'm creating vertical menu for my asp.net mvc 4 web application.
so I referred this Link to integrate to my matter .
so I edited my _HECLayout.cshtml (that contains in “~/Views/Shared/”) file Like this
<!DOCTYPE html>
<html lang="en">
<head>
<script type="text/javascript" src='~/Scripts/jquery-2.0.0.min.js'/></script>
<link href="~/Content/font-awesome.min.css" rel="stylesheet" />
<link href="~/Content/font-awesome.css" rel="stylesheet" />
<link href="~/Content/css.css" rel="stylesheet" />
<script>
$(document).ready(function () {
$('ul.formxd li a').click(
function (e) {
e.preventDefault(); // prevent the default action
e.stopPropagation; // stop the click from bubbling
e.stopPropagation; // stop the click from bubbling
$(this).closest('ul').find('.selected').removeClass('selected');
$(this).parent().addClass('selected');
});
});
</script>
<meta charset="utf-8" />
<title>#ViewBag.Title - Higher Education Council</title>
<link href="~/favicon.ico" rel="shortcut icon" type="image/x-icon" />
<meta name="viewport" content="width=device-width" />
#Styles.Render("~/Content/css")
#Scripts.Render("~/bundles/modernizr")
<div id="header">
<div id="top-area">
<div id="logo-area">
<img src="~/Images/hec-logo.png" />
</div>
</head>
<body>
<header>
<div class="content-wrapper">
<div class="float-right">
<section id="text-view" style="display: inline; width: 100%; text-align: right;" >
<p><i>Higher Education Institutes </i></p>
</section>
</div>
</div>
</header>
#RenderSection("featured", required: false)
<div id="menu_area">
<div class="hec_admin_menu" >
<ul class="formxd">
<li class="welcome"><a class="welcome"><i class="icon-fa-university"></i>Welcome HEC</a></li>
<li ><a class="dashboard" href="#"><i class="icon-dashboard"></i>Dashboard</a></li>
<li ><a class="reports" href="#"><i class="icon-file"></i>Reports</a></li>
<li><a class="administrator" href="#"><i class="icon-user"></i>Administrator</a></li>
<li><a class="search" href="#"><i class="icon-search"></i>Search</a></li>
<li><a class="logout" href="#Url.Action("Login", "HECAccount")"><i class="icon-signout"></i>Logout</a></li>
</ul>
</div>
<div class="hec_admin_body">
<section class="content-wrapper main-content clear-fix">
#RenderBody()
</section>
</div>
</div>
<footer>
</footer>
#Scripts.Render("~/bundles/jquery")
#RenderSection("scripts", required: false)
</body>
</html>
It's giving smooth view like the demo which I mentioned HERE , but logout url action not working properly.And all the urls not working properly ,
Any suggestion to overcome with his probblem
<li><a class="logout" href="#Url.Action("Login", "HECAccount")"><i class="icon-signout"></i>Logout</a></li>
I got same error when I trying to insert jquery reference like above , links doesn't working
So I referred This Link
That's an approach to give jquery reference to HTML Page , but in CSHTML when you insert #Scripts.Render("~/bundles/jquery") you don't need add one by one references like you done here ,
So remove <script type="text/javascript" src='~/Scripts/jquery-2.0.0.min.js'/></script>
keep #Scripts.Render("~/bundles/jquery") under the code
If your project doesn't have the "jquery-2.0.0.min.js" file , manually insert into "~/Script" Folder
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">Date Sources <span class="icon-caret-down"></span>
<li>
Information
<ul class="fallback">
<li>Ovens</li>
<li>Fridges</li>
<li>Tables</li>
<li>Costing</li>
<li>Installation</li>
</ul>
</li>
<li>
Legal Info
<ul class="fallback">
<li>Terms & Conditions</li>
<li>Data Protection</li>
</ul>
</li>
<li>
Contact Us
<ul class="fallback">
<li>Contact Details</li>
<li>Your Comments</li>
<li>Additional Info</li>
</ul>
</li>
<li>
Need Help?
<ul class="fallback">
<li>Need Help</li>
<li>FAQs</li>
<li>Ask our Experts</li>
</ul>
</li>
<li>
Cookies
</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"><span class="icon-home"></span></li>
<li>
Information
<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>Information Go For HREF</li>
<li>Ovens</li>
<li>Fridges</li>
<li>Tables</li>
<li>Costing</li>
<li>Installation</li>
</ul>
</li>
<li>
Legal Info
<ul class="fallback">
<li>Terms & Conditions</li>
<li>Data Protection</li>
</ul>
</li>
<li>
Contact Us
<ul class="fallback">
<li>Contact Details</li>
<li>Your Comments</li>
<li>Adding Directors Searches</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"><span class="icon-home"></span></li>
<li>
Information
<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>Ovens</li>
<li>Fridges</li>
<li>Tables</li>
<li>Costing</li>
<li>Installation</li>
</ul>
</li>
<li>
Legal Info
<ul class="fallback">
<li>Terms & Conditions</li>
<li>Data Protection</li>
</ul>
</li>
<li>
Contact Us
<ul class="fallback">
<li>Contact Details</li>
<li>Your Comments</li>
<li>Adding Directors Searches</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:
Information
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"><span class="icon-home"></span></li>
<li>
Information
<ul class="fallback">
<li>Information (go for)</li>
<li> New Customer (Go For)
<li>Ovens</li>
<li>Fridges</li>
<li>Tables</li>
<li>Costing</li>
<li>Installation</li>
</ul>
</li>
<li>
Legal Info
<ul class="fallback">
<li>Terms & Conditions</li>
<li>Data Protection</li>
</ul>
</li>
<li>
Contact Us
<ul class="fallback">
<li>Contact Details</li>
<li>Your Comments</li>
<li>Adding Directors Searches</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