I have this code and I am woundering how to change it so that it will get the information without you having to use the click function.
Image from Visual Studio
if (PREVIEW == 1) {
$(this).before('<span class="prev-img" alt="Preview" title="Preview">🔍</span>');
var linkurl = $(this).attr('href');
$(this).prev('.prev-img').on('click', function() {
$('.am_preview').each(function () {
$(this).removeClass('am_preview');
});
$(this).next('a').addClass('am_preview');
$.ajax({
type: "get",
url: "xmlhttp.php?action=load_preview&linkurl="+encodeURI(linkurl),
async: true,
success: function(response) {
$('.am_preview').outerHTML(response.html);
$(".am_embed").css("max-width", MAXWIDTH+"px");
},
error: function (response) {
$('.am_errorMessage').each(function () {
$(this).hide();
});
$('.am_preview').after('<div class="am_errorMessage">' + NOPREVIEW + '</div>');
}
});
setTimeout(function() {
$('.am_errorMessage').each(function () {
$(this).fadeOut('fast');
});
}, 6000);
$(this).hide();
});
}
// Embed from sites with oembed api
if (file.indexOf('soundcloud.com/') !== -1) {
$(this).scembed();
$(this).prev('.prev-img').hide();
}
Related
iam trying to run my function again if the status of my json false but its not working
$(document).ready(function(){
function checkIfCompleted(){
$('.btn').click( function(e) {
e.preventDefault();
var url = "https://localhost/check/1";
$.ajax({
url: url,
success: function(response) {
if(response.status === false){
checkIfCompleted();
}else{
alert(done);
}
},
});
});
}
checkIfCompleted();
});
You are adding click multiple times to the button, you are not rerunning the Ajax call
$(document).ready(function() {
function checkIfCompleted(cnt) {
var url = "https://localhost/check/1";
$.ajax({
url: url,
success: function(response) {
if (response.status === false) {
if (cnt >= 5) {
throw new Error('too many retries');
} else {
checkIfCompleted(++cnt);
}
} else {
alert(done);
}
},
});
}
$('.btn').click(function(e) {
e.preventDefault();
checkIfCompleted(0);
});
});
i use find_all but there is some problem on my data.
i only need text, but js script was copied too
crawled js data is like this.
how can i remove this?
window.__mugFnList__.push(function() { !function($, window) { if (mug.common.isDev()) { console.log("asyncGet funding"); } function asyncGet() { $.ajax({ url: "getSe3FundingView.nhn", dataType:"html", data: { authorNo: "11166748", seriesNo: "368342" }, success: function(resultHtml){ if (mug.common.isDev()) { console.log("asyncGet funding success!!"); } $.trim(resultHtml); $("#__tmp_fundingView").replaceWith(resultHtml); $(window).trigger("viewerFundingLoad"); }, error: function(resp) { console.log(resp); } }); } $(window).load(function() { setTimeout(asyncGet, 1); }); }(jQuery, window); });
window.__mugFnList__.push(function() {
!function($, window) {
if (mug.common.isDev()) {
console.log("asyncGet event");
}
function asyncGet() {
$.ajax({
url: "getEventView.nhn",
dataType:"html",
data: {
volumeNo: "13440747",
memberNo: "11166748"
},
success: function(resultHtml){
if (mug.common.isDev()) {
console.log("asyncGet event success!!");
}
$.trim(resultHtml);
$("#__tmp_eventView").replaceWith(resultHtml);
"") {
try {window.__viewer._resizePan();}catch(e){};
}
$(window).trigger("viewerEventLoad");
},
error: function(resp) {
console.log(resp);
}
});
}
$(window).load(function() {
setTimeout(asyncGet, 1);
});
}(jQuery, window);
});
$(document).ready(function () {
setInterval(function () {
$('#RoomBlock').children('div').each(function () {
ShowRoom($(this).attr('id'));
});
}, 10000);
function ShowRoom(id) {
$.ajax({
type: "POST",
url: "/localhost/GetData/" + id
success: function (response) {
if (response != null) {
$('#' + id).empty();
$('#' + id).append(response);
} else {
alert("null");
}
},
error: function (response) {
alert("error!");
}
});
})
i have 5 table and they always fetch data from server for several time, success times for every table is different, i want to put some loading bar on every table when they make a request. is there any way to do that with javascript ?
the one that make confuse is how to know that the first table is make a request and still while the other one was done.
try something like below
$(document).ready(function () {
setInterval(function () {
$('#RoomBlock').children('div').each(function () {
ShowRoom($(this).attr('id'));
});
}, 10000);
function ShowRoom(id) {
/* Add some loader here (bar or image)*/
var loaderHtml = "<div id='loader_'+id>_____Add your loader code here_____</div>";
$('#' + id).append(loaderHtml);
$.ajax({
type: "POST",
async: false,
url: "/localhost/GetData/" + id
success: function (response) {
if (response != null) {
$('#' + id).empty();
$('#loader_'+ id).remove();
$('#' + id).append(response);
} else {
alert("null");
$('#loader_'+ id).remove();
}
},
error: function (response) {
alert("error!");
}
});
})
When i click on add-to-basket button i see an error which appears in my browser console saying :
Here is my basket.js file :
$(document).ready(function() {
initBinds();
function initBinds() {
if ($('.remove_basket').length > 0) {
$('.remove_basket').bind('click', removeFromBasket);
}
if ($('.update_basket').length > 0) {
$('.update_basket').bind('click', updateBasket);
}
if ($('.fld_qty').length > 0) {
$('.fld_qty').bind('keypress', function(e) {
var code = e.keyCode ? e.keyCode : e.which;
if (code == 13) {
updateBasket();
}
});
}
}
function removeFromBasket() {
var item = $(this).attr('rel');
$.ajax({
type: 'POST',
url: '/home/u919084925/public_html/mod/basket_remove.php',
dataType: 'html',
data: ({ id: item }),
success: function() {
refreshBigBasket();
refreshSmallBasket();
},
error: function() {
alert('An error has occurred');
}
});
}
function refreshSmallBasket() {
$.ajax({
url: '/home/u919084925/public_html/mod/basket_small_refresh.php',
dataType: 'json',
success: function(data) {
$.each(data, function(k, v) {
$("#basket_left ." + k + " span").text(v);
});
},
error: function(data) {
alert("An error has occurred");
}
});
}
function refreshBigBasket() {
$.ajax({
url: '/home/u919084925/public_html/mod/basket_view.php',
dataType: 'html',
success: function(data) {
$('#big_basket').html(data);
initBinds();
},
error: function(data) {
alert('An error has occurred');
}
});
}
if ($(".add_to_basket").length > 0) {
$(".add_to_basket").click(function() {
var trigger = $(this);
var param = trigger.attr("rel");
var item = param.split("_");
$.ajax({
type: 'POST',
url: '/home/u919084925/public_html/mod/basket.php',
dataType: 'json',
data: ({ id : item[0], job : item[1] }),
success: function(data) {
var new_id = item[0] + '_' + data.job;
if (data.job != item[1]) {
if (data.job == 0) {
trigger.attr("rel", new_id);
trigger.text("Remove from basket");
trigger.addClass("red");
} else {
trigger.attr("rel", new_id);
trigger.text("Add to basket");
trigger.removeClass("red");
}
refreshSmallBasket();
}
},
error: function(data) {
alert("An error has occurred");
}
});
return false;
});
}
function updateBasket() {
$('#frm_basket :input').each(function() {
var sid = $(this).attr('id').split('-');
var val = $(this).val();
$.ajax({
type: 'POST',
url: '/home/u919084925/public_html/mod/basket_qty.php',
data: ({ id: sid[1], qty: val }),
success: function() {
refreshSmallBasket();
refreshBigBasket();
},
error: function() {
alert('An error has occurred');
}
});
});
}
// proceed to paypal
if ($('.paypal').length > 0) {
$('.paypal').click(function() {
var token = $(this).attr('id');
var image = "<div style=\"text-align:center\">";
image = image + "<img src=\"/images/loadinfo.net.gif\"";
image = image + " alt=\"Proceeding to PayPal\" />";
image = image + "<br />Please wait while we are redirecting you to PayPal...";
image = image + "</div><div id=\"frm_pp\"></div>";
$('#big_basket').fadeOut(200, function() {
$(this).html(image).fadeIn(200, function() {
send2PP(token);
});
});
});
}
function send2PP(token) {
$.ajax({
type: 'POST',
url: '/mod/paypal.php',
data: ({ token : token }),
dataType: 'html',
success: function(data) {
$('#frm_pp').html(data);
// submit form automatically
$('#frm_paypal').submit();
},
error: function() {
alert('An error has occurred');
}
});
});
I tried to resolve it but couldn't find a proper solution.
Help me with this, I cannot understand the cause of this error.
This is mainly due to Rules of Origins (CORS), for some reason the javascript(browser) sees the request as not residing in the same server. And the reason for that, I believe, is because /home/u919084925/public_html/mod/basket.php is not seen as a valid url on the server, it should start with http://{hostname}/{path}.
It looks like your ajax url is totally wrong and the browser interpret that is cross origin ajax request. Please simply check in browser's address bar if your ajax provided urls are valid.
I am using this function to add or remove products from favorites.
When I add or remove a product from favorites a div pops out with a message.
I have a problem with the queue animation.
Does anyone knows a way to fix this?
function addFavorite(code, action) {
var website = 'http://localhost';
var cod = code;
var action = action;
var $this = $j(this);
if (action == 'removeFav') {
$j.ajax({
type: 'post',
url: '/ajax/handler.favorite.php?action=removeFav',
data: {
'cod': cod
},
beforeSend: function() {
$j('.topMessage').show();
$j('.topMessage span').html('<img src="' + website + '/assets/loader.gif" alt="loading..">');
$j('.topMessage span').animate({
top: "+=80px",
}, 500);
},
success: function(data) {
$j('.topMessage span').html(data);
$j('.topMessage span').delay(3000).animate({
top: "-=80px",
}, 500);
}
});
}
if (action == 'addFav') {
$j.ajax({
type: 'post',
url: '/ajax/handler.favorite.php?action=addFav',
data: {
'cod': cod
},
beforeSend: function() {
$j('.topMessage').show();
$j('.topMessage span').html('<img src="' + website + '/assets/loader.gif" alt="loading..">');
$j('.topMessage span').animate({
top: "+=80px",
}, 500);
},
success: function(data) {
$j('.topMessage span').html(data);
$j('.topMessage span').delay(3000).animate({
top: "-=80px",
}, 500);
}
});
}
}
You can do something like this:
Create a function to display your messages and animate your div.
function displayAddedMessage(message) {
var span = $j('.topMessage span');
var addedMessage = span.parent();
var wrapper = addedMessage.parent();
addedMessage.css('top', -85).hide();
if (message) {
span.html(message);
}
var clonedAddedMessage = addedMessage.clone(false);
addedMessage.remove();
wrapper.prepend(clonedAddedMessage);
clonedAddedMessage.show().delay(100).animate({
top: 10
}, 500).delay(3500).animate({
top: -100
}, 500);
}
You can use this function in:
function addFavorite(code, action) {
var website = 'http://localhost';
var cod = code;
var action = action;
var $this = $j(this);
if (action == 'removeFav') {
$j.ajax({
type: 'post',
url: '/ajax/handler.favorite.php?action=removeFav',
data: {
'cod': cod
},
beforeSend: function() {
$j('.topMessage').show();
displayAddedMessage('<img src="' + website + '/assets/loader.gif" alt="loading..">');
},
success: function(data) {
$j('.topMessage span').html(data);
displayAddedMessage(data);
}
});
}
if (action == 'addFav') {
$j.ajax({
type: 'post',
url: '/ajax/handler.favorite.php?action=addFav',
data: {
'cod': cod
},
beforeSend: function() {
$j('.topMessage').show();
displayAddedMessage('<img src="' + website + '/assets/loader.gif" alt="loading..">');
},
success: function(data) {
$j('.topMessage span').html(data);
displayAddedMessage(data);
}
});
}
}
You can achieve like this :
$j('.topMessage span').stop().animate({
top: "-=80px",
}, 500);
Refer documentation.
hope this will solve your problem
first create a global variable
var isAnimating = false; // a global variable
then change your ajax request code with this one
$j.ajax({
type: 'post',
url: '/ajax/handler.favorite.php?action=removeFav',
data: {
'cod': cod
},
beforeSend: function() {
$j('.topMessage').show();
$j('.topMessage span').html('<img src="' + website + '/assets/loader.gif" alt="loading..">');
iF(!isAnimating) { // check animation is not running
isAnimating = true;
$j('.topMessage span').animate({
top: "+=80px",
}, 500);
}
},
success: function(data) {
$j('.topMessage span').html(data);
$j('.topMessage span').delay(3000).animate({
top: "-=80px",
}, 500, function() {
isAnimating = false; // this callback will called after animation complete
});
}
});