Interested in implementing a monthly JavaScript redirect strategy?

I am in the process of creating an index.html page that will automatically redirect to a specific page based on the month. This is specifically for my investment portfolio, where I track announcements like Ex-Dividend dates for companies I invest in. For example, if I want to see which companies have upcoming Ex-Dividend dates in April, I simply visit the index.html page and it will take me to the designated April page.

To get the code for the random redirect feature, I visited this website: ()

Below is the code provided by them:

<script type="text/javascript">

var urls = new Array();
urls[0] = "http://learninginhand.com";
urls[1] = "http://learninginhand.com/about";
urls[2] = "http://learninginhand.com/contact";
urls[4] = "http://learninginhand.com/services";
urls[5] = "http://learninginhand.com/videos";
urls[6] = "http://learninginhand.com/infographics";
urls[7] = "http://learninginhand.com/resources";
urls[8] = "http://learninginhand.com/stickaround";
urls[9] = "http://learninginhand.com/posts";
urls[10] = "http://learninginhand.com/pet";

var random = Math.floor(Math.random()*urls.length);

window.location = urls[random];

</script>

Answer №1

To extract the current month using JavaScript, I referred to this helpful page: (https://www.w3schools.com/jsref/jsref_getmonth.asp).

Below is the code snippet provided on that website:

var d = new Date();
var n = d.getMonth();

Since JavaScript counts months starting from 0, I added a small modification to increment it by one:

n = n + 1;

I also made adjustments to the array structure as follows:

var urls = new Array();
urls[1] = "01.html";
urls[2] = "02.html";
urls[3] = "03.html";
urls[4] = "04.html";
urls[5] = "05.html";
urls[6] = "06.html";
urls[7] = "07.html";
urls[8] = "08.html";
urls[9] = "09.html";
urls[10] = "10.html";
urls[11] = "11.html";
urls[12] = "12.html";

In comparing with the code found at (), the redirection part has been modified in the following way:

var random = Math.floor(Math.random()*urls.length);

window.location = urls[random];

</script>

The alteration made was simplifying it to:

var random = n

window.location = urls[random];

</script>

The initial randomization logic was removed and replaced solely with n for direct referencing.

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

Optimal methods for sending Redux state to components

Currently, I am collaborating on a React + Redux project alongside other developers, and we are at an impasse regarding the best practice for passing state and actions throughout our components. My strategy involves creating a 'container' or &ap ...

Iterate through the classes for updates rather than focusing on individual id fields

I am currently working on creating a function that can refresh data with an AJAX call for multiple instances of a class within a single page. The function functions perfectly when there is only one instance on the page: $(document).ready(function() { ...

The functionality of $watch is not functioning correctly within the Modal, despite implementing it with $scope.user={pwd1:''}

For the Plunker link referenced, please click here: http://plnkr.co/edit/0vOjw0?p=preview index.html <!DOCTYPE html> <html ng-app="app"> <head> <link rel="stylesheet" href="style.css"> ...

Master the art of line animations with D3.JS

Recently delving into D3 and I have a query regarding the animation of lines. I have been able to animate circles with the following code snippet: var data = d3.range(100).map(function() { return new agent( 0, 0, (Math.random() ...

The window.open() function is malfunctioning on Google Chrome

Within my vue.js application, I have encountered an issue when attempting to utilize window.open() to open a new tab. Strangely, whenever I use this method, the new tab opens briefly before immediately closing without loading any content. Surprisingly, win ...

The app has crashed, currently waiting for any file changes before starting again

Can someone assist me? I am encountering an issue while trying to run npm start. The error pertains to the server.js file. [nodemon] restarting due to changes... [nodemon] starting `node server.js` /Users/johnngo/Desktop/LambdaSchool/HTTP-AJAX/server.js ...

Implement feature to enable selection using jQuery

I have a question about implementing an <option value="fiat">Fiat</option>"> element into a dropdown list using jQuery. I've tried the code below and encountered some issues. When I click the button, I want to add the <option value= ...

The function Page.render() will output a value of false

Currently, I am utilizing phantomjs to capture screenshots of multiple webpages. The code snippet below is what I have used to generate a screenshot image. var page = require('webpage').create(); page.viewportSize = { width: 1200,height: 800}; ...

Does repeatedly accessing count($array) in a foreach loop result in a decrease in performance?

While browsing through SO, I came across the query "Identifying the final element of an array when utilizing a foreach loop in PHP". In the discussions, user "johndodo" argued that there is no reduction in performance for repeatedly accessing count($array ...

Choose and target any element on the page using jQuery

Can someone help with selecting any element on a webpage, such as clicking on the body, a div, or a specific ID, so that I can save it to a variable for later editing? I've attempted: $("*", document.body).click(function (e) { e.stopPropagation() ...

I am consistently running into an Uncaught Syntax error within my Express server.js while using Angular 1.5.6

I've been struggling for hours to integrate Angular with routes that I've created. Eventually, I decided to give Express a try and set up a basic server.js file to run as a standalone server. However, nothing seems to be working and I keep encou ...

Calculating the size of an array by using the sizeof operator along with an offset

I was pondering about sizeof(arrayName + offset). It returns the sizeof(pointer). Even though the array name is essentially a constant pointer in C, sizeof(arrayName) provides the size in bytes of an array. Therefore, it seems like the compiler interprets ...

Having trouble with Vue 3 Composition API's Provide/Inject feature in Single File Components?

I am currently developing a VueJS 3 library using the Composition API. I have implemented Provide/Inject as outlined in the documentation. However, I am encountering an issue where the property in the child component remains undefined, leading to the follo ...

Using jQuery to toggle visibility based on data equivalence

I created a code snippet in which I am implementing jQuery show/hide functionality when the data attribute matches. Here is the HTML structure: <div class="container"> <div class="item" data-item="1">1 <div class="inside" data-c ...

Is there a simple way to display all the data from a JSON object even if the contents are unknown beforehand?

Greetings! I am Pearson's dictionary api. Here is a glimpse of what I receive from an api call: { "status": 200, "offset": 0, "limit": 10, "count": 10, "total": 135, "url": "/v2/dictionaries/entries?headword=dog", "results": [ { ...

Steps for changing a component's properties when the component serves as a property

The scenario I'm facing involves a component that receives a prop named button. This button prop is essentially a Component itself, containing various other props within it. My goal is to override the specific prop called type within this nested struc ...

Formatting in Cx framework: Configuring right alignment and decimal precision on NumberField

Currently, I am involved in a project that involves UI development using Cx, a new FrontEnd framework based on React. You can find more information about Cx at One of the tasks in my project involves creating a Grid with filter fields located at the top ...

The custom div slider speed is too quick, specifically when transitioning back from a different browser tab

While observing the code in action, everything seems to work fine but when I switch to another browser tab and come back, the speed of the animation is too fast. Below is the jQuery code snippet: var divId = 1; var lp = 0; $(document).ready(function (){ ...

What is the best way to create a time delay between two consecutive desktop screenshot captures?

screenshot-desktop is a unique npm API that captures desktop screenshots and saves them upon request. However, I encounter the need to call the function three times with a 5-second delay between each call. Since this API works on promises, the calls are e ...

Is there a way to identify when an image extends beyond the boundaries of the browser window and subsequently reposition it?

Currently, I am implementing a method found at this link to display popup images of my thumbnails. However, there is an issue when the thumbnail is positioned close to the edge of the screen and the original image is too large, causing it to be cut off by ...