Customized settings saved in local storage using JavaScript

Here is the script I am currently using for my app:

<script>
if (localStorage.getItem("0") === null) {
//do nothing 
}
else if(localStorage.getItem("1")===null{
}
else if(localStorage.getItem("2")===null{
}
else if(localStorage.getItem("3")===null{
}
else if(localStorage.getItem("0") !==null){
window.location = "cosmote.html"
}
else if(localStorage.getItem("1") !==null){
window.location = "germanos.html"
}
else if(localStorage.getItem("2") !==null){
window.location = "zapp.html"
}
else if(localStorage.getItem("3") !==null){
window.location = "sunlight.html"
}
</script>

Answer №1

Have you thought about optimizing the storage of the last page value?

You could consider storing it in one local storage area and then retrieving it with a single call like

localStorage.getItem("lastPage");

By avoiding multiple accesses to local storage through a switch or if else statement, you can make your code more efficient and readable.

If using an object and a value for each page isn't suitable, this alternative approach may be worth exploring for better performance.

Answer №2

It seems like you may have included a couple of additional parentheses by mistake. This could potentially be causing the issue.

If you're looking for a straightforward Javascript library to work with local storage, you might want to consider exploring .

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

Avoiding scrolling when a button (enveloped in <Link> from react-scroll) is pressed in React

Currently, I am implementing the smooth scroll effect on a component using react-scroll. However, adding a button inside the component to create a favorite list has caused an issue where the smooth scroll effect is triggered upon clicking the button. Is ...

Selectize Fails to Populate Options Using Ajax

ExpressJS is the platform I am using to develop a management dashboard for a community project. Right now, my main concern is integrating a modal that allows users to add new games to a database. Although I am able to fetch data remotely, I am facing diffi ...

Repeated firing of jQuery's Ajaxstop within a click event

When using the quantity plus button on the woocommerce cart page and reaching maximum stock, I want to display a notice. However, due to an auto update on the cart, I have to wait for the ajax load to complete before showing the notice. My script revealed ...

How is it possible to encounter a Javascript unexpected token ] error within an HTML code?

While working on my project, I encountered a JavaScript error in the console of Chrome. The error message stated "Unexpected token ]" and it was pointing to a specific line of raw HTML code. I am puzzled about what could be causing this issue. Unfortunatel ...

What is the best way to parse JSON data received from an Android device using PHP?

I am having an issue with my android emulator where it is sending a request to a PHP script to access MySQL data. I have checked the PHP script by manually giving values to the SQL query and I can retrieve the data in my Android emulator. However, I am fac ...

Receiving null value with Web API POST using [FromBody]

Below is the code for my WebAPI in C#: [Route("")] [HttpPost] public void SaveTestRun([FromBody] object data) { inputResultsToDatabase(data); } This is the ajax request I am making: sendTestData() { t ...

Add a sound file to the server and configure the images based on the server's response

I am a newcomer to JavaScript and AJAX, and I am facing an issue while trying to upload an audio file to the server and display the image from the server response. When attempting to pass the audio file from the input tag to an AJAX call, I encounter an il ...

Executing PHP to capture and showcase the HTTP request received from one page onto another

As a beginner in web development, I humbly ask for patience as I navigate through unfamiliar territory. If possible, please guide me through the logical steps necessary to accomplish the task at hand. I am currently working with PHP and need assistance i ...

Confirmation for deletion in HTML form

Is there a way I can include a confirmation message for deleting an item on my form button? Any suggestions on which JavaScript code to use? <script type="text/javascript"> function confirmDelete() { return confirm("Are you sure you want to delete ...

Tips for uploading an input file using ajax method instead of a form

I have a dynamic table that sends all input text to my database using ajax. Here is the code I have implemented so far: <script language="javascript" type="text/javascript"> var i = 2; // This variable always points to the last row function test( ...

Create the correct structure for an AWS S3 bucket

My list consists of paths or locations that mirror the contents found in an AWS S3 bucket: const keysS3 = [ 'platform-tests/', 'platform-tests/datasets/', 'platform-tests/datasets/ra ...

Using JavaScript to assign the title property to an <a> tag

I'm currently working on modifying a code snippet that utilizes jQuery to display the "title" attribute in an HTML element using JavaScript: <a id="photo" href="{%=file.url%}" title="{%=file.name%}" download="{%=file.name%}" data-gallery><i ...

Switching between all tabs simultaneously in an MDX page (React + Gatsby + MDX) using @reach/tabs

Creating a Tab component for a Gatsby site has proved to be a bit tricky. Imagine having a page with multiple tabs all labeled the same: Heading 1 First tab block Tab 1 | Tab 2 Content Tab 1 Second tab block Tab 1 | Tab 2 Content Tab 1 for the second bl ...

Set the value of one email input to another email input in AngularJS

I'm having trouble trying to link an email input field to another in angularjs without success. Here is what I have attempted so far: <div class="row-fluid"> <div class="control-group span12"> <label for="email">Email</labe ...

Text centered on hover for figure caption

Check out this fiddle http://jsfiddle.net/sLdhsbou/ where I am trying to center the "x" that appears on hover perfectly no matter what size the image is. Also, why does the transition not occur when moving the mouse outside of the figure, unlike when movi ...

Tips for determining if a user is refreshing the page or navigating to a different page

Scenario: I need to store a specific variable in the local storage every time a user exits the page. By utilizing the window.onbeforeunload method, I am able to capture the event when a user is leaving the page. However, I want to assign different values ...

What is the process for including a file in a request?

I've been encountering an issue while trying to upload a CSV file and send a request to an API. Despite attempting to do so via XHR, Unirest, and Axios, none of these methods seem to be working properly. Could there be something amiss with the impleme ...

Utilizing a JavaScript class within the document ready function

Having trouble with a countdown script wrapped as an object in a separate file. Struggling to setup multiple counters or objects due to the timeout function in the countdown class not finding the object that was set up within the document ready event. Wh ...

ionChange - only detect changes made in the view and update the model in Ionic 2

In my Ionic 2 application, I have a feature that allows users to schedule notifications as reminders. The requirements for this feature are as follows: Upon entering the reminder page, it should check if there is a saved reminder. If a reminder is saved ...

Adjusting the value of 'this' within a service using a function

I am a newcomer to Angular and currently delving deeper into its intricacies. Despite my efforts in researching, I have not come across a solution for the issue at hand. My service sets the initial value of this.totalCount = 0; Within my controller, upo ...