How to load dynamic content (jquery) inside ajax call? - php

My homepage loads pages with jquery ajax call. In the Pictures subpage there is 3 div, and each loads a php with ajax, too. I would like to include a gallery js to every sub-subpage. However, the js does not loads. Here is my code in the index.php for the subpages:
<script type="text/javascript">
$(function()
{
var actualmenu = 'fooldal.html';
$('#tartalom').load('fooldal.html');
$('.fooldal').click(function()
{
$('#tartalom').load('fooldal.html');
actualmenu = 'fooldal.html';
}
);
$('.kepek').click(function(){
$('#tartalom').load('kepek.php', function(){
$(document.body).on('click', 'a[rel=gallery_view]' ,function(){
$(this).KeViewer('gallery', {'percent' : 70});
return false;
});
});
actualmenu = 'kepek.php';
});
});
</script>
And there is my kepek.php page:
<script type="text/javascript">
$(function()
{
$('#galeria').load('kepek_csenge.php');
$('#csenge').click(function()
{
$('#galeria').load('kepek_csenge.php');
}
);
);
</script>
<div id="albums">
<div class="album" id="csenge">
Csenge
<br/>
<img src="albumok/csenge/01.jpg" alt="csenge"/>
</div>
</div>
<div style="clear:both;"></div>
<div id="galeria" width="500"></div>
Inside kepek_csenge.php there are rows like this, which should trigger the gallery:
<a class="thumb_wrapper" href="albumok/csenge/01.jpg" title="Első" rel="gallery_view"><img alt="" class="thumb" src="albumok/csenge/thumbs/01.jpg" /></a>
Unfortunately it just loads a new page with the selected picture. How can i use the galleryviewer js in this page?

It is important to understand that the document you are loading into was ready long before you load anything with ajax
This means that the code wrapped in $(function() in your ajax pages will fire as soon as it is encountered. If that code precedes the html it references then it won't find those elements since they don't exist yet.
Try moving all the script tags in the ajax loaded content to after the html.
It is generally easier to put all the scripts into one and wrap different parts in functions and then just call those functions in the success callback of load();
function loadPage(page){
var url = '...' +page;
$('#selector').load(url, function(){
initViewer();
});
}

Related

How to reload a parent page on closing a overlay lightbox (wordpress)?

I need to refresh a parent page when closing a overlay lightbox (not popup window).
The lightbox has no close button, it's closed by clicking anywhere outside the lightbox.
I tried the following code, but it doesn't refresh the parent page.
Would you plase let me know how to refresh the parent page?
HTML:
<div class="dwqa-ask-question">
<a href="https://www.myweb.com/ask-questions/" rel="lightbox" data-lightbox-type="iframe">Ask Question
</a>
</div>
Lightbox:
<div class="nivo-lightbox-overlay nivo-lightbox-theme-default nivo-lightbox-effect-fade nivo-lightbox-open">
<div class="nivo-lightbox-wrap">
<div class="nivo-lightbox-content">
<iframe src="https://www.mycom.com/ask-questions/" class="nivo-lightbox-item" frameborder="0" vspace="0" hspace="0" scrolling="auto"></iframe>
</div>
<div class="nivo-lightbox-title-wrap"></div>
</div>
Previous
Next
Close
</div>
Added the following code in a lightbox page (=ask-questions page):
$(document).ready(function () {
if (window.opener) {
window.close();
}
if (window != top) {
top.location.replace(document.location);
}
});
jQuery(document).ready(function refreshParentWindow(){
window.opener.location.href="/{!$CurrentPage.parameters.id}";
window.top.close();
});
jQuery(document).ready(function (){
var rel = window.opener.location;
window.close();
rel.reload();
});
Thank you.
"Responsive Lightbox" is the plugin i'm using for the lightbox.
There are multiple ways to set this up. I personally always use additional css classes to show and/or hide my html elements on the page, but since you're currently using a plugin, then you could add an event listener to your page.
So use this javascript on your page:
jQuery(document).ready($ => {
console.log("You just loaded your js file onto the page"); // Testing whether you've loaded your js file correctly!
$(document).on("click", e => {
if (
document.querySelector('.nivo-lightbox-overlay').contains(e.target)
&&
!document.querySelector(".nivo-lightbox-wrap").contains(e.target)
) {
console.log("You just clicked outside of the lightbox wrap"); // Testing event listener
location.reload(); // a simple refresh
// OR
// location.reload(true); // a hard refresh
};
});
});
You see the orange area around your light-box wrap with nivo-lightbox-overlay class? Click anywhere on it an it'll reload your page!
Let me know if you were able to get it to work!
Another solution
jQuery(document).ready( $ => {
$(document).on("click", "div.nivo-lightbox-overlay", () => {
if ($("div.nivo-lightbox-overlay").length) {
location.reload(); // a simple refresh
// OR
// location.reload(true); // a hard refresh
}
})
});

Pagination with $_GET - in AJAX - is it posible

I'm developing a PHP class for pagination using $_GET. It is standart, found from the web.
Here it works good :
page.php :
<form method ="GET">
<?php
$pages = new Pagination();
echo "<br/>";
?>
</form>
I want to use this page.php in index.php with ajax / jquery and staying in the index.php
<!DOCTYPE html>
<body>
<div id ="result"></div>
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<script>
$(document).ready(function() {
$.post('./page.php',
function (data) {
$('#result').html(data);
}
);
});
</script>
</body>
</html>
Is this possible way ?
Is it possible that instead of using jquery's $.post, that you can replace $.post with $.get?
So instead of $.post as you said its looking for $_GET['page']
So you could do something like this:
<script>
$(document).ready(function(e) {
var page_num = 1;
$('.nextpage').on('click',function(e){
e.preventDefault(); // stop the link from going anywhere
$.get('./page.php',
{
page: page_num // this is the same as $_GET['page']
},
function (data) {
$('#result').html(data);
page_num++;
}
);
});
$('.nextpage').click(); // emulate the click to get the first page
});
</script>
and in your body something like this:
Next page
It's worth noting that on your page.php you don't need to have that form as i cannot see it's going to be doing much
UPDATE
So to have the pagination manipulated on the index.php from page.php you could have page.php return a hidden div called .hidden_pagination along with its full content.
<script>
$(document).ready(function(e) {
$('.pagination').on('click','a',function(e){
e.preventDefault(); // stop the link from going anywhere
var next_page = $(this).attr('data-id'); // get the next page from the link data-id attribute
$.get('./page.php',
{
page: next_page // this is the same as $_GET['page']
},
function (data) {
$('#result').html(data);
$('.pagination').html($('#result').children('.hidden_pagination').html()); // add the new pagination to the current pagination
}
);
});
$('.nextpage').click(); // emulate the click to get the first page
});
</script>
<div class="pagination">
Next page
</div>
<div id="result">
this will be replaced with the ajax response
</div>

How can I use jQuery effects on Ajax loaded content?

Hi and thanks for taking some time to look at my question. I have a part of the page where content is dynamicly loaded into from another file. Reason for this is it needs to be live updated. Now I want to be able to apply jquery effects that are usually used for show/hiding content (slide, fade etc) to animate the difference between the current data and the new data. This is the code used to get the content and load it into the div:
function k() {
$.post("../includes/ajaxAgenda.php", {
limit : value
}, function(data) {
$('#tab-agenda').html(data);
});
};
$(document).ready(function() {
k();
$('#tab-agenda').scroll(function() {
loadMore();
});
});
var refreshId = setInterval(function() {
k();
}, 1000);
So I guess my question is how do I animate what gets loaded in so it doesn't just "pop" from one content to another?
edit: I tried using the .live instead of .scroll but it doesn't seem to work:
$(document).ready(function() {
$('#tab-agenda').live("scroll",function() {
alert("hi");
loadMore();
});
});
You need to use live function of jquery to bind the dynamically added elements.
Ref: http://api.jquery.com/live/
Try this :
$('#tab-agenda').live("scroll",function() {
loadMore();
});
I suggest you to add the ajax loader image with proper css over the content/ div as like below.
function loadmore(){
$("#loader").css('display','block');
//your
//code
//here
$("#loader").css('display','none');
}
html
<img id="loader" src="ajax-loader.gif" style="display:none" />
<div id="content">
your cont to display
</div>

call jquery function wordpress

Below code is jquery function
$(document).ready(function () {
$('#access').accordion({
create: function (event, ui) {
$('div#access> div').each(function () {
var id = $(this).attr('id');
$(this).load(id+'.html', function () {
$('#access').accordion('resize');
});
});
}
});
});
where access is the div id i have used for menu items in header.
below code is header.php page code
<div id="access" >
<ul>
<?php wp_list_pages('depth=1&title_li=');?>
</ul>
</div>
what i want is onclick of the menu item the javascript function should be called..
how to call function from php file?
You don't call the JavaScript function from PHP. The PHP merely enables you to build the HTML page dynamically. Once the page is ready, the JavaScript is called and it starts binding the events to the appropriate elements.
What you need to do is look at the generated code using the 'view source' or firebug and explore the structure of the generated HTML and then you can bind the event to the requested element.

Load pages using jQuery/Ajax in the same div that loads a page every 6 sec?

On my index.php I have a header, sidebar, footer and the main part of it is the <div id="feed"> that loads engine.php every 6000 ms.
I have a Contact page ( contact.php ) in my sidebar. Instead of copying my index.php to a new page, with header, sidebar, footer and a main div for the contact content, can I load it in the #feed div of index.php withour refreshing the site in the browser?
To summarize it, my question is, is there any way that my pages to be loaded on the same div ( #feed) without refresh and freeze the setTimeout timer?
When the user click back on Home, then the engine.php is loaded and reloaded every 6 seconds.
Maybe this can be done with Ajax, I don't know...
Thank you for this and any examples/codes are highly appreciated.
<script language="JavaScript">
$(function () {
function loadfeed() {
$('#feed')
.addClass('loading')
.load('engine.php', function () {
$(this).removeClass('loading');
setTimeout(loadfeed, 6000);
});
}
loadfeed();
});
</script>
Update
Having something like this works, but the engine.php loads after 6 sec.
$("#contactBtn").click(function() {
$("#feed").load('contact.php');
});
I have no way of fully testing this, but you try something like this.
<script language="JavaScript">
var timerID;
$(function () {
function loadfeed() {
$('#feed')
.addClass('loading')
.load('engine.php', function () {
$(this).removeClass('loading');
timerID = setTimeout(loadfeed, 6000);
});
}
$("#contactBtn").click(function() {
clearTimeout(timerID);
$("#feed").load('contact.php');
$("#feedBtn").bind('click', loadfeed);
});
loadfeed();
});
</script>
The key here is the use of a global timerID variable and the clearTimeout() function.
If this works, you can include a Return to feeds button with id="feedBtn" in contact.php, but you’ll have to bind the loadfeed function to the button’s click event after loading it.
Without interrupting the timeout cycle, the contact form will display for a maximum of 6 seconds inside #feed before the next $.load request finishes.
If you want to leave the timeout cycle going, rather than putting everything in #feed, you can give it an appropriate sibling:
<div id="panels">
<div id="feed">
<!-- ... -->
</div>
<div id="contact" style="display:none">
<!-- ... -->
</div>
</div>
Then, switch which is currently displaying:
$('a.contact').click(function (e) {
e.preventDefault();
$('#contact').show();
$('#panels > div:not(#contact)').hide();
});
$('a.feed').click(function (e) {
e.preventDefault();
$('#feed').show();
$('#panels > div:not(#feed)').hide();
});
The feed will continue to load into #feed, while the contact page can display uninterrupted.
Also, if you supply a clue on your links, you can combine those click handlers with fair ease:
<div id="menu">
Feed
Contact
</div>
<script>
$('#menu > a').click(function (e) {
e.preventDefault();
var panel = $(this).data('panel'); // or $(this).attr('data-panel') for jQuery 1.4 or older
$('#' + panel).show();
$('#panels > div:not(#' + panel + ')').hide();
});
</script>
You can load a file with $.load
$("#contactBtn").click(function() {
$("#feed").load('contact.html') No need for a callback or the timeout.
}
Check the jquery docs if you're looking for something more specific. Hope that helps.

Categories