Jump to content

[Problem] Script causes the page to automatically refresh [Problem]


Recommended Posts

I don't know what's wrong with my script, when I login into a page it redirects me to the admin page normally but the admin page keeps refreshing! I've located the problem which is in my javascript but I don't know what's wrong with it.

 

Here is the javascript code:

$(document).ready(function() {

	(function($){  
		$.fn.avu = function() {  

		$.post('backend/build_table.php', { 'session_id' : sessid }, function(data) {

			// login check
			try {
				JSON.parse(data);
			} catch (e) {
				location.href = './index.php';
				return;
			}

			data = jQuery.parseJSON(data);

			var tbl = "";
			$.each(data, function(id, row) {
				$('.tooltip').hide();
				$("tbody tr").remove();

				var tbl_row = "";
				var hash;
				var name;
				$.each(this, function(k , v) {

						if(k=='hash'){hash=v;}
						if(k=='name'){name=v;}

						if(k=='filepath' || k=='results'){return;}

						else if(k=='size'){ tbl_row += '<td class="'+k+'">'+bytesToSize(v)+"</td>\n"; }
						else if(k=='scanned')
						{ 
							// set status
							if(v=='0')
							{
								v = 'In queue';
							}
							else if(v=='1' && row['results'] == null)
							{
								v = 'Clean';
							}
							else if(v=='1' && row['results'] != null)
							{
								v='';
								var results = jQuery.parseJSON( row['results']);
								v+='<ul>';
								$.each(results, function(kr,r) {								
									v+='<li class="small"><span class="tt" title="Scanned with: '+r.scanner+'">'+r.result+ '</span></li>' + "\n";
								});
								v+='<ul>';
							}


							tbl_row += '<td class="'+k+'">'+v+"</td>\n"; 
						}
						else{ tbl_row += '<td class="'+k+'">'+v+"</td>\n"; }
				})

				// add extra row
				var durl = 'download/'+hash+'/'+name;
				tbl_row +=  '<td><a href="'+durl+'" target="blank"><img src="assets/img/download.png" class="tt bdown" title="Download file" /></a>'+
							'<img src="assets/img/delete.png" class="tt bdel"title="Remove file" /></td>';
				
				tbl += '<tr id="'+row['id']+'">'+tbl_row+"</tr>\n";

			});

			// build
			$("tbody").html(tbl);
			$('.tt').tooltip({'delay':4});

			 // remove file
			$(".bdel").click(function() {

				var id= $(this).parent().parent().attr('id');
				
				$.post("backend/delete_file.php", { id: id, 'session_id' : sessid },
				function(data) {
					$('tr[id="'+id+'"]').remove();
					$('.tooltip').hide();

					//rebuild
					setTimeout(function() {
						$('tbody').avu();
					}, 2000);


				});

			});

		});

		};
	})(jQuery);  

	// build and refresh table
	$('tbody').avu();
	setInterval(function() {
		$('tbody').avu();
	}, 20000);

	/**
	 * Convert number of bytes into human readable format
	 *
	 * @param integer bytes     Number of bytes to convert
	 * @return string
	 */
	function bytesToSize(bytes) {
		var sizes =['Bytes','KB','MB','GB','TB'];

		if(bytes==0) return 'n/a';
		var i=parseInt(Math.floor(Math.log(bytes)/Math.log(1024)));
		return Math.round(bytes/Math.pow(1024,i),2)+' '+sizes[i];
	}

});

What causes the page to refresh every 2 seconds? and how can I fix this?

 

Thanks in advance :)

Perhaps it's failing at this line, and therefore redirects you to ./index.php ?

// login check
try {
	JSON.parse(data);
} catch (e) {
	location.href = './index.php';
	return;
}

- W

Edited by yomanny

Perhaps it's failing at this line, and therefore redirects you to ./index.php ?

// login check
try {
	JSON.parse(data);
} catch (e) {
	location.href = './index.php';
	return;
}

- W

 

Thanks for the help, fixed it now everything works properly :)

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.