The JavaScript code snippet presented in this page can be used to refresh the page if the window width is changed from a device size (mobile, or tablet, or desktop size) to other.
- For examples, if you have in your page some content with different settings for desktop, tablet, and mobile size; if the user resizes the window from full size to a lower window-width (or viceversa), the page is refreshed so to have the content according to current browser's width. - Here is the code, add it in the page you want to refresh it according to window width. To test it, rezise the browser.
Click on the code to select it.
/* JS code to refresh the page if window is changed (from: ) */
function winSize() {
 // returns an object with height and width of the window
  var win_size = {};
  if (self.innerHeight) {
    win_size.height = self.innerHeight;
    win_size.width = self.innerWidth;
  } else if (document.documentElement && document.documentElement.clientHeight) {
    win_size.height = document.documentElement.clientHeight;
    win_size.width = document.documentElement.Width;
  } else if (document.body) {
    win_size.height = document.body.clientHeight;
    win_size.width = document.body.clientWidth;
  return win_size;
var win_size = winSize();

// set indice "m" for mobile (max. 400px), "t" for tablet (max. 700px), "d" for desktop, according to window size
var dev_i = (win_size.width <400) ? 'm' : ((win_size.width <700) ? 't' :'d');

// when Resize browser, check window-width; refresh if current device indice not initial device indice
window.addEventListener('resize', function(e){
  var win_size2 = winSize();
  var dev_i2 = (win_size2.width <400) ? 'm' : ((win_size2.width <700) ? 't' :'d');
  if(dev_i2 != dev_i) window.location.replace(window.location.href);
}, false);
The script refreshes the page according to these two sizes: 400px, and 700px. If you want to have one size that determines the refresh, modify the variables: var dev_i and var dev_i2.
For example:
var dev_i = (win_size.width <500) ? 'm' : 'd';
var dev_i2 = (win_size2.width <500) ? 'm' :'d';

Daily Test with Code Example

Which tag is used to add definition lists into a <dl> element?
<dt> <dd> <li>
  <dd> - Hyper Text Markup Language</dd>
  <dd> - Language for web pages</dd>
Which CSS property can hide an element on page, letting an empty space in its place?
display position visibility
#id {
  visibility: hidden;
Click on the event which is triggered when the mouse clicks on an object.
onclick onmouseover onfocus
document.getElementById("id").onclick = function(){
Indicate the PHP variable that contains the contents of both $_GET, $_POST, and $_COOKIE arrays.
if(isset($_REQUEST["id"])) {
  echo $_REQUEST["id"];
Refresh page if window width changes from a device size to other

Last accessed pages

  1. SHA512 Encrypt hash in JavaScript (9327)
  2. My New Room 2 (353)
  3. Ajax-PHP Rating Stars Script (12781)
  4. Voting Poll System script PHP-AJAX (5823)
  5. querySelector and querySelectorAll (6543)

Popular pages this month

  1. Flash Games - Free online Games (1360)
  2. Qwop (1158)
  3. Tunnel Rush (1062)
  4. Courses Web: PHP-MySQL JavaScript Ajax HTML CSS Flash-AS3 (983)
  5. Drag Racer V3 (758)