JavaScript for Altering Viewport/Screen Width

Lately, I've been experimenting with creating a responsive button for our new website. The goal is to adjust the body width to 460px, simulating how our site would appear on mobile devices. While researching, I came across a technique that utilizes iframes – by adjusting the iframe width, the viewport width changes accordingly, thus achieving the desired responsiveness effect.

My challenge lies in implementing this using vanilla JavaScript. Although setting the body width is straightforward, Bootstrap and CSS media queries seem to ignore this custom width as they are based on viewport/screen width, which remains constant.

So, the question is: How can I get this to work? I have buttons representing mobile and desktop views, and they should be able to adjust the screen's width dynamically while ensuring that the media queries respond appropriately.

Is there a way to accomplish this? For context, my viewport is defined as:

<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

and the media queries are structured like this:

@media screen and (min-width: 992px) {}

Answer №1

Assuming I understand your situation correctly, I have two recommendations for you.

  • To open a new window that is fully loaded in the desired width and height, you can utilize the open function. You may refer to this link for more information: here

    var option_string = "location=yes,width=640,scrollbars=yes,status=yes";
    var URL = "https://your_link.com";
    var win = window.open(URL, "_blank", option_string );
    
  • Instead of loading the entire page, consider using an iframe to display the specific page you intend to embed. Upon clicking the Desktop or Mobile button, adjust the width of the iframe accordingly based on the button clicked. Refer to the example below for clarification:

    <body>
        <iframe id="your_site" src="https://your_link.com">
        <button class="btn btn_desktop">Desktop</button>
        <button class="btn btn_mobile">Mobile</button>
        <script>
            var $iframe = document.getElementById('your_site');
            var $resize_button = document.getElementByClass('btn');
            $resize_button.on('click', function(e){
                var $button = e.target;
                if (button.classList.contains('btn_desktop')) { // enlarge your frame
                    $iframe.style.width = '640px'; 
                } else {
                    $iframe.style.width = 'auto';
                }
            })
        </script>
    </body>
    

I hope these suggestions prove helpful to you.

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

Searching within an Angular component's DOM using JQuery is restricted

Want to incorporate JQuery for DOM manipulation within Angular components, but only want it to target the specific markup within each component. Trying to implement Shadow DOM with this component: import { Component, OnInit, ViewEncapsulation } from &apo ...

Error encountered: Unexpected token '"', expecting "}". Perspective from React : It is an unfortunate occurrence which h

I'm a newbie when it comes to React and I'm currently following a tutorial. Unfortunately, I encountered an error message while the script was trying to compile: Parsing error: Unexpected token, expected "}" The issue seems to be with the &apos ...

Which is better: triggering mouseleave from inside or outside of mouseenter event

As I delve into the basics of jQuery, I stumbled upon mouseenter and mouseleave actions. The question that arose in my mind is: where should I place the mouseleave action? Which approach is more correct and reliable? $(document).ready(function(){ $(&a ...

In Vue(tify), transitions require children to have keys, but in this case, there are no tags present as children

Trying to implement a transition on a list using Vue and Vuetify, but encountering the error message vue.runtime.esm.js?2b0e:619 [Vue warn]: <transition-group> children must be keyed: <v-card> I attempted the Vuetify approach <v-fade-transi ...

Issue with $_SERVER['PHP_SELF'] not functioning properly

Apologies if this question has been asked before, but after searching extensively I couldn't find a solution. Therefore, I am posting here... The issue is that I'm trying to submit values on the same page (using jQuery Mobile UI) and I have used ...

Jquery draggable droppable does not support displaying multiple divs simultaneously

I tried to implement the jquery ui draggable and droppable features to display 3 divs within a designated area. Below is the code snippet: --CSS: #content-1 { width: 200px; height: 100px; border: 1px solid red; display: none; } #content-2 { width: ...

Utilizing JavaScript in Protractor, explore a list to identify the selected checkboxes and those that remain unchecked

Currently, I am working on developing an end-to-end test using Protractor. My main objective is to extract the names from a list and then determine which checkboxes have been selected and which ones haven't. To check the state of the checkbox, I pla ...

There appears to be a slight issue with the tailwind dark mode not fully extending when scrolling to the bottom of the page

While using dark mode in a Next.js web app with Tailwind, you may have noticed that when scrolling, if you go past the scroll container (almost as if you're bouncing off the bottom or top of the page), the dark mode doesn't extend all the way. In ...

Using router-links with events: A simple guide

My current issue involves passing information to another page. During testing without using routes, I was successful in passing information from one component to another. However, after implementing routes, clicking the event navigates to the other compo ...

Having an issue with retrieving value from a textfield in JavaScript

<input id="checkOldPassword" type="button" title="Check New Password" value="Check New Password" onclick="checkPassword()" /> <input id="newPassword" type="text" maxlength="8" min="8" /> <script language="javascript"> function checkPassw ...

Managing the display of my website's screenshots within the list of recently visited sites on the browser's new tab page

Internet browsers like Firefox and Chrome have the ability to take screenshots of visited websites and display them as "recently used websites" on a new tab. However, if your website contains confidential information, you may be wondering how to prevent b ...

The problem with the CSS Grid effect

Looking for assistance in creating a grid layout similar to the one on this website: Upon inspecting the page source, I found the following code snippet: http://jsfiddle.net/o45LLsxd/ <div ng-view="" class="ng-scope"><div class="biogrid ng-scope ...

Creating a responsive table layout with CSS

I currently have the following code: .fiftyFiftySection { background-color: #000; } .odometer { font-size: 3em; text-align: center; } table td { column-width: 1000px; text-align: center; } @media (max-width: 500px) { table td { column ...

"Move a div towards the mouse's location by animating it with JavaScript when

I've been working on making the ball element move and shrink towards the goals upon mouse click. I successfully created a function that captures the mouse click coordinates in the goals, but I'm facing challenges with the ball animation. I consi ...

Error message: The "spawn" function is not defined and is causing a TypeError to be thrown in

Having a bit of trouble here. I'm trying to make an async request using redux-thunk in my action creator, and the code looks like this: export const downloadFromYoutube = (download) => { console.log("Hello"); return dispatch => { va ...

When working with Next.js Components, be aware that using a return statement in a forbidden context can lead to

Whenever I try to add a new component to my Next.js project, I encounter an error that displays the following: `./components/GridMember.js Error: error: Return statement is not allowed here | 6 | return (test); | ^^^^^^^^^^^^^^^^^^^^^^^^^ Caused ...

The image displays successfully on desktop, however, it fails to load once the website is deployed on GitHub or Netlify

While developing this website on my Mac, I encountered an issue where images load fine when I copy the project path from Atom to Chrome. However, once I push the same code to GitHub and then publish it on Netlify, the image fails to load. Does anyone kno ...

Analyze Javascript code and monitor every variable alongside their corresponding values

After watching Bret Victor's engaging talk "Inventing on Principle" the other night, I was inspired to create a real-time JavaScript editor similar to the one he showcased. You can see a glimpse of it in action at 18:05 when he demonstrates binary sea ...

How can you add draggable functionality to a Bootstrap dropdown menu?

My custom bootstrap dropdown design <div class="dropdown"> <button class="btn btn-default dropdown-toggle" type="button" id="dropdownMenu1" data-toggle="dropdown" aria-haspopup="true" aria-expanded="true"> Dropdown <span cla ...

Can you tell me the name of this specific page arrangement style?

What is the term for the specific layout style used on Asana's homepage? This layout consists of a single, long page divided into sections that are approximately the size of the viewport. While sometimes it includes parallax scrolling, in this case i ...