I am loading in my navigation with jquery from a nav.html file so I don't have to update it on every page each time it updates.
I am trying to target elements in that loaded nav to be active when on certain pages and it only works when the nav is hard pasted into the page's html but it doesnt when I load it in from the nac-content.html file.
I did noticed when i view the source code on browser it doesnt actually paste in the html but still displays it which I think is the disconnect but I am not 100%. Not sure if I should look into different way to load it in or if my jquery is the issue.
Any insight would be greatly appreciated.
HTML:
<nav>
<div id="sidebar">
<div id="sidebar-content">
<!-- nav loads here from nav-content.html -->
</div>
</div>
</nav>
Jquery:
/*Loads the Nav */`
window.onload = function(){
$.get("nav-content.html", function(data){
$("#sidebar-content").html(data);
})
}
/* changes classes within the loaded nav based on what page it's on */
$(document).ready(function() {
$('ul#devSubmenu').removeClass('collapse'),
$('ul#appStackSubmenu').removeClass('collapse'),
$('a[href^="#devSubmenu"]').attr("aria-expanded","true"),
$('a[href^="#appStackSubmenu"]').attr("aria-expanded","true");
});
You could do something like this
$(document).ready(function () {
// DOM has been parsed
$.get("your/url", function (data) {
var $navContainer = $("#sidebar-content");
$navContent = $(data); // build a jQuery object from your html markup
// do your updates using the find method of jQuery objects
$navContent.find("ul#devSubmenu").removeClass("collapse");
// ...
$navContainer.append($navContent); // assuming the container is empty.
});
});
window.onLoad gets executed after $(document).ready
You could use
document.addEventListener("DOMContentLoaded", function(event) {
$.get("nav-content.html", function(data){
$("#sidebar-content").html(data);
})
});
Related
I am loading in the content of my sidebar navigation with jquery from a nav-content.html file so I don't have to update it on every page each time it updates.
What I am trying to do is when on a specific page, the sidebar nav with uncollapse based on what category that page is on.
I am trying to target elements in that loaded nav to be active when on certain pages and it only works when the nav is hard coded into the page's html but it doesn't work when I load it in from the nav-content.html file with jquery.
I did noticed when i view the source code on browser it doesn't actually paste in the html but still displays it which I think is the disconnect but I am not 100%. Not sure if I should look into different way to load it in or if my jquery is the issue.
Any insight would be greatly appreciated.
HTML:
<nav>
<div id="sidebar">
<div id="sidebar-content">
<!-- nav loads here from nav-content.html -->
</div>
</div>
</nav>
Jquery:
/*Loads the Nav */`
window.onload = function(){
$.get("nav-content.html", function(data){
$("#sidebar-content").html(data);
})
}
/* changes classes within the loaded nav based on what page it's on */
$(document).ready(function() {
$('ul#devSubmenu').removeClass('collapse'),
$('ul#appStackSubmenu').removeClass('collapse'),
$('a[href^="#devSubmenu"]').attr("aria-expanded","true"),
$('a[href^="#appStackSubmenu"]').attr("aria-expanded","true");
});
I asked this a few days ago but this is a rephrased/re-explained of deleted post.
Two things to get you on the right path.
1) jQuery's get() does not load an HTML file. You might mean to use load() to get your sidebar content: https://api.jquery.com/load/
2) $(document).ready fires before window.onload. See: window.onload vs $(document).ready()
In order to ensure that your content is loaded before modifying the classes, you can make your modifications in the callback from load() (the function passed as the second parameter to load()).
Something like [untested]:
$(function() {
$( "#sidebar-content" ).load( "nav-content.html", function() {
$('ul#devSubmenu').removeClass('collapse'),
$('ul#appStackSubmenu').removeClass('collapse'),
$('a[href^="#devSubmenu"]').attr("aria-expanded","true"),
$('a[href^="#appStackSubmenu"]').attr("aria-expanded","true");
});
});
The wrapping $(function() { ... }) is just jQuery shorthand for $(document).ready();
I am a beginner in playing with jQuery/AJAX, my goal is to load content to the div below:
<div id="blogcontentloaded">
</div>
I came up with .load and it worked, the page loads but it keeps refreshing and loads over and over.
$(function loadBlog(e) {
$('#blogcontentloaded').load('/blog/page1.html');
e.preventDefault();
return false;
});
I tried using e.preventDefault but it doesn't work for me.
Also my goal is to do this without any buttons. When main page loads portion of the page that I want to load along with main page is going to be for updating the content in loaded element.
Thanks for the help!
You can use the javascript load function. It may solve your problem. here you can get some information about windows load and jQuery ready functions.
$( window ).on( "load", function() {
$('#blogcontentloaded').load('/blog/page1.html');
});
You need to wrap the function in the 'ready' function and make sure that it is executed once:
$(document).ready(function(){
$('#blogcontentloaded').load('/blog/page1.html');
});
Have you used the jQuery file on the top of other js files?
Add the jQuery.js file
$(document).ready(function(){
$('#blogcontentloaded').load('/blog/page1.html');
e.preventDefault();
return false;
})
I am making a dynamic page that contains several divs that change when the user selects to go to the next passage on my page. However, I cannot get my website to load the first set of information.
The HTML contains these blank divs, and then my function loads in a JSON file and changes the divs by their ID's. When I first load my page, these divs appear without text. Could it be that my javascript is not running when the page is first loaded? Here is an example of my HTML:
<div id="passage-section">
<!-- Title -->
<div id="passage-title"></div>
<!-- The panel that will display the content -->
<div id="passage"></div>
<!-- Button that when clicked activates a dialog box for the passage. -->
<button id="max-passage" class="max"></button>
</div>
And here is my function:
//This section of code handles the calling of the first passage.
//Loading in the JSON file and changing the contents of the page.
function loadFirstPassage()
{
var timeout = 250;
//load the new JSON file and change the elements
$.getJSON("passages/2.1.1.json", function( data ) {
document.getElementById("passage-title").innerHTML = data["passageNumber"];
document.getElementById("passage").innerHTML = "<p class='serif'>".concat(data["reading"]).concat('</p>');\
//fading the elements back in
$("#passage-title").fadeIn(timeout);
$("#passage").fadeIn(timeout);
});
}
I tried calling this function in the body and that did not work, so currently it is getting called in the head.
<head>
<!--The orginal load of the page maybe it works, who knows? -->
<script>
$(window).load(function()
{
loadFirstPassage();
});
</script>
you could try load it right after the DOM finished
(function() {
console.log('DOM Loaded')
})();
jQuery would use
$( document ).ready(function() {
console.log('DOM Loaded');
});
Try
$(document).ready(function() {
loadFirstPassage();
});
instead of $(document).load.
I try to load my content with jquery load function in to the index.html's content area. I succeed with that but my js are not working. So I want to call them on each click of my menu's elements. Is it possible?
click here to see the page
menu:
<div id="nav" class="section group">
<div id="menu" class="col span_9_of_12">
<ul id="navmenu">
<li>Hakkımda</li>
<li>Portfolyo</li>
<li>İletişim</li>
<li>Fotoğraflar</li>
</ul>
</div>
load function:
$(document).ready(function() {
$('#content').load($('#navmenu li a:last').attr('href'));
var hash = window.location.hash.substr(1);
var href = $('#navmenu li a').each(function(){
var href = $(this).attr('href');
if(hash==href.substr(0,href.length-5)){
var toLoad = hash+'.html #content';
$('#content').load(toLoad)
}
});
$('#navmenu li a').click(function(){
var toLoad = $(this).attr('href')+' #content';
$('#content').hide('slow',loadContent);
$('#load').remove();
window.location.hash = $(this).attr('href').substr(0,$(this).attr('href').length-5);
function loadContent() {
$('#content').load(toLoad,'',showNewContent())
}
function showNewContent() {
$('#content').show('slow',hideLoader());
}
function hideLoader() {
$('#load').fadeOut('slow');
}
return false;
});
});
I'm not sure if this is causing your issue, but for all four of the URLs that you are trying to load into the #content div, the content starts out with <div id="content">. You should remove that from the content, since it is getting loaded into <div id="content"></div>. Otherwise, you end up with:
<div id="content">
<div id="content">
...
</div>
</div>
Note that the .load() function does not replace the element it is called on, it inserts the returned html into it's body.
Here is what I see in Firefox when I inspect your page after the content/aboutme.html content is loaded:
Also, if the content you are loading needs to be instrumented with dynamic behavior, you may need to instrument it after inserting it into the DOM. In that case, you need to pass a callback function to the .load() function.
$('#content').load(url, function(responseText, textStatus) {
if (textStatus == "success") {
// Instrument the dynamic behavior of the elements just inserted into
// the #content div here.
carouselfn();
}
});
It sounds to me like you are trying to dynamically load html and javascript. When you use any method of dynamically loading content, including .load(), it only loads static content, meaning it will not parse and execute any javascript that you try to load.
Furthermore, there is no cross-browser way to load and execute javascript dynamically after the page has loaded unless you use eval, which is not recommended.
I would recommend including all of your javascript in the main index.html page, then loading just the html from your other pages dynamically, and then calling your javascript functions needed for that dynamic content.
Edit:
You need to call your library's javascript functions after the html has finished loading. So you would need to supply a callback to the .load() function:
$('#content').load(
$(this).attr('href'),
function () { /* javascript calls here */ }
);
I'm using Phonegap to build a small (test only) Macrumors application, and remote hosts actually work (there is no same host browser restrictions). I am using the jQuery Load() function to load the contents of the Macrumors homepage http://www.macrumors.com/ into a bin, hidden div, then the each function to loop through all the article classes to show the title in a box with a link to the page.
The problem is, after the Macrumors HTML content is loaded, the each function doesn't work with the article class. Also, in the load function (which allows you to specify certain selectors, id's and classes included, to only load in those sections of the page) the class doesn't work; none of the classes do, in both the load function and each function. And many Id's don't work in the each function either.
Can anybody explain this to a noob like me?
Here is the code:
function onDeviceReady()
{
// do your thing!
$('#bin').load('http://www.macrumors.com/ #content');
$('.article').each(function(){
var title = $('a').html();
$('#content').append('<b>'+title+'</b>')
});
}
And the HTML stuff
<body onload="onBodyLoad()">
<div id="bin">
</div>
<div id="content">
</div>
</body>
I sincerely apologize if there's some very simple mistake here that I'm missing; I'm a major JS newbie.
.load() is asychronous. It hasn't completed yet when you're executing .each(). You need to put your .each() and any other code that wants to operate on the results of the .load() in the success handler for .load().
You would do that like this:
function onDeviceReady()
{
// do your thing!
$('#bin').load('http://www.macrumors.com/ #content', function() {
$('.article').each(function(){
var title = $('a').html();
$('#content').append('<b>'+title+'</b>')
});
});
}
I'm also guessing that your .each() function isn't working quite right. If you want to get the link out of each .article object, you would need your code to be like this so that you're only finding the <a> tag in each .article object, not all <a> tags in the whole document:
function onDeviceReady()
{
// do your thing!
$('#bin').load('http://www.macrumors.com/ #content', function() {
$('.article').each(function(){
var title = $(this).find('a').html();
$('#content').append('<b>'+title+'</b>')
});
});
}