Javascript page refresh disabled

Disable HTML auto-refresh?

A client’s website has an auto-refresh feature which works great for desktop sites, but I’m making an iPhone app and need to disable it on the mobile version. The code used is:

I would like to use javascript to disable it, if possible. Thanks. EDIT: I DO NOT have access to the HTML file, and therefore can’t modify it. I need to do this via code on the Objective-C side in Xcode.

3 Answers 3

I will introduce something simple

Add meta tag with ID id=»meta-refresh» like this:

 var iOS = false, p = navigator.platform; if( p === 'iPad' || p === 'iPhone' || p === 'iPod' ) if(iOS) < // check if iOS then do the following var mr = document.getElementById("meta-refresh"); mr.parentNode.removeChild(mr); >

Also, the initiated request which JavaScript can not disable once loaded!! For the same, the exact work around one can find in an Old Post answer given by user XP1

The above is using xmlhttp requrest (AJAX) to check before the document is loaded and remove the meta tag if the device is target device(iphone)

one can use refresh dynamically id the device is not iPhone/iOS. that will remove the requirement of doing the dynamic check and requirement to avoid first refresh call. without using meta tag

 var iOS = false, p = navigator.platform; if( p === 'iPad' || p === 'iPhone' || p === 'iPod' ) if(!iOS)< window.setTimeout(function(),30000); //30 Seconds > 

Источник

Stop form refreshing page on submit

How would I go about preventing the page from refreshing when pressing the send button without any data in the fields? The validation is setup working fine, all fields go red but then the page is immediately refreshed. My knowledge of JS is relatively basic. In particular I think the processForm() function at the bottom is ‘bad’. HTML

$(document).ready(function() < // Add active class to inputs $("#prospects_form .boxsize").focus(function() < $(this).addClass("hasText"); >); $("#form_validation .boxsize").focus(function() < $(this).parent().addClass("hasText"); >); // Remove active class from inputs (if empty) $("#prospects_form .boxsize").blur(function() < if ( this.value === "") < $(this).removeClass("hasText"); >>); $("#form_validation .boxsize").blur(function() < if ( this.value === "") < $(this).parent().removeClass("hasText"); >>); /////////////////// // START VALIDATION $("#prospects_form").ready(function() < // DEFINE GLOBAL VARIABLES var valName = $('#form_name'), valEmail = $("#form_email"), valEmailFormat = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[8\.4\.9\.7\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]))$/, valMsg = $('#form_message'), valCaptcha = $('#form_captcha'), valCaptchaCode = $('.form_captcha_code'); // Generate captcha function randomgen() < var rannumber = ""; // Iterate through 1 to 9, 4 times for(ranNum=1; ranNum// Apply captcha to element valCaptchaCode.html(rannumber); > randomgen(); // CAPTCHA VALIDATION valCaptcha.blur(function() < function formCaptcha() < if ( valCaptcha.val() == valCaptchaCode.html() ) < // Incorrect valCaptcha.parent().addClass("invalid"); return false; >else < // Correct valCaptcha.parent().removeClass("invalid"); return true; >> formCaptcha(); >); // Remove invalid class from captcha if typing valCaptcha.keypress(function() < valCaptcha.parent().removeClass("invalid"); >); // EMAIL VALIDATION (BLUR) valEmail.blur(function() < function formEmail() < if (!valEmailFormat.test(valEmail.val()) && valEmail.val() !== "" ) < // Incorrect valEmail.addClass("invalid"); >else < // Correct valEmail.removeClass("invalid"); >> formEmail(); >); // Remove invalid class from email if typing valEmail.keypress(function() < valEmail.removeClass("invalid"); >); // VALIDATION ON SUBMIT $('#prospects_form').submit(function() < console.log('user hit send button'); // EMAIL VALIDATION (SUBMIT) function formEmailSubmit() < if (!valEmailFormat.test(valEmail.val())) < // Incorrect valEmail.addClass("invalid"); >else < // Correct valEmail.removeClass("invalid"); >> formEmailSubmit(); // Validate captcha function formCaptchaSubmit() < if( valCaptcha.val() === valCaptchaCode.html() ) < // Captcha is correct >else < // Captcha is incorrect valCaptcha.parent().addClass("invalid"); randomgen(); >> formCaptchaSubmit(); // If NAME field is empty function formNameSubmit() < if ( valName.val() === "" ) < // Name is empty valName.addClass("invalid"); >else < valName.removeClass("invalid"); >> formNameSubmit(); // If MESSAGE field is empty function formMessageSubmit() < if ( valMsg.val() === "" ) < // Name is empty valMsg.addClass("invalid"); >else < valMsg.removeClass("invalid"); >> formMessageSubmit(); // Submit form (if all good) function processForm() < if ( formEmailSubmit() && formCaptchaSubmit() && formNameSubmit() && formMessageSubmit() ) < $("#prospects_form").attr("action", "/clients/oubc/row-for-oubc-send.php"); $("#form_send").attr("type", "submit"); return true; >else if( !formEmailSubmit() ) < valEmail.addClass("invalid"); return false; >else if ( !formCaptchaSubmit() ) < valCaptcha.parent().addClass("invalid"); return false; >else if ( !formNameSubmit() ) < valName.addClass("invalid"); return false; >else if ( !formMessageSubmit() ) < valMsg.addClass("invalid"); return false; >else < return false; >> >); >); // END VALIDATION ///////////////// >); 

Источник

Читайте также:  Функции php возвращает элементы массива

Disable F5 and browser refresh using JavaScript

I want to disable browser refreshing using JavaScript. Currently, I am using window.onbeforeunload and I don’t want it to be called when user refreshes the browser. What is the best way to do it?

11 Answers 11

Update A recent comment claims this doesn’t work in the new Chrome . As shown in jsFiddle, and tested on my personal site, this method still works as of Chrome ver 26.0.1410.64 m

This is REALLY easy in jQuery by the way:

jsFiddle

// slight update to account for browsers not supporting e.which function disableF5(e) < if ((e.which || e.keyCode) == 116) e.preventDefault(); >; // To disable f5 /* jQuery < 1.7 */ $(document).bind("keydown", disableF5); /* OR jQuery >= 1.7 */ $(document).on("keydown", disableF5); // To re-enable f5 /* jQuery < 1.7 */ $(document).unbind("keydown", disableF5); /* OR jQuery >= 1.7 */ $(document).off("keydown", disableF5); 

On a side note: This only disables the f5 button on the keyboard. To truly disable refresh you must use a server side script to check for page state changes. Can’t say I really know how to do this as I haven’t done it yet.

On the software site that I work at, we use my disableF5 function in conjunction with Codeigniter’s session data. For instance, there is a lock button which will lock the screen and prompt a password dialog. The function «disableF5» is quick and easy and keeps that button from doing anything. However, to prevent the mouse-click on refresh button, a couple things take place.

  1. When lock is clicked, user session data has a variable called «locked» that becomes TRUE
  2. When the refresh button is clicked, on the master page load method is a check against session data for «locked», if TRUE, then we simple don’t allow the redirect and the page never changes, regardless of requested destination
Читайте также:  Ru key ru setup html

TIP: Try using a server-set cookie, such as PHP’s $_SESSION , or even .Net’s Response.Cookies , to maintain «where» your client is in your site. This is the more Vanilla way to do what I do with CI’s Session class. The big difference being that CI uses a Table in your DB, whereas these vanilla methods store an editable cookie in the client. The downside though, is a user can clear its cookies.

Источник

Prevent user from refreshing the page

I want to prevent the user from refreshing the page, How to do this? I have been using e.preventDefault method but it`s not running properly.

You need to send e in your event properly, like this: $(someElement).someAction(function(e)<>); or use return false in the end of your method

4 Answers 4

you can use the window.onbeforeunload even.

window.onbeforeunload = function()

The HTML specification states that authors should use the Event.preventDefault() method instead of using Event.returnValue to prompt the user.

window.addEventListener('beforeunload', function (e) < // Cancel the event e.preventDefault(); // If you prevent default behavior in Mozilla Firefox prompt will always be shown // Chrome requires returnValue to be set e.returnValue = ''; >); 
  

Some basic explanations about these features

It is not advised, but if you want to completely prevent the user from refreshing the page in any way possible you can use:

Not only will it prevent refreshing, but it will also prevent navigation.

Linked

Hot Network Questions

Subscribe to RSS

To subscribe to this RSS feed, copy and paste this URL into your RSS reader.

Site design / logo © 2023 Stack Exchange Inc; user contributions licensed under CC BY-SA . rev 2023.7.27.43548

By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.

Источник

Оцените статью