Is it possible to automatically redirect the site to a special page with a video background when the browser width is

Although I typically focus on Media Queries, I need to come up with a different strategy.

On my website, I have a full-screen video background that is rendered in VP8 & VP9 codec using Adobe After Effects animations. Everything looks great in 1080 x 1920 resolution, but when I scale down my browser to 1366 x 768 (Laptop), the site appears poorly as the videos are heavily cropped.

In an attempt to address this issue, I created a separate HTML file and rendered a special video in 480p resolution. This solution actually looks quite good. However, I'm unable to automatically redirect users to this version if they scale down the browser in real-time.

I require a script that can monitor the width of the browser continuously and direct users to the dedicated site where the videos are optimized at 480p resolution (in webm, .mp4, .ogv formats) instead of 720p. Unfortunately, implementing this cannot be achieved through Media Queries alone.

In my HTML code:

<link href="css.css" rel="stylesheet" type="text/css">
<script src="http://code.jquery.com/jquery-2.0.1.js"></script>
<script type="text/javascript" src="js/redirect.js"></script>
<body>
<div id="big"><video preload="auto" autoplay loop muted="muted" volume="0">
<source src="video/of.webm" type='video/webm; codecs="vp9"'></video></div>
<div id="menu"><a href="intro.html" class="ex2-fadeout" >In</a> <a href="cut.html" class="ex2-fadeout">Ofe</a> <a href="portfolio.html" class="ex2-fadeout">Pf</a> <a href="kontacy.html" class="ex2-fadeout">Contact</a></div>
</body></html>

In my redirect.js file, I am utilizing the following script:

$(window).resize(function () { 
    /* call some function */
});

var width = $(window).width();
var height = $(window).height();

$(document).ready(function(){
    //this is called only once
    r($(window).height());
});

$(window).resize(function () { 
    //this is called everytime when you resize window
    r($(window).height());
});

function r(h) {
    if (h > 1024) window.location.replace("http://google.com"); //this is edited
    return 0;
}

What am I doing wrong here?

Answer №1

After reading the previous comment, it seems like you are already familiar with using jQuery, so let's dive right in. jQuery provides an easier way to handle the DOM event onResize():

$(window).resize(function () { 
    /* call some function */
});

To retrieve the actual width and height of the window, you can use the following code:

var width = $(window).width();
var height = $(window).height();

Putting it all together, here is the code snippet: copy code from here

$(document).ready(function(){
    //this is called only once
    r($(window).height());
});

$(window).resize(function () { 
    //this is called every time the window is resized
    r($(window).height());
});

function r(h) {
    if (h > 1024) window.location.replace("http://google.com"); //this is edited
    return 0;
}

to here

Check out this fiddle.


It's important to note the difference between the size of the document and the window:

$(window).height();   // returns height of browser viewport
$(document).height(); // returns height of HTML document
$(window).width();   // returns width of browser viewport
$(document).width(); // returns width of HTML document

For more information, check here.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Concealing the nearest div that has a particular class

I'm currently developing an application that simulates desktop-style application windows with the ability to open, minimize, and close. While I have successfully implemented the functionality to expand and collapse these windows, I am facing difficult ...

The challenge of integrating Bootstrap with Mixitup filtering on page load

Looking to showcase our portfolio in bootstrap using MixItUp v3.1.11 filtering, and attempting to load a specific category (not all projects) upon page load. Patrick Kunka has shared an example of achieving this here. A related question was asked here O ...

Utilizing the Google Geocode API to handle a promise with a substantial array

My Objective To efficiently process a large array using the .map method and interact with the Google Geocoder API through promises to get location data. The goal is to utilize Promise.all to store results in a .json file upon completion of the operation. ...

Tips for transferring parameters using a href tag without causing a page refresh

Is there a way to pass parameter values without refreshing the page? I would appreciate any help. Thank you. searchresult.php <a href="index.php?id=<?php echo $data['ID']?>">clickme</a> index.php <?php echo $_GET['id ...

Matching the characters '/#' in a href can be achieved by using regular expressions

I am using the following code to check if the href attribute starts with # and based on that add a class: $('.navbar-nav a').each(function(index, element) { if($(element).attr("href").match("^#")) { //Add class to internal links ...

Remove newline character from HTML code using Python 3.2 and urllib module

After fetching HTML content as a string using urllib, I encountered difficulty in searching the string due to its HTML format. Is there any way to "unformat" the string by removing all the new lines without altering the HTML code itself? Here is the Pytho ...

Unexpectedly, the child component within the modal in React Native has been resetting to its default state

In my setup, there is a parent component called LeagueSelect and a child component known as TeamSelect. LeagueSelect functions as a React Native modal that can be adjusted accordingly. An interesting observation I've made is that when I open the Lea ...

Issue with JQUERY where HTML select element is not visible

$(document).ready(function(){ var selArr = [ {val: 'corsair', text: 'Corsair'}, {val: 'evga', text: 'EVGA'}, {val: 'antec', text: 'Antec'} ]; $('#pSupply& ...

JSON only retrieve the corresponding data

I am looking to send a JSON object back to Postman without including a "title" like: { "name": { "name": "Three Rivers Campground", "lengthLimit": 25, "elevation": 6332, ...

"Encountering a Reactjs error when trying to render a

Currently, I am engrossed in a React JS tutorial but seem to have hit a roadblock with the following error: Failed to compile. Error in ./src/Components/Projects.js Syntax error: Unexpected token (15:10) return { <ProjectItem key={projec ...

Explain and suggest the necessary parameters for a function

Is there a way to clearly describe the parameters required by my function and make them visible when I am typing my code? For instance, if we consider the ExpressJS render function below, it effectively shows what the callback expects and will return. In ...

Expanding a div with CSS and Angular: A comprehensive guide

For my project, I am working on a specific scenario: When the user clicks on the "Animals" tile, it should expand while the other tiles replace it. Similarly, clicking on the "Plants" tile should expand it and reorder the other tiles. ===========1st View ...

Switching between javascript objects within the redux store and the application

I am working with a set of JavaScript objects that are retrieved from an external API library. My goal is to save these objects in my React application using Redux. These objects are ES2015 classes that include two useful methods called fromJSON and toJSO ...

What is the best way to keep the menu bar in place as you scroll?

Can someone help me figure out how to keep the menu bar fixed at the top of the page when the user scrolls down? I've tried various methods but haven't had any luck. Any assistance would be greatly appreciated. Here is the CSS code: And here is ...

Having trouble with AutoCompleteExtender OnClientItemSelected not functioning in IE8 but working perfectly in IE9? Let's dive into the JavaScript substring

My AutoCompleteExtender is connected to a web service and works perfectly. The Target TextBox (tb_provider1) has autocomplete capabilities thanks to the GetProviders function. What I want to achieve is triggering a javascript function when a user selects a ...

Turn on and off jquery tabs depending on user's choice

Currently, I am utilizing jQuery tabs to showcase specific data. However, I aim to offer users the flexibility to choose whether they prefer to view the content as tabs or in a sequential manner. Even after attempting to manipulate the ui-tabs classes ba ...

What is the best way to select and style individual HTML elements using CSS?

Having trouble editing a Validations Form in Ionic 4 with CSS. It seems that only the form elements are targeted after clicking on the form group. Attached below are the form states before and after click. I'm unable to style the form elements before ...

Issue encountered when trying to retrieve a database variable from a mapReduce operation in MongoDB

Greetings! I am currently developing an application that utilizes a MongoDB database. Within this database, there exists a user collection where all user data is stored. The structure of a document in this collection is as follows: { "_id" : ObjectId( ...

Align the <div> vertically within the <td> tag

My current set up looks something like this: <tr> <td> <div class="blue">...</div> <div class="yellow">...</div> </td> <td> <div class="blue">...</div> <div class="yellow ...

Adjust the vertical size of a table cell

I am currently working on code that was written by someone else and I am facing an issue with a 3 block element that needs to be modified. My goal is to change the height of the first block so that it only goes as big as the image inside it. Despite my e ...