Only execute jQuery if the last visited page was the homepage

On the homepage, the CSS that is initially loaded on the JSFiddle is present. My goal is to have the inner pages transition to their new CSS class only when visited after the homepage.

<div class="container">
  <div>
    Thank you for your assistance!
  </div>
</div>

<style>
.container {
    padding: 100px 0;
    background: #ccc;
    text-align: center;
}

.container div {
    background: #eaeaea;
    width: 200px;
    height: 200px;
    margin:0 auto;
    text-indent: -9999px;
}

.container.inner,
.container.inner div {
    transition: all 0.75s ease-in;
}

.container.inner {
    padding: 10px 0;
}

.container.inner div {
    width: 100px;
    height: 100px;
}
</style>
<script>
jQuery( document ).ready(function() {
    setTimeout(function() {
        if ( jQuery(".home").length == 0  ) {
            jQuery('.container').addClass('inner');
        }
    }, 500 );
});
</script>

To achieve this, I believe using a cookie or two would be necessary to determine if the previous page was the homepage and to apply the CSS without delay or animation if it was an inner page.

Answer №1

document.location is an essential element to consider:

if(document.location.href.indexOf('home')){
   // add the specified class in this section.
}

Keep in mind:
document.location stores the URL of the current page.

Answer №2

In my solution, I opted to utilize PHP in order to identify the document referrer. Big thanks to @Jai for steering me towards the right path. To execute the jQuery responsible for CSS modifications, I stored it in a separate file and trigger that JavaScript file whenever the referrer is "index.php" or "/".

Below is the specific code snippet I integrated into WordPress functions.php:

$homePagePaths = array (
  '/index.php',
  '/'
);

$parts = parse_url($_SERVER['HTTP_REFERER']);
if (empty($parts['path']) || in_array($parts['path'], $homePagePaths)) 
  wp_enqueue_script( 'inner-animation', 
  get_stylesheet_directory_uri() . '/js/inner-animation.js', 
  array('jquery'), '', true )
;

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

Managing numerous JavaScript objects within a plugin

I have developed a straightforward javascript plugin that enables me to gather data from specific html elements. A page can contain x number of elements (usually up to 20), each with its own settings. However, the issue I am facing is that the returned obj ...

What are the best ways to stop jQuery events from propagating to ancestor elements?

I have a collection of nested UL's that follow this structure: <ul class="categorySelect" id=""> <li class="selected">Root<span class='catID'>1</span> <ul class="" id=""> <li>First Cat<span ...

Switch up the Position of a Frame with jQuery

I'm attempting to cycle through an array of URLs in order to designate them as locations for a frame. The current setup I have can be found on this fiddle: var l = ['0', '1', '2', '3', '4', '5&ap ...

Talwind Flexbox Grid Design

Currently, I am exploring the world of website layout creation using Tailwind Flexbox Grids as I believe it can add significant value. However, I have encountered some queries: How does one go about constructing a layout like this? Referencing this speci ...

HTML5 for Bulk Video Upload

Is it possible to use the html5 api to record and upload video content directly from the browser? I am facing an issue in a project where the video recordings can be very long or large, causing extended upload times for users. Ideally, I would like the vi ...

What is the best way to validate adjusted tags within the html editor of Visual Studio 2012?

The html editor in Visual Studio 2012 keeps giving me warnings for tag names that are not recognized in the detected or configured html schema. Since I am using AngularJs by Google, this is quite inconvenient as it relies on the ability to enter custom ta ...

Activating scrolling through Javascript

A friend of mine created a web page to help me learn some JavaScript. He designed a feature where the content in each div becomes visible as soon as the user scrolls past it. However, I noticed that the content appears too late for my liking. I would like ...

Switching my Selenium code to HtmlUnit: A Step-by-Step Guide

Currently, my Selenium code is working perfectly fine. However, I am looking to convert this code into HtmlUnit. I know I can use the HtmlUnitDriver like WebDriver driver = new HtmlUnitDriver(); I want to make it purely HtmlUnit. Below is the code that I ...

Issue with JQuery autocomplete

I am encountering an issue with the JQuery Autocomplete plugin where it is not providing autocomplete suggestions when I enter text. Any thoughts on why this might not be working for my code? The basic example works fine, but mine does not seem to functio ...

The success of your order hinges on jQuery being defined when using browserify

I encountered an issue while attempting to utilize a plugin located in /js/lib/stellar.jquery.js: var $ = require('jquery'); require('./lib/stellar.jquery') $(function(){ $.stellar(); }); Upon running the code, I received an err ...

Draggable element with a centered margin of 0 auto

Encountering an issue with jQuery UI sortable/draggable where the element being dragged, when centered using margin:0 auto, starts dragging from the left side of the container instead of the center where it is positioned. Check out this demo to see the pr ...

Adjust the text according to the selected checkbox option

I am attempting to update the displayed text based on whether a checkbox is currently checked or not. The value of the viewable text should reflect the user's selection. I believe I am close, but the functionality is not working as expected. <html ...

Modify the Ng-Pattern with Jquery

Is there a way for someone to assist me in updating the Ng-Pattern with Jquery? I have looked at the questions below, but they haven't been helpful (Most of them focus on updating to dynamic values using Angular JS). Question 1 Question 2 Question ...

What is the best way to utilize the html element id with C# programming language?

Currently, I am attempting to utilize C# and SQL in order to track the number of times a link is clicked on my website. I have already created an HTML link with the following format: <a href="home.aspx" id="topNav-home" runat="server" onserverclick="cl ...

I am facing difficulty importing emotion js style using dynamic variables

I recently designed a webpage that has the following appearance: https://i.stack.imgur.com/AnIXl.jpg Here is the code from my _app.tsx file: import '../styles/globals.css' import type { AppProps } from 'next/app' import { createTheme ...

Prevent using href for opening the browser when clicked

In the control, there is an href link that triggers a javascript function and passes a variable to it: <a href="<%#XPath("link").ToString()%>" onclick="return getLink(this)">Link</a> I'm looking for a way to prevent the browser fro ...

Changing datetime-local format from European to American dates

<input type="datetime-local"> While this input retrieves the local time from your PC, I'm curious if there's a way to make it display as US time specifically. ...

jQuery - Patience is a virtue, wait until the SlideUp() animation is

Is there a way to pause the script until the jQuery function slideUp() is completed? <script type="text/javascript"> $(document).ready(function() { $("div[class=item]").click(function() { var id = $(this).attr("id"); $("#content" ...

Having issues with reading files in PHP using AJAX? Explore alternative methods for downloading files seamlessly

I need to store a value in a txt file and allow the user to download it. Currently, the value is successfully written to the txt file, but the readfile function does not execute, preventing the download from starting. The PHP code is on the same page as ...

Navigating events within the Material Design Light (MDL) mdl-menu component can be

I am utilizing Material Design Light with just PHP, jQuery, and MDL min CSS and JS, no other frameworks involved. Keeping it simple with a basic menu. <button id="lang-switcher" class="mdl-button mdl-js-button mdl-button--fab mdl-js-ripple-effect"> ...