Skip to content

Instantly share code, notes, and snippets.

@TheNolle
Last active May 16, 2024 21:00
Show Gist options
  • Save TheNolle/4e7f637467b232775d9491f2e776a210 to your computer and use it in GitHub Desktop.
Save TheNolle/4e7f637467b232775d9491f2e776a210 to your computer and use it in GitHub Desktop.
A quick and effective guide to creating and managing windows with JavaScript, including code samples, limitations, and best practices for providing an enjoyable user experience.

English Version: Click me


Les fenêtres en JavaScript 🖼️🌐🔩

Dans ce guide, nous allons explorer la création et la gestion des fenêtres avec JavaScript. Vous apprendrez comment créer des fenêtres avec des dimensions précises, positionner et déplacer des fenêtres, et en connaître les limitations. Vous trouverez ci-dessous la table des matières pour vous aider à naviguer facilement dans ce guide.

Table des matières 📚

  1. Créer une fenêtre avec des dimensions précises 📏
  2. Définir une taille minimale et maximale pour la fenêtre 📐
  3. Masquer la barre d'outils de la fenêtre 🛠️
  4. Positionner une fenêtre à un emplacement précis 🔍
  5. Déplacer une fenêtre déjà ouverte 🚚
  6. Limitations et bonnes pratiques 🚧

Créer une fenêtre avec des dimensions précises 📏

Pour créer une fenêtre avec des dimensions précises, utilisez la méthode window.open() et spécifiez la largeur et la hauteur de la fenêtre en pixels.

let url = "https://thenolle.com";
let width = 800;
let height = 600;
let windowFeatures = `width=${width},height=${height}`;
let newWindow = window.open(url, "_blank", windowFeatures);

Définir une taille minimale et maximale pour la fenêtre 📐

Utilisez la méthode resizeTo() pour définir une taille minimale et maximale pour la fenêtre.

let minWidth = 400;
let minHeight = 300;
let maxWidth = 1200;
let maxHeight = 900;

newWindow.resizeTo(Math.max(minWidth, Math.min(maxWidth, width)), Math.max(minHeight, Math.min(maxHeight, height)));

Masquer la barre d'outils de la fenêtre 🛠️

Pour masquer la barre d'outils de la fenêtre, utilisez la méthode window.open() et spécifiez toolbar=no dans les options.

let windowFeatures = `width=${width},height=${height},toolbar=no`;
let newWindow = window.open(url, "_blank", windowFeatures);

Positionner une fenêtre à un emplacement précis 🔍

Pour positionner une fenêtre à un emplacement précis de l'écran de l'utilisateur, utilisez la méthode window.open() et spécifiez la position X et Y de la fenêtre.

let posX = 100;
let posY = 200;
let windowFeatures = `width=${width},height=${height},toolbar=no,left=${posX},top=${posY}`;
let newWindow = window.open(url, "_blank", windowFeatures);

Déplacer une fenêtre déjà ouverte 🚚

Pour déplacer une fenêtre déjà ouverte, utilisez la méthode moveTo().

let newX = 300;
let newY = 400;
newWindow.moveTo(newX, newY);

Limitations et bonnes pratiques 🚧

Il est important de noter que certaines de ces méthodes peuvent être considérées comme une mauvaise utilisation car elles peuvent tromper les utilisateurs et les empêcher d'interagir avec le navigateur de manière attendue.

Voici quelques limitations et recommandations :

  • Vous ne pouvez pas empêcher complètement un utilisateur de passer en mode plein écran sur une fenêtre créée avec JavaScript, mais vous pouvez désactiver le mode plein écran pour le contenu de la fenêtre en ajoutant l'attribut allowfullscreen="false" à l'élément <iframe> ou <video>.
  • Vous ne pouvez pas empêcher complètement un utilisateur de déplacer une fenêtre de navigateur créée avec JavaScript, mais vous pouvez désactiver le redimensionnement et la barre de titre de la fenêtre pour limiter la possibilité de déplacement en spécifiant resizable=no, scrollbars=no, status=no, titlebar=no, toolbar=no dans les options de window.open().

Utilisez ces méthodes avec précaution et gardez à l'esprit les bonnes pratiques en matière de conception d'interface utilisateur pour offrir une expérience utilisateur agréable et sans surprises. 🌈👩‍💻👨‍💻

Version Française: Clique sur moi


JavaScript Windows 🖼️🌐🔩

In this guide, we will explore creating and managing windows with JavaScript. You will learn how to create windows with precise dimensions, position and move windows, and understand their limitations. Below is a table of contents to help you navigate easily through this guide.

Table of Contents 📚

  1. Create a window with precise dimensions 📏
  2. Set a minimum and maximum size for the window 📐
  3. Hide the window's toolbar 🛠️
  4. Position a window at a specific location 🔍
  5. Move an already opened window 🚚
  6. Limitations and best practices 🚧

Create a window with precise dimensions 📏

To create a window with precise dimensions, use the window.open() method and specify the width and height of the window in pixels.

let url = "https://thenolle.com";
let width = 800;
let height = 600;
let windowFeatures = `width=${width},height=${height}`;
let newWindow = window.open(url, "_blank", windowFeatures);

Set a minimum and maximum size for the window 📐

Use the resizeTo() method to set a minimum and maximum size for the window.

let minWidth = 400;
let minHeight = 300;
let maxWidth = 1200;
let maxHeight = 900;

newWindow.resizeTo(Math.max(minWidth, Math.min(maxWidth, width)), Math.max(minHeight, Math.min(maxHeight, height)));

Hide the window's toolbar 🛠️

To hide the window's toolbar, use the window.open() method and specify toolbar=no in the options.

let windowFeatures = `width=${width},height=${height},toolbar=no`;
let newWindow = window.open(url, "_blank", windowFeatures);

Position a window at a specific location 🔍

To position a window at a specific location on the user's screen, use the window.open() method and specify the X and Y position of the window.

let posX = 100;
let posY = 200;
let windowFeatures = `width=${width},height=${height},toolbar=no,left=${posX},top=${posY}`;
let newWindow = window.open(url, "_blank", windowFeatures);

Move an already opened window 🚚

To move an already opened window, use the moveTo() method.

let newX = 300;
let newY = 400;
newWindow.moveTo(newX, newY);

Limitations and best practices 🚧

It is important to note that some of these methods can be considered misuse as they can deceive users and prevent them from interacting with the browser as expected.

Here are some limitations and recommendations:

  • You cannot completely prevent a user from going into full-screen mode on a window created with JavaScript, but you can disable full-screen mode for the window's content by adding the allowfullscreen="false" attribute to the <iframe> or <video> element.
  • You cannot completely prevent a user from moving a browser window created with JavaScript, but you can disable resizing and the title bar of the window to limit the possibility of moving by specifying resizable=no, scrollbars=no, status=no, titlebar=no, toolbar=no in the options of window.open().

Use these methods with caution and keep in mind the best practices in user interface design to provide a pleasant and surprise-free user experience. 🌈👩‍💻👨‍💻

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment