Jquery Scripts stop working after infinite scroll loads new pages - javascript

i am trying to load one script which is working fine on first page but once the infinite scroll load the new page that js functionality is not showing up there.
<script>
$(document).ready(function(){
var original = $('.load_more_text a').attr('href');
var new_href = original.replace('/page/', '/?page=');
$('.load_more_text a').attr('href', new_href);
$('.prod_meta span.winned-for').text('Sold');
$('.prod_meta span.winned-for').parent().addClass('green');
$('.prod_meta span.winned-for').parent().parent().parent().addClass('green_pr');
})
</script>
what am i missing ?

Call the function after infinite scroll loads
example
<script>
$(document).ready(function(){
//YOUR SCRIPT infinite scroll load
$.ajax({
url: newUrl,
type: "GET",
data: data,
cache: false,
success: function (data, textStatus, xhr) {
loadtext(); //call the function after infinite scroll loads
},
});
function loadtext(){
var original = $('.load_more_text a').attr('href');
var new_href = original.replace('/page/', '/?page=');
$('.load_more_text a').attr('href', new_href);
$('.prod_meta span.winned-for').text('Sold');
$('.prod_meta span.winned-for').parent().addClass('green');
$('.prod_meta span.winned-for').parent().parent().parent().addClass('green_pr');
}
})
</script>

Related

Track click over iframe by Javascript / Jquery

how can we track clicks on iframe (like any advt.) and record that event in my own database (with ajax) according to website users respectively with advt id and user id
i have tried onclick (like below) event on parent div but not working -
$('div.ad').on('click', function(){
$.ajax({
url: 'clickevent.php',
type: 'POST',
data: {
'id': adId
},
dataType: 'json',
success: function(data){
// show success msg to user
},
error: function(){
// show failure msg
}
});
});
i tried focus and blur while cursor is over that iframe
<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
<script>
jQuery(function( $ ){
var isOverAd = false;
$( "iframe[ id *= google ]" )
.mouseover(function(){
isOverAd = true;
})
.mouseout(function(){
isOverAd = false;
});
$( window ).blur(
function(){
if (isOverAd){
$.ajax({
url: 'clickevent.php',
type: 'POST',
data: {
'id': adId
},
dataType: 'json',
success: function(data){
//show success msg
},
error: function(){
//show failure msg
}
});
}
}).focus();
});
i have also tried for when window loss blur while mouse on that iframe
i have mapped pixels ($0 values) it is also not working.
but nothing is working ......i will be very thankfull if you help me
To access an frame in page you can do this:
var frame = window.frames['FRAME-ID'];
$(frame).load(function ()
{
$(frame).contents().find('ELEMENT-IN-FRAME');
//To manage click on element body ( or any elements):
$(frame).contents().find('body').on('click', function(){alert('k')});

Masonry items not reloaded when cliking ajax load more button

Hi everyone i have one problem about masonry items.
I have created this DEMO from codepen.io
In this demo you can see there is this javascript code:
$(window).load(function()
{
$( function() {
var $container = $('.posts-holder');
$container.masonry({
isFitWidth: true,
itemSelector: '.kesif-gonderi-alani'
});
});
});
I show only 10 posts when a page is opened. If user want to show other 10 posts then user needs to click (show more button). I created this ajax function for show more posts.
$('.showmore').live("click",function(event)
{
event.preventDefault();
var ID = $(this).attr("id");
var P_ID = $(this).attr("rel");
var URL=$.base_url+'diger_fotograflar_ajax.php';
var dataString = "lastid="+ ID+"&profile_id="+P_ID;
if(ID)
{
$.ajax({
type: "POST",
url: URL,
data: dataString,
cache: false,
beforeSend: function(){ $("#more"+ID).html('<img src="wall_icons/ajaxloader.gif" />'); },
success: function(html){
$("div.posts-holder").append(html).each(function(){
$('.posts-holder').masonry('reloadItems');
});
$("#more"+ID).remove();
}
});
}
else
{
$("#more").html('The End');// no results
}
return false;
});
this code working when clicking showmore button $('.posts-holder').masonry('reloadItems'); but collecting new posts in one place. But when I change the width of the page everything is improving.
I think that you can use $container.masonry(); after adding your elements, like this :
$("div.posts-holder").append(html).each(function(){
$('.posts-holder').masonry('reloadItems');
});
$container.masonry();
You can see it working here.
Hope that can help.
you have to use the appended method of masonry ... otherwise how would it know that you have added any new element.
Adding the elements simply wont align them as masonry doesnt have any event listner for new element added.
var el = $('<div class="kesif-gonderi-alani" style="height:300px;"></div>')
$container.masonry().append( el ).masonry( 'appended',el );
Hers is small demo on codepen http://codepen.io/knaman2609/pen/xbJMRY
Click on the button to append elements dynamically
http://masonry.desandro.com/methods.html

jQuery script delayed by AJAX request - how to fix it?

I'm making a simple responsive Wordpress Theme and I want only one page there, and the content loaded via AJAX, so I have an AJAX function:
jQuery(document).ready(function(){
jQuery.ajaxSetup({cache:false});
jQuery("#mainnav a").click(function(){
var post_link = jQuery(this).attr("href");
jQuery("body").load(post_link);
return false;
});
});
It's meant to load entire pages content (entire body) on clicking menu links. And it works well.
But the problem is I have a jQuery script responsible for centering content div:
(function (jQuery) {
jQuery.fn.centerAlign = function() {
return this.each(function(i){
var w = jQuery(this).width();
var ow = jQuery(this).outerWidth();
var ml = (w + (ow - w)) / 2;
var h = jQuery(this).height();
var oh = jQuery(this).outerHeight();
var mt = (h + (oh - h)) / 2;
jQuery(this).css("margin-top", "-" + mt + "px");
jQuery(this).css("top", "50%");
jQuery(this).css("margin-left", "-" + ml + "px");
jQuery(this).css("left", "50%");
jQuery(this).css("position", "absolute");
});
};
})(jQuery);
jQuery(document).ready(function() {
jQuery("#content").centerAlign();
});
That script centers the #content div at reload instatly, so I mean there is no delay. The div is centered from the start.
Problem is when I combine that scripts, because after AJAX loads the entire body, #content div is not positioned for ~1 second (in top-left corner of the page) and then it comes back to its place. So jQuery script works with a delay.
I coulnd't find a solution for that problem, .on and .live don't work for me. I think I could just load each page content into a #content div, not entire "body", but I don't know how to make it.
I'd appreciate if you can help me with that, so it will just resize the #content div with animation, not position it from start.
EDIT:
Ok, I ended up with such code:
jQuery(function(){
jQuery("#mainnav a").click(function(e){
var content=jQuery("#content");
jQuery.ajax({
url: jQuery(this).attr('href'),
dataType: "HTML",
beforeSend: function(){
content.empty();
},
success: function(){
content.load();
},
error : function(){
content.html("<p>Przepraszamy, ale strona jest chwilowo niedostępna</p>");
},
});
e.preventDefault();
})
})
It should work, but the problem is that I can't figure out what should I put in that lines:
success: function(){
content.load();
It workd up to that point. I clears the #content div but when I put content.load("#content"); it loads entire page, with menu and footer. And I want to load just #content content.
EDIT:
Ok. Firebug shows that this code is working, but there is nothing shown within the #content div in broswer window.
jQuery(function(){
jQuery("#mainnav a").click(function(e){
var content=jQuery("#content");
var href=jQuery(this).attr('href');
var text = jQuery(this).find('#content').html();
jQuery.ajax({
url: jQuery(this).attr('href'),
dataType: "HTML",
beforeSend: function(){
content.empty();
},
success: function(){
content
.html(text)
.centerAlign();
},
error : function(){
content.html("<p>Content Unavailable</p>");
},
});
e.preventDefault();
})
})
I think you're on the right track with your new code, you just need to fill out your success function:
success: function( html ){
html = jQuery(html).find('#content').html();
content.html(html).centerAlign();
},
or perhaps (using th1rdey3's idea):
success: function( html ){
html = jQuery(html).find('#content').centerAlign().html();
content.html( html );
},
You could centerAlign #content before placing it inside body. Something like this
var $div = jQuery('<div/>').load(post_link);
$div.find('#content').first().centerAlign();
jQuery('body').html($div.html());
or only loading the content part
var $div = jQuery('<div/>').load(post_link);
jQuery('#content').first().html($div.find('#content').first().html());

When this ajax jquery trigger I want a image that loads for 1 second

I have this code
<script type="text/javascript">
$(function () {
$(".a").live("click", function () {
var $this = $(this),
ProductImageId = $this.data('ProductImageId');
$.ajax({
url: '/Home/AddToCart',
type: "post",
cache: false,
data: { ProductImageId: ProductImageId },
success: function (data) {
data = eval(data);
$this.addClass('productSelected');
},
error: function (result) {
$(".validation-summary-errors").append("<li>Error connecting to server.</li>");
}
});
});
Every time someone click on this button I want a image to load for like 1 sec
so the user knows that something happened..
any kind of help is appreciated
Something like:
var $loader = $('<img src="loader.gif">').appendTo(document.body);
And then
success: function (data) {
setTimeout(function() {
$loader.remove();
data = eval(data);
$this.addClass('productSelected');
},1000);
}
It will show the image for 1sec + the ajax loading time. Although, as a visitor of your site I would probably prefer to skip the 1sec delay...
Add before send like this :
beforeSend: function(){
$("some element").append("<img src='url' id='one_sec_img' />");
SetTimeout(1000, function(){
$("#one_sec_img").hide();
})
}
But, I think your requirement is something normal :
Here, bcoz of the before send, as soon as you click button that triggers ajax, the image will be loaded, and on success, the image can be hidden . This is the usual process, in this case,setTimeout is not needed . But, if your requirement is something like, the image should appear only one sec, you can use set timeout. Otherwise, below code is enough.
beforeSend: function(){
$("some element").append("<img src='url' id='one_sec_img' />");
},
success: function(res){
$("#one_sec_img").hide();
}
<script type="text/javascript">
$(function () {
$(".a").live("click", function () {
var html=$(this).html();
$(this).html('loading');//<img src="" />
var $this = $(this),
ProductImageId = $this.data('ProductImageId');
$.ajax({
url: '/Home/AddToCart',
type: "post",
cache: false,
data: { ProductImageId: ProductImageId },
success: function (data) {
$(".a").html(html);
data = eval(data);
$this.addClass('productSelected');
},
error: function (result) {
$(".validation-summary-errors").append("<li>Error connecting to server.</li>");
}
});
});
try this.
Use jQuery BlockUI Plugin to show the image, may be it solve your problem.
Try:
$(".a").click( function( ) {
$("#idOfImage").show().delay( 1000 ).hide();
// ajax call
});
else if you only want the image to disappear only after the ajax call
$(".a").click( function( ) {
$("#idOfImage").show();
$.ajax({
// parameters
}).done( function( ) {
$("#idOfImage").hide( );
});
});

jQuery, Shadowbox and AJAX

I would like to load some content using AJAX and Shadowbox
Basically I would like the user to goto a page in case javascript is off. So this is what I want to do :-
1) Cancel the click event i.e the User must not go to a different page.
2) Load content using ajax function in jQuery
3) Show the content inside a shadow box
My code works ok until loading content using ajax but the shadowbox is not displayed and the URL is gettin refreshed i guess, everything goes blank.
jQuery(document).ready(function($){
// rounded corners
$('img').addClass('corner iradius16');
DD_roundies.addRule('#nav li a',4,true);
DD_roundies.addRule('.grid',6,true);
$('h2 a').bind('click', function() {
var id = $(this).attr('id');
$.ajax({
url: "/ajax-post.php?p="+id,
success: function(data){
Shadowbox.open({
content: data,
player: "html",
height: 350,
width: 350
});
}
});
return false;
});
UPDATE 1
tried this code, and as soon as the shadowbox loads, the document gets reloaded and white.
Shadowbox.init({
skipSetup: true,
players: ["html"]
});
// LOAD
jQuery(document).ready(function($){
// rounded corners
$('img').addClass('corner iradius16');
DD_roundies.addRule('#nav li a',4,true);
DD_roundies.addRule('.grid',6,true);
$('.post h2 a').bind('click', function() {
var id = $(this).attr('id');
$.ajax({
url: "<?php bloginfo( 'template_directory'); ?>/ajax-post.php?p="+id,
success: function(data){
show_box(data);
}
});
return false;
});
});
//shadowbox
function show_box(html) {
Shadowbox.open({
content: html,
player: "html",
height: 350,
width: 350
});
}
UPDATE 2
Okay got the prbolem, the html that I am getting via ajax has some javascript in it and that is the reason for the page reload.
Why is this happening ? Any ideas ?
Since you're not getting any JavaScript errors try debugging by breaking it down:
Ensure that the binding to and overriding of the click event is functioning properly:
$('h2 a').bind('click', function() {
alert('click even fired');
return false;
});
If that works, check the data that your ajax request is returning:
$('h2 a').bind('click', function() {
var id = $(this).attr('id');
$.ajax({
url: "ajax-post.php?p="+id,
success: function(data){
alert(data);
}
});
return false;
});
The code looks like it should work, so I'm guessing there's either something wrong elsewhere (in which case the first test will likely fail) or you're getting some really odd data returned.
Need to run
Shadowbox.setup('h2 a');
This will reinitialise and bind it to any ajax loaded content

Categories