Smooth menu navigation - javascript

I'm currently in the midst of developing a site. The current layout is pretty basic.
There's a horizontal menu navigation across the top, and there is a main centered div beneath the menu that holds the rest of the HTML and information and so on.
Now, when a user clicks on an item in the horizontal menu, the page reloads to display the new page with the new HTML as it should. However, on each page I have to copy the menu HTML to it. This seems redundant and unnecessary. So I was thinking about it and thought of this way...
Through javascript, I get the html page and display on the main content div. Like so...
$.get( "newpage.html", function( data ) {
$("#content-div").html( data );
});
All scripts, css, and html and now loaded onto the main original page. I can two immediate benefits...
The page has a seamless, smooth transition when it comes to displaying a new page
I do not have redundant HTML when it comes to the menu
Is this optimal though? Is it a good practice to use a $.get(); call to load a new HTML page as the main method of page navigation? Are there better alternatives?
I've considered Jquery Tabs but this requires all of the HTML to be loaded beforehand, which I was hesitant about.

The answer is that it depends.
If there are multiple pages that have similar layouts and simple elements, then yes, the $.get(); method would work, but could be even faster and smoother.
When I come across this junction, I would rather just keep the existing HTML elements and just load new content into them.
Again, I don't have the full code, so I could show you (and anyone else who sees this) what I mean if you shared, but that's up to you.
Now, if some of the pages had more complex elements, you should navigate by reloading.
In that space, consistency is key.
To not be redundant with your code, you just need to make your site like a template.
For example, you could have three pages (header, footer, and the page itself) and use PHP to load the header and footer in while only writing the code once.
header.html
<html>
<head>
Fill with meta info, title, etc.
</head>
<body>
<div class="nav">
Put the nav here.
</div>
index.php
<?php include_once "header.html"; ?>
<div class="main">
Just fill it with all the page's content.
</div>
<?php include_once "footer.html"; ?>
footer.html
<div class="footer">
Footer info here.
</div>
</body>
</html>
What this allows is, aside from edit once, do everywhere, non-redundant HTML and also for CSS edits to be preserved across the site, as long as in each individual page the <div>'s are in the same class
Again, as I first said, it all depends.
If you have simple content and don't mind spaghetti code, you would be better suited to just replace the div info directly on each navigation use.
But, this method is, at least from what I've experienced, more widespread and versatile, so I'd recommend the template-esque method with PHP being loaded in.

Related

loading navbar for the whole site, how should i deal with the links?

I have just started this small project for a website and I thought i should use only one menu bar for the whole site, since it's kinda tedious to have to change all the links for all the pages in the menu bar.
I have found the following code for loading my menu bar:
<div id="nav-placeholder"></div>
<script type="text/javascript">
$(function(){
$("#nav-placeholder").load("navbar.html");
});
</script>
It works fine and I do get my menu, however I find that the links basically work the same as if I just had the menu on the page instead of loading it.
So for example if I am on the root directory everything loads fine, but as soon as I move up one directory the links wont work anymore.
I know this can be fixed by simply changing them, but then I would have to make more than a couple of navbars for the whole site?
Any fix around this?, hope you guys get my idea, would appreciate any help.
If you're using anything that can dynamically create pages on the fly like ASP or PHP then you could make a separate page with just the menu in and include it in each page that needs it. For example:
ASP:
<!-- #Include virtual="menu.aspx" -->
PHP:
<?php include 'menu.php';?>

Website: Same universal menu on different pages

I'm trying to put together a website that no-longer uses frames (my previously preferred method) but a singe page format.
One big problems I've found is that each page on the site will use the same menu. Now it occurs to me that if I amend the menu at a later date, then I'd have to change it on every page manually. This seems very time consuming and Inefficient.
Can anyone suggest ways I can alter the menu code once and have it on every page? I was thinking initially of embedding a javascript anchor to a js file on each page, then I would only have to change the js file. Are there better ways to do this?
The menu is a simple image and mix of text and anchor links.
I can program HTML/JS/CSS/C++ ... and willing to look at others if necessary to achieve my goals.
Thank you.
Make a separate partial view file containing your menu code and than include it on each page you need it to use the menu.
In PHP:
<?php require_once(__ROOT__.'/mainMenu.php'); ?>
In ASP something like:
<%# Register src="~/mainMenu/mainMenu.ascx" tagname="MainMenu" tagprefix="uc" %>
<uc:MainMenu ID="MainMenu" runat="server />
Ideally you can expand this logic and create a master template page and than feed just the dynamic content in it - that keeps all your code on one place and makes changes very simple.

Filling in a CSS Element with Javascript

I am working on a website and wanted the links to substitue only a certain text instead of loading a whole new site because of (overexagerated example) 3 new words.
The body is divided into one CSS Elements and one for the links
<body>
<div id="body>
Here is the entire body, inside a css block
</div>
</body>
<a href="home.html" >
<div id="box">
<br> Home <br>
</div>
</a>
Here i have a nice little div element (which intern is the link) however by clicking on the link i would open an entire new website (which would mean loading all elememts on the site again. I want to make it so that only my body loads and all the other things stay ... something similar to frames in html, however i am trying to stay away from frames due to SEO.
I can't be sure about your question cause you didn't post any code but i think you need jquery .text() to get it and modify with simple javascript.
Try looking at this:
jquery text

Duplicated Pages Displaying Differently

Ok I have been having fun and games trying to sort an issue out with a website.
The home page http://www.haylockpittman.co.uk/ has been replicated to http://www.haylockpittman.co.uk/new-refurb-publish/
BUT, as you can see the images on the sub page do not align correctly like the home page.
I cannot use the home page template on the internal page as we need to change the text at the bottom and as it is set up it the text is widgetised. But we duplicated the template and renamed it.
I have compared the source codes for both pages and apart from titles the only difference is at line 142/143 where the home page calls
<body class="home page page-id-92 page-template page-template-front-page-php singular two-column right-sidebar">
and the internal page calls
<body class="page page-id-2118 page-template page-template-renorefub-php singular two-column right-sidebar">
The problem is I don't want the images on every internal page so I am wary about what I need to change. I assume I need to add a new class or div id and call it. But I am pretty novice at this and the original designer wont help so any advice would be much appreciated.
Thanks in advance.
Home page has CSS rules for '.home #outer #left' but the second doesn't (as it doesn't have the .home class on the body)
Add in extra CSS rules to apply the CSS to the same DIV on the second page.
Use Firebug/Developer Tools etc next time :)

Javascript ends in Errorpage in Joomla

Currenly I'm trying to animate the component module for a website for a school project. The animation contains a fadeIn/fadeOut and a change of the content without loading a whole new website. After the animation it adds a hashtag to the URL (like #about-me). In Joomla there appears only my 404 Errorpage and the animation doesn't take effect. It works perfectly on the HTML-Only Version and in Typo3 but not in Joomla. Google shows no result to this question, only to special plugins and modules but not for the general modules. How can I use it the right way? You can watch the side under:HTML: http://www.aks-schulinfo.de/gtm3_big/24h/essmannExample of errored jQuery: http://www.aks-schulinfo.de/gtm3_big/24h/essmann/js/dynamicpage.jsThanks for your help.
Okay, you'd like to change the page's content without reloading the page. One of the easiest ways to do this is to load all of the content on the page, and then hide everything that's unrelated. So, setgup your index.php page (or which ever is your main) with all of the content separated into their own divs.
<div id="contentHome"> .... </div>
<div id="contentAnmeldung"> .... </div>
<div id="contentRegelwerk"> .... </div>
<div id="contentSitempa"> .... </div>
Use javascript to find the current hash tag with location.hash, then switch the possibilities and show related content while hiding the other three. If you're really gung-ho, you can do the same with php as the page is being created so that people can link to specific "pages" still.
NOTE: This works well with limited content like the site currently has. Don't even try it if you have a lot of content because page load times will be horrendous.
The contentchange without reloading is now working fluently, thank you! The other half (the fades) are still not working. Why does the fadeeffect apply at the html-only view but not in at the Joomla site?
Links:
HTML-Only: www.aks-schulinfo.de/gtm3_big/24h/essmann/htmlonly
Joomla-version: www.aks-schulinfo.de/gtm3_big/24h/essmann/

Categories

Resources