Clone an entire div container on another page - javascript

I have a div container which holds several other divs and other content, which collectively, makes up my slideout sidenav menu.
I would like to call that entire div from another page. I have tried some suggested jquery methods, but so far, have had no luck.
This is a shortened version of what the section looks like...
<!--Opening div for menu container-->
<div id="menuContainer">
<div>
<div>
<span class="menu">
<a href="#" onclick="CollapsiblePanel_1.open();">SECTION TITLE
<p class="subMenuOpenBtn">Open ▼</p>
</a></span></div>
<div id="CollapsiblePanel_1" class="CollapsiblePanel">
<div class="tab">
<div class="subMenu">
<ul>
<li>Option 1</li>
<li>Option 2</li>
<li>Option 3</li>
<li>Option 4</li>
<li>Option 5</li>
</ul>
<div class="subMenuCloseBtn">
<span>Close ▲
</span>
</div>
</div><!--Closing div for menu container-->
I want to save that entire bit of code on it's own page called 'SlideOutPage' (I'm not entirely sure if tat page should be saved in an html, php, or other type page)
Then... on my index.php page, I want to create a new div or 'container', and somehow call that section of code into it.

This is possible through Javascript/jQuery, yes. Save that into a new file, then use jQuery's .load() function like so.
<div id="wrapper">
Loading, please wait...
</div>
JS:
<script>
$(function() {
$("#wrapper").load("myfile.html");
}
</script>
HOWEVER, I would suggest doing this with PHP instead. You can just include the file with a PHP include or require statement.
<?php
include "myfile.php";
?>

Difference between various methods
The best way to do it is using PHP because its a backend scripting language and wont be visible to visitor. If someone visits the webpage, it looks normal to him/her.
Through JQuery, it will be visible in source code. So, in some way visitor can know that its fetching the div from another file.
<!-- external file `menu.html`
Opening div for menu container-->
<div id="menuContainer">
<div>
<div>
<span class="menu">
<a href="#" onclick="CollapsiblePanel_1.open();">SECTION TITLE
<p class="subMenuOpenBtn">Open ▼</p>
</a></span></div>
<div id="CollapsiblePanel_1" class="CollapsiblePanel">
<div class="tab">
<div class="subMenu">
<ul>
<li>Option 1</li>
<li>Option 2</li>
<li>Option 3</li>
<li>Option 4</li>
<li>Option 5</li>
</ul>
<div class="subMenuCloseBtn">
<span>Close ▲
</span>
</div>
</div><!--Closing div for menu container-->
Through PHP
include 'menu.html'; or require 'menu.html'
<div id="wrapper">
<?php include 'menu.html'; ?>
</div>
or,
<div id="wrapper">
<?php require 'menu.html'; ?>
</div>
The file in which this code is called should be .php.
You can also use include_once 'menu.html' or require_once 'menu.html'. In this your file will be called only one time in a page. If there is another occurrence of same file, then it won't be included.
Through JQuery
<div id="wrapper"></div>
<script>
$(function() {
$("#wrapper").load("menu.html");
}
</script>

Related

Twitter-bootstrap "affix" is not affixing

In implementing a search functionality, I'm returning dynamically generated html in response to an ajax request and inserting the HTML into a div. Part of the dynamic HTML is a "navigation pane" that I want to affix using bootstrap (the "#affixcontainer" div). However, the navigation pane refuses to stick.
<div class="hidden-xs col-xs-3">
<div id="affixcontainer" data-spy="affix" data-offset-top="120" data-offset-bottom="10">
<div class="panel panel-primary">
<div class="panel-heading">SEARCH RESULTS</div>
<div class="panel-body" style="padding:0;margin:0">
<nav id="scroll-nav" class="bs-docs-sidebar hidden-print hidden-xs" role="navigation">
<ul class="nav nav-pills nav-stacked bs-docs-sidenav">
<li><span class="glyphicon glyphicon-arrow-up" aria-hidden="true"></span>Top</li>
<li>Category 1</li>
<li>Category 2</li>
<li>Category 3</li>
<li>Category 4</li>
<li>Category 5</li>
</ul>
</nav>
</div>
</div>
</div>
</div>
<div class="col-xs-9">
SEARCH LISTINGS
</div>
I also tried to affix using javascript (separately, see code snipped below), but that doesn't seem to work either. The script is at the bottom of the dynamically generated HTMl. What am I doing wrong?
$(document).ready(function(){
$("#affixcontainer").affix({
offset: {
top: 120,
bottom: 20
}
});
});
I'm assuming the HTML above is the HTML that is being dynamically inserted. The .affix() function would need to be called after it is appended to the DOM. The data- attributes only work for elements available on page load. Anything inserted after that require a separate call to affix(). See
https://github.com/twbs/bootstrap/blob/v3.3.6/js/affix.js#L148 for it's implementation.
EDIT
Also, $(document).ready() does not work since AJAX calls occurs after this event. The HTML elements would not be in the DOM yet.

hide footer in a specific fluid page with JS

First of all, what do I mean by "fluid page"? I mean one long page that is broken into id sections which show those id's as pages. It's most basic structure looks like this:
<div class="mainnav">
<ul>
<li>PAGE 1</li>
<li>PAGE 2</li>
<li>PAGE 3</li>
<li>PAGE 4</li>
<li>PAGE 5</li>
</ul>
</div>
<div id="page1" class="container-fluid"> Page 1 content </div>
<div id="page2" class="container-fluid"> Page 2 content </div>
<div id="page3" class="container-fluid"> Page 3 content </div>
<div id="page4" class="container-fluid"> Page 4 content </div>
<div id="page5" class="container-fluid"> Page 5 content </div>
<!-- sticky footer -->
<div class="footer"></div>
So as you can see whenever the you hit the menu item for that ID, the content slides up until it reaches that Id and it displays the content as if it was a different page. Easy enough!
My problem is, I have the footer sticky and I did that so that when you click to the next page (ID) the footer is still there, EXCEPT, I don't want it to show when #page1 is selected.
I attempted to build a small JS, but my JS is less than great. Here is my attempt
<script type="text/javascript">
function hideFooter(){
$("#homeContainer").css(display:none);
}
$(document).ready(function() {
hideFooter();
});
</script>
Obviously, not working. Here is a FIDDLE that you can use.
Any ideas!?
You just need to add a handler on the .mainnav a tags and toggle as appropriate:
$(document).ready(function() {
Viewport();
$(window).bind('resize', Viewport);
$('.mainnav a').click(function(){
if (this.href.indexOf('#page1') == -1) {
$('.footer').show();
} else {
$('.footer').hide();
}
});
});
http://jsfiddle.net/taHCv/4/

How to display section-container

I'm new to Foundation 4 and am currently trying to do the following:
I'm using the section-container horizontal-nav and was wondering if anyone knows the configuration to get the section container to display its content first by default instead of hiding it until it's been clicked? So when the page loads, the section will be shown first until the user clicks on "Section 1".
<div class="section-container horizontal-nav" data-section="horizontal-nav">
<section>
<p class="title" data-section-title>
Section 1
</p>
<div class="content" data-section-content>
<ul class="side-nav">
<li>Link 1</li>
<li>Link 2</li>
<li>Link 3</li>
<li class="divider"></li>
<li>Link 1</li>
</ul>
</div>
Tried adding active into the class attribute?
<div class="content active" data-section-content>
The section is placed as a horizontal nav-bar and I'd like for it to
be shown when the page loads as if the user has already clicked on it.
You can achieve this by giving you menu items an id and open that menu in jquery.
Section 1
then add this to the script section of your page:
$(function () {
$("#menu1").click();
});

jQuery tab and content outside the current webpage

Bit of a silly question here but here goes... say I wish to use the jQuery tabs but instead of my tabs pointing to DIVs within the same page I want to point them to pages within my site or even external pages... for example
<div id="tabs">
<ul>
<li>Page 1</li>
<li>Profile</li>
<li>Photos</li>
<li>Page 3</li>
<li>Page 4</li>
</ul>
<div id="profile">
<p>Profile</p>
</div>
<div id="photos">
<p>Photos</p>
</div>
</div>
For the tabs pointing to Page 3 and Page 4 would I have to use an IFrame or a Div with an iFrame within to display the desired content? For example
<div id="tabs">
<ul>
<li>Page 1</li>
<li>Profile</li>
<li>Photos</li>
<li>Page 3</li>
<li>Page 4</li>
</ul>
<div id="profile">
<p>Profile</p>
</div>
<div id="photos">
<p>Photos</p>
</div>
<div id="page3">
<iframe></iframe>
</div>
</div>
or
<div id="tabs">
<ul>
<li>Page 1</li>
<li>Profile</li>
<li>Photos</li>
<li>Page 3</li>
<li>Page 4</li>
</ul>
<div id="profile">
<p>Profile</p>
</div>
<div id="photos">
<p>Photos</p>
</div>
<iframe id="page3"></iframe>
If this question makes no sense or my wording is bad please say and I'll improve my question.
UPDATE **
Sorry I should add that when using the original code (the first block) the tabs that reference pages and not DIVs are not shown when the user uses IE8. IE9 seems fine... my problem was risen when testing in IE8! However the first tab (pointing to Page1) is displayed when the page loads... when I click on the Page 3 or Page 4 links nothing is shown...
If you are using jQueryUI Tabs, you could use select or show event for this. For example:
$( ".selector" ).tabs({
show: function(event, ui) {
// do your stuff with changing attribute `src` of your iframe
}
});
Just use an iFrame: <iframe id="page3"></iframe>
Have you tried this and run into some kind of problem?

How to add div dynamically in tabs using JQuery Mobile

I have an application which uses phonegap and jquery mobile and it has tabs like this
<div data-role="header">
<div data-role="tabs">
<ul>
<li>Tab 1</li>
<li>Tab 2</li>
<li>Tab 3</li>
</ul>
</div>
</div>
<div data-role="content">
<div id="tab-1">
<h2>Here is the second tab</h2>
</div>
<div id="tab-2">
<h2>Here is the second tab</h2>
</div>
<div id="tab-3">
<h2>Here is the third tab</h2>
</div>
</div>
Now i need to do the following things
How to add a div dynamically to any of the tab?
How to show progress bar for about 10 seconds and then display the result.
You can dynamically add DOM elements with jQuery like this:
$('#container').append("<div></div>");
For jQuery-Mobile, you'd have to call refresh on any library specific constructs, such as listviews.

Categories