Insufficient speed in location replacement

Hi there! Iā€™m currently working on creating a website that features a language selection menu. The idea is that when a user chooses a language, a cookie will be set to remember their choice. Upon revisiting the website, JavaScript will automatically redirect the user to the page with content in their chosen language. However, I am facing an issue where the language selection menu briefly appears for 1 second before the page transitions. I would like to have JavaScript perform a location.replace as soon as the page loads. Here's the code snippet: HTML

<!DOCTYPE html>

<html>

<head>
<script src="javascript\indexscript.js""></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" integrity="sha512-dTfge/zgoMYpP7QbHy4gWMEGsbsdZeCXz7irItjcC3sPUFtf0kuFbDz/ixG7ArTxmDjLXDmezHubeNikyKGVyQ==" crossorigin="anonymous">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap-theme.min.css" integrity="sha384-aUGj/X2zp5rLCbBxumKTCw2Z50WgIr1vs/PFN4praOTvYXWlVyh2UtNUU0KAUhAX" crossorigin="anonymous">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js" integrity="sha512-K1qjQ+NcF2TYO/eI3M6v8EiNYZfA95pQumfvcVrTHtwQVDG+aHRqLi/ETn2uB+1JqwYqVG3LIvdm9lj6imS/pQ==" crossorigin="anonymous"></script>
<link rel="stylesheet" type="text/css" href="style\indexstylesheet.css">
<script src="javascript\indexscript.js""></script>



</head>

<body onLoad="checkCookie()">
  <div id="overlay">
    <div id="background">
    </div>
    <div class="popup-info">
      <img class="flag" id="dutch" src="images\dutchflag.png" onclick="nl()">
      <img class="flag" id="english" src="images\englishflag.png" onclick="en()">
      <div id="textholder">
        <h1 class="lang" id="NLtext">Welke taal spreekt u?</h1>
        <h1 class="lang" id="ENtext">What language do you speak?</h1>
      </div>
    </div>
  </div>
</div>

</body>

</html>

CSS

#background {
    position: fixed;
    top: 0px;
    left: 0;
    right: 0;
    bottom: 0;
    background-color: rgba(0, 0, 0, 0.85098);
    z-index: 1;
    color: white;
    margin: 0 auto;

}
.popup-info {
    top: 100px;
    left: 0;
    right: 0;
    height: 450px;
    width: 800px;
    margin: 0 auto;
    background: white;
    position: fixed;
    z-index: 2;
    -webkit-border-radius: 25px;    
}

/* Rest of the CSS remains unchanged */

JAVASCRIPT

Updated JavaScript functions are included here...

Answer ā„–1

Avoid placing your code in the onload event of the body. Instead, insert it inline at the top of the page (within the head section) so that it is executed immediately after parsing. This ensures optimal speed when using JavaScript.

For even better efficiency, consider performing these checks on the server to enable immediate redirection. You can choose to redirect on the server side (keeping the same URL but displaying different content) or through client-side redirection for a new request with minimal processing overhead. Server-side execution provides more control and reliability compared to JavaScript redirection, which may not always execute as expected.

Additionally, keep in mind that the onload event may not always be the most suitable choice for all types of code. This event triggers after the entire document, including images, is loaded. Instead, consider utilizing the DOMContentLoaded event, which fires once the DOM structure is ready, potentially before image loading completes. This event is ideal for initializing elements and hooking up events.

However, in the specific scenario outlined, where immediate redirection is required without reliance on the DOM, skip using any event altogether and simply execute the code directly.

Answer ā„–2

As previously mentioned by others, server side redirection offers the quickest solution. If a JavaScript redirection is necessary, consider changing checkCookie to an immediately executing expression

(function checkCookie() {
    var language = getCookie('language');
    if (language === 'nederlands') {                                 
     location.replace("nlhome.html");   
    } else if (language === 'engels') {
      location.replace("enhome.html");
    }
}());

Move this and the getCookie function to a new JavaScript file and include it in the head section.

<head>
<script src="javascript\redirection.js"></script>

<!-- Latest compiled and minified CSS -->

This way, the checkCookie function will be executed as soon as the page loads.

Additionally, I suggest adding a loading spinner overlay to the HTML.

If the cookie is not found, remove the spinner overlay. This can also be included in the checkCookie function as an else statement.

By doing this, you can prevent any page flash and make the redirection process less intrusive.

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

Bootstrap is causing issues with unidentified div elements

I recently embarked on creating a slideshow using HTML, CSS, and jQuery. After completing the slideshow, I decided to add an interactive page beneath it. To streamline the layout process, I opted to utilize Bootstrap. However, upon loading Bootstrap, I en ...

Issue: Invalid operation - Angular Service

Trying to execute a function that is defined in a service has been causing some issues for me. var app = angular.module('title', ['flash', 'ngAnimate', 'ngRoute'], function ($interpolateProvider) { $in ...

Ensure all checkboxes for children are checked

To ensure syncing between parent and child checkboxes, here is how it works: When a user selects a parent checkbox, all corresponding child checkboxes should be selected as well. For instance, selecting the checkbox with id ='s9' will automatical ...

Error encountered while using jQuery Ajax - unable to properly handle the error

Currently, I am having issues with my ajax query when trying to retrieve the login status. The function seems to be malfunctioning. function getdetails(playlistname) { alert(playlistname); $.ajax({ type: "POST", url: "model/login_details1.php", ...

What is the best way to include a href link with parameters retrieved through an API in a React application

I am currently working on a frontend project using react. My goal is to include a link to the survey page after generating a map based on data retrieved from an API. However, I am facing two main challenges: Firstly, I am unsure about how to add paramet ...

Ajax undoes any modifications enacted by JavaScript

When using ajax, I trigger an OnTextChangedEvent. Before this event occurs, there is a Javascript function that validates the input field and displays text based on its validity. However, once the Ajax is executed, it resets any changes made by the Javascr ...

What are some ways to enhance the functionality of the initComplete feature in Dat

$('#example').dataTable( { "initComplete": function(settings, json) { alert( 'DataTables has finished its initialisation.' ); } } ); Is there a way to extend the initComplete function for other languages? $.extend( true, $.f ...

Transferring CSV information from a div container to an Excel spreadsheet

At this moment, when I have a <textarea> structured as follows: <textarea> 1, 2, 3, 4 5, 6, 7, 8 9, 10, 11, 12 </textarea> and then attempt to transfer the content to Excel, it prompts me to paste from a comma-separated source. However ...

Navigating through tabs on a mobile device by scrolling sideways

Is there a way to create a dropdown menu with tabs in a bootstrap site where the icons extend beyond the width of the menu, allowing mobile users to swipe horizontally? Check out the code here: https://jsfiddle.net/6yw6rp02/1/ This is the current code s ...

Unveiling the secrets of extracting element content using JavaScript (with JQuery) from an HTML object

I have written the code below to access the page "JQueryPage.aspx" and retrieve data from it using jQuery: <script type="text/javascript> $.get ( "JQueryPage.aspx", function(data) { alert("Data Loaded: " + data); ...

Chrome believes ISO time that lacks Z is in UTC; problem with C#

Check out this jsfiddle: http://jsfiddle.net/E9gq9/7/ on Chrome, Firefox, and Internet Explorer and see the differences: Chrome: Chrome http://images.devs-on.net/Image/vBTz86J0f4o8zlL3-Region.png Firefox: Firefox http://images.devs-on.net/Image/aNPxNPU ...

Issue with scroll bar not being repositioned correctly after hiding modal

I have a modal that opens when clicking on a button. However, once the modal is open, I am seeing two scroll bars: One for the modal itself Another for the html/body To try and remove the extra scrollbar, I used the following code: if ($(".dialog-popup ...

Is there a way to eliminate the initial and final double quotes within Angular 4?

Similar to JavaScript, TypeScript also uses either double quotes (") or single quotes (') to enclose string data. I have data coming from the backend that includes HTML content. Here is an example of my API response: <p>afjhjhfsd</p> Wh ...

Activate the enter key functionality with the help of knockout and a JavaScript function

My HTML textbox has an onkeypress event that triggers the sending of a message as shown below: <input type="text" data-bind="attr:{id: 'txtDim' + $data.userID, onkeypress: $root.sendMsg('#txtDim' + $data.userID, $data)}" /> I ...

Execute a block of code in PHP when a button is clicked

I'm looking for a way to trigger the execution of a piece of code or function upon clicking a button. I don't have an HTML form submitting the request, and I prefer to avoid reloading the page. For example: If I click a button called "Hello", I ...

Strange Reselect selector actions

It seems like my selector function is only triggered when one of the arguments changes, not both. Here's the selector I'm using to retrieve transactions from the state and apply two filters to them: export const getFilteredTransactionsSelector ...

The Infinite Loop: Unveiling the En

I'm interested in creating a unique shape resembling the symbol of infinity using CSS, SVG, or Canvas. If you're unfamiliar with what an infinity symbol looks like, here's an example: https://i.stack.imgur.com/cLhBh.jpg So far, I've a ...

inject the HTML content into the <div> container

Snippet: https://jsfiddle.net/x9ghz1ko/ (Includes notes.) In my HTML file, there are two distinct sections: <section class="desktop_only"> and <section class="mobile_only">. The challenge lies in positioning a JavaScript sc ...

Which function offers quicker rendering: fillRect() or the combination of moveTo(), lineTo(), and stroke()?

I am currently working with a legacy code where the timeline for a video is rendered using Canvas instead of DOM rendering, as it is believed to be faster. The original code used the fillRect() method, but was later changed to a combination of moveTo(), li ...

I want to create a feature on my website where users can generate a random password by clicking a button on the password field. This will

After setting up a form in HTML that connects to a function in views.py within the Django framework, I decided to enhance it by adding a button that automatically populates the password field with a randomly generated password. To achieve this, I utilized ...