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.


Script code

Add the code in the page you want to refresh it according to window width. Then, to test it, rezise the browser.

<h4>Resize the window</h4>

<script>
/* JS code to refresh the page if window is changed (from: https://coursesweb.net/ ) */
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);
</script>

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

HTML
CSS
JavaScript
PHP-MySQL
Click on the HTML tag which creates an horizontal line in web page.
<br /> <em> <hr />
Some content ...
<hr />
Content under line ...
Which CSS property defines the text color?
font-style font-variant color
h2 {
  color: #cbdafb;
}
Click on the function which searches if a character, or text exists in a string.
indexOf() toString() split()
var str = "Web courses - http://CoursesWeb.net/";
if(str.indexOf("http://") == -1) alert("http:// isn`t in string");
else alert("http:// is in string");
Which function splits a string into an array of strings based on a separator?
array_merge() explode() implode()
$str = "apple,banana,melon,pear";
$arr = explode(",", $str);
var_export($arr);      // array (0=>"apple", 1=>"banana", 2=>"melon", 3=>"pear")
Refresh page if window width changes from a device size to other

Last accessed pages

  1. Input text fields (2285)
  2. Insert, Select and Update NULL value in MySQL (30219)
  3. Register and show online users and visitors (25571)
  4. JavaScript Slideshow Content (855)
  5. Get Attribute (ID, Class, Name, Title, Src) with jQuery (55241)

Popular pages this month

  1. Insert, Select and Update NULL value in MySQL (515)
  2. Read Excel file data in PHP - PhpExcelReader (430)
  3. PhpSpreadsheet - Read, Write Excel and LibreOffice Calc files (319)
  4. PHP getElementById and getElementsByTagName (255)
  5. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (234)