Conceal a div element after redirecting to a different HTML page

I have a dilemma with my two HTML pages - index.html and register.html. I am trying to navigate from index.html to register.html, but I want to skip the select region step and go straight to the login page. Here's the code snippet I've been attempting:

$("#lgnToProfile").on("click", function(){
    window.location.href = "register.html";
    $("#slctRgn").hide();
    $("#lgnPage").show();
});

Could someone please review this and provide guidance on how to fix it? What am I doing wrong?

I checked out some solutions suggested on Stack Overflow, specifically regarding showing a DIV or section after redirecting to an HTML page, but unfortunately, none of them have worked for me.

Answer №1

To avoid altering your URL path, you can utilize "localStorage":

var currentPath = window.location.pathname;
var savedPath   = localStorage.getItem('previousPage');

// check if a page has been saved
if (typeof savedPath === 'string') {

    // condition for when the current page is "register.html" and the saved page is "index.html"
    if (currentPath.match('/register.html') && savedPath.match('/index.html')) {
        $("#slctRgn").hide();
        $("#lgnPage").show();
    }
}

// save current page
localStorage.setItem('previousPage', currentPath);

https://developer.mozilla.org/fr/docs/Web/API/Window/localStorage

Answer №2

After transitioning to a new webpage, any Javascript that was executed on the previous page becomes ineffective.

To address this, you must somehow communicate to register.html that it should hide the select region div and then proceed to conceal the div using a script within register.html.

For instance, in index.html, you can append a query string parameter to notify register.html.

$("#lgnToProfile").on("click", function() {
    window.location.href = "register.html?hideSlctRgn=1";
}

Subsequently, in register.html, upon document readiness, verify if the parameter is present and hide the div accordingly.

$(function() {
    if ( window.location.search.indexOf('hideSlctRgn=1') != -1 ) {
        $("#slctRgn").hide();
        $("#lgnPage").show();
    }
})

There exist alternative (and preferable) methods for detecting the presence of a query string parameter, but the above serves as a straightforward approach. Instead of a query string parameter, you could also utilize a cookie or explore the option of utilizing local storage.

Answer №3

In order to proceed to the next page, it is important to examine the referer page. Since JavaScript lacks access to the referer page, one can achieve this by utilizing an anchor or hash to verify the referer and display/ conceal the necessary elements.

Simply add a link on index.html without requiring a click event in jQuery, directing to the registration page with an appended hash name: register.html#referer. The term referer can be customized as needed.

Upon reaching register.html, one must assess the value of the hash:

$(document).ready(function() {

    // retrieve the hash value
    var hash = window.location.hash;

    // check if the hash exists and corresponds to “referer” (the chosen hash value)
    if(hash == "#referer") {

        // implement your code to display or hide elements

    }

});

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

Show a toast message and reset the form upon submission

Here I have created a contact form using HTML, CSS, and JavaScript. However, I'm facing an issue where the form redirects me to a page displaying the submitted details after clicking the submit button. I want to display a toast message instead and res ...

Why is it that when drawing rectangles with HTML 5 canvas using pixel size, a black rectangle appears instead of blue?

My intention was to create a large blue rectangle measuring 320 X 240 pixels on the Canvas in Firefox, but I'm only getting a black rectangle instead. This issue is perplexing as I am simply experimenting with pixel drawing and for some reason, it&apo ...

Modify CSS to switch body background image when a DIV is hovered

Feeling a bit lost on where to start with this issue. My divs are positioned as desired on the page. Just to clarify, I am utilizing CSS Grids for this. In short, all I'm wondering is if it's possible to change the background of the body elemen ...

What is the best way to keep the heights of two divs in sync?

Is there a way to make two divs have the same height, even though their content determines their individual heights? I am looking for a solution that doesn't involve using a table or a parent div. I'm new to JavaScript, so I'm hoping there i ...

Transform the appearance of several elements using querySelectorAll

When targeting class names in an HTML page with a checkbox using querySelectorAll, keep in mind that getElementByID only works with the first element. QuerySelectorAll displays a nodeList which might require some corrections - how can this be achieved? le ...

Leveraging JavaScript to create a horizontal divider

Just a quick question - how can I create a horizontal line in Javascript that has the same customization options as the HTML <hr> tag? I need to be able to specify the color and thickness of the line. I am working on a website where I have to includ ...

Developing a notification system using a combination of ajax, jquery, and Iframe

I am in the process of setting up a messaging system on my website. Currently, I have a table with three columns - two integer fields (from and to) and a timestamp for the date of sending. On one section of the page, I want to display a list of messages ...

Issues with window.location.href on iOS devices are causing functionality problems

Problem: The ajax function is working correctly on Windows and Android devices, but it fails to work on iOS devices. It shows an alert message on Windows and Android, but nothing happens on iOS. I have tried using window.location.href, location.href, and l ...

Having trouble setting the background of a div in CSS

How can I apply the background-color: #f8f8f8 class to the step class without interfering with the display of the horizontal lines after 1, 2? What could be causing this issue? .main-progress { text-align: center; position: relative; margin- ...

Image not located: 404 error in JavaScript

I am currently working with JavaScript and have encountered an issue. I have an array of objects that contain various data such as id, title, price, and image. I need to retrieve this data from the array in order to display it. While I am able to successfu ...

Utilizing Promises in the apply function

I am currently working on a project in Node.js that utilizes bluebird for promise handling, as well as ES6 native promises. In both projects, I have a chain where I make a database query structured like this: some_function(/*...*/) .then(function () ...

Interacting with JQuery UI Button causes it to shift position on mouseover

In my table, there are two large cells. The left cell contains two drop-downs and a JQuery UI button that is causing trouble, while the right cell displays a list generated from an AJAX database query. As the list grows longer, the button gradually moves u ...

How can I apply styling to Angular 2 component selector tags?

As I explore various Angular 2 frameworks, particularly Angular Material 2 and Ionic 2, I've noticed a difference in their component stylings. Some components have CSS directly applied to the tags, while others use classes for styling. For instance, w ...

When using the Google Maps API fetch() method, the response is empty. However, when accessing the same

I am currently working on extracting the "photo_reference" from the JSON data provided below; { "html_attributions" : [], "results" : [ { "formatted_address" : "Bandar Seri Begawan, Brunei", "geometry" : { "locati ...

Convert a tuple into a JSON string

Looking to serialize a tuple into JSON: List<Tuple<string, string, string, string, string, string>> iCalEvents = new List<Tuple<string, string, string, string, string, string>>(); When I use the following code to output the value: ...

The issue of data not being transmitted when an ajax jquery form is modified

Each item in the cart/order has its own unique form with IDs created in a loop (e.g. 'id'=>'cart_'.$line )(cart_1,cart_2), along with an update link for each form within the loop. See the code snippet below: echo form_open($control ...

Injecting Vibrant Lines into Browser Using three.js

My current project involves drawing colored lines in a browser without using meshes. I am retrieving data from a MySQL database where geometry and other attributes are stored, and then converting this data into text blocks that create individual line objec ...

Need help implementing the disableGutters property on MTableToolbar?

I am currently using react material-table and I would like to customize the default toolbar styles by passing the prop disableGutters={true}, similar to how it's done in material-ui toolbar. Below is the code snippet that I have tried: <MaterialTab ...

How to ensure NodeJS waits for a response before returning a value

I've come across a seemingly simple problem that I just can't seem to solve. My current project involves working with an asynchronous messaging bot. In this particular scenario, the bot is supposed to react to an event by calling a Restful API a ...

Converting a database query result into a JavaScript variable: A step-by-step guide

I've been struggling with this problem for a day now and I feel like giving up. My main goal is to export the query result as a string (specifically dataString) so that I can easily import it as a string in my external .js file. module.exports.getKl ...