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

Having difficulty initializing jQuery DataTables upon button click with an Ajax request

I have a piece of HTML code that represents a partial view: <table id="table_id" class="table table-inverse"> <thead class="thead-inverse"> <tr> <th>Select</th> ...

Checking for the presence of an element prior to moving forward in a selenium and python script

Is there a way to create a loop that runs as long as a specific div element appears on a website? I am trying to navigate through several pages and click the "next" button for each page. The last page does not have the div element I am looking for, so I w ...

Guide to updating the value of an input field with Selenium in Python

I have an input element that I need to clear its current value and input a new one. The HTML structure is as follows: <input class="input-mini" type="text" name="daterangepicker_start" value=""> To locate this element, I used the following code: ...

Yet another query about jQuery validation

I need help validating the promoRent field to ensure it contains a number. While this field is not required, if a value is entered, it must be greater than lotRent. Here's the current code snippet: jQuery.validator.addMethod("PRgreaterThanLotRent", ...

Sequence of HTML elements arranged in a stack

Recently, I came across a useful jQuery tutorial called "jQuery for Absolute Beginners: Day 8". In this tutorial, there is an interesting code snippet that caught my attention: $(function() { $('.wrap').hover(function() { $(this).childre ...

The alignment issue persists in HTML/CSS despite troubleshooting efforts

I am facing a challenge while attempting to center text within a modal window, despite my efforts the text remains uncentered. This is my HTML code: <div ng-init="modalCompassDir()"> <div class="myModal"> <img class='floor ...

Setting Up AdminLTE Using Bower

Recently, I decided to incorporate the Admin LTE Template into my Laravel project. I diligently followed the guidelines outlined here As soon as I entered the command: bower install admin-lte The installation process seemed to start, but then the ...

elevate the div with a floating effect

My goal is to create a chat feature for users that will only be visible for a limited time period. I was thinking of using PHP and timestamp to achieve this functionality, but I also want to make the chat visually disappear by having the message div float ...

How can I place a div using pixel positioning while still allowing it to occupy space as if it were absolutely positioned?

I successfully created an slds path element with multiple steps. I want to display additional subtext information below each current step, but there might not always be subtext for every step. To achieve this, I created an absolute positioned div containi ...

The jQuery function is not functioning correctly

Currently, I am in the process of setting up an accordion menu using jQuery and CSS3. Everything is working perfectly fine so far except that the menu always opens upon page load and the code to hide it is not functioning as intended. Here's the link ...

transmitting a JSON object to the JSONResult controller in ASP.NET MVC

When I send an ajax request to my MVC controller and pass an object, it shows as null in the controller. function add() { var viftech = { "id": $("#id").val(), "name": $("#name").val(), "lastname": $("#lastname").val(), ...

cordova and nodejs causing a communication problem

As a UI front end Developer, my expertise lies in user interface design rather than server side and port connections. I have successfully created a node server.js file that looks like this: var app = express(); var http = require('http').Server( ...

What is the best way to create a line break in a React caption without it being visible to the user?

Is there a way to add a break or invisible character between "we make it simple" and "to create software" in order to match the Figma design for the React web app caption? https://i.stack.imgur.com/Z4rpt.png https://i.stack.imgur.com/06mMO.jpg https://i.s ...

Error: The request does not have the 'Access-Control-Allow-Origin' header

As a beginner in post requests, I've been encountering an error when attempting to make a post request. Despite searching for solutions, the answers are too complex for me to grasp how to adjust my code to resolve it. var url = 'http://unturnedb ...

Is there a way to securely store my JWT Token within my application's state?

userAction.js -> Frontend, Action export const login = (userID, password) => async (dispatch) => { try { dispatch({ type: USER_LOGIN_REQUEST }); const url = "http://localhost:8080/authenticate/"; const ...

A guide to showing JSON data in reverse order on a Vue.js HTML page

Here is the order of my JSON data: {"data": {"pid": 50, , "location": {"lat": 10.0520222278408, "lon": 76.5247535705566, "state": "Kerala", "country": "India"}, "package": 0, "contact": {"email": "<a href="/cdn-cgi/l/email-protection" class="__cf_emai ...

What is the best approach for addressing null values within my sorting function?

I created a React table with sortable headers for ascending and descending values. It works by converting string values to numbers for sorting. However, my numeric(x) function encounters an issue when it comes across a null value in my dataset. This is th ...

What is the process by which node.js synchronously delivers a response to a REST web service?

Sorry if this question seems obvious! I'm struggling to grasp how node.js handles requests from the browser. I've looked at tutorials online where express.js was used to create the server-side code with node.js. Routes were then set up using prom ...

Tips for preventing a bootstrap modal from being dragged beyond its parent container

I need help figuring out how to prevent my bootstrap modal from being dragged outside of its parent container. Is there a way to constrain the modal within its parent? $(document).ready(function() { ShowValidationResult(); }); function ShowValidation ...

What is the best way to compare JavaScript arrays with the same items but in a different order?

Within my product groups, each group is identified by a set of product_type_ids. I am looking for a way to match different groups based on their product_type_ids. The order of the ids may vary within the group, but as long as the sets of ids match, I cons ...