Why does the script run fine within the HTML but fails to work externally?

I recently created a script for my navigation bar that turns it into a sticky navigation once it reaches the top of the page.

Everything works perfectly when I include the script directly in my index file using <script> tags. However, when I try to place it in an external JavaScript file, it doesn't seem to work at all.

Check out the complete code on JSFiddle

Here's the script:


var windw = this;
$.fn.followTo = function ( pos ) {
    var $this = this,
        $window = $(windw);
    $window.scroll(function(e){
        if ($window.scrollTop() > pos) {
            $this.css({
                position: 'fixed',
                top: "20px"
            });
        } else {
            $this.css({
                position: 'absolute',
                bottom: '0',
                left: '0', right:'0',
                top: 'inherit'
            });
        }
    });
};
$('#mainNav').followTo( $(window).height() - ( $('#mainNav').innerHeight() + $('.globalHeader').innerHeight() ));

Answer №1

Remember to include the jQuery library before any external scripts in your code. If you're using a fiddle, simply click on the javascript text located in the top right corner of the javascript code tab and select a jquery version from the frameworks dropdown menu

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

Achieving a transparent background in WebGLRender: A guide

I've been experimenting with placing objects in front of CSS3DObjects using the THREE.NoBlending hack. However, in the latest revisions (tested on r65 and r66), I only see the black plane without the CSS3DObject. Here is a small example I created: in ...

JavaScript: How to identify and select a specific item from a context menu

Currently diving into the world of JavaScript, so please keep that in mind when explaining. My main goal is to figure out a way to detect when a user triggers a right-click or uses the context menu from the keyboard. Specifically, I want to know if they s ...

The issue of overlapping divs causing links to be unresponsive

I am currently working on developing a Shopify theme that utilizes the 960 grid system. Here is the layout I have implemented: <div id="header" class="container_16"> <!-- positioned relatively --> <div id="tl_overlay"></div> ...

Is there a way to verify the presence of months in a column related to departments?

Is there a way to validate whether the current row aligns with the current column month and year? If not, can it be set to 0. Let's consider the current dataset. Presenting my resultData https://pastebin.com/GHY2azzF I want to verify if this data ...

Navigating through screens in a React PWA using a stack approach

As I work on developing a PWA application intended for download onto users' phones, I am faced with the challenge of creating multi-step screens. For example, when creating an activity that involves multiple steps such as adding a name and then adding ...

Using an ASP.NET web API to return a date and populate a jQuery datepicker with the response

Having an issue with what should be a simple task. My web API returns dates in the following format: 2014-01-06T09:46:12.7819007+01:00 It appears to be a common ISO format, but when I use the jQuery datepicker, it interprets the date incorrectly as 7323- ...

Send an image using ajax without the need for a form

Is it possible to perform a file upload inside a form without nesting another form in HTML5? I need to allow the user to select an image from a dropdown, but also provide the option to dynamically add and choose a new image while filling out the form. The ...

Node server receiving duplicate emit events from React socket io client

I have been working on a react application with node as the backend, and they are communicating through socket-io. The issue I am facing is that when React uses emit to send data to node, it sends the data twice, resulting in duplicate submissions. For ins ...

Filtering JSON data in AngularJS is simple and effective

I am working with JSON data and I want to display it filtered by genre. The solution provided in the previous question How to filter JSON-Data with AngularJs? did not work for me. Here is myapp.js: var myApp = angular.module('myApp', []); myAp ...

The spacing of the numbers in Highcharts is too cramped

Is there a solution to make close values like 16 and 16.5 readable in Highcharts? I couldn't find an answer in the documentation. http://jsfiddle.net/01Lquzrm/ $(function () { $('#container').highcharts({ chart: { t ...

Enhance your forms with jQuery by adding custom error messages

Following a jQuery post of my form data, I receive a JSON object in return. In cases where the form is deemed invalid, the status is flagged as "error" and error messages are attached directly to the specific fields. This is the response retrieved from th ...

How to redirect in Next.js without including the locale in the URL

When I use the following redirection: { source: "/restauracja", destination: "/restaurant", permanent: true, }, it redirects me to /pl/restaurant Is there a way to fix this redirect issue and eliminate th ...

Using img-fluid in CSS to dynamically resize database-supplied images, providing a consistent and optimized viewing

Currently, I am working on a bootstrap site that operates as a CMS. This allows users to select a layout and drag images into DIV containers (bootstrap columns) for the purpose of saving and displaying them on digital displays. Although most functionaliti ...

retrieving information from a Windows Presentation Foundation (WPF) service using

Could someone provide an example of how to fetch data from the following URL which provides JSON data? It works perfectly when tested with the Firefox REST client. I am unsure of how to consume this web service using PHP or jQuery. url: "http://ws.g ...

Having trouble sending data from the controller to the view in Laravel 8

I am struggling to display data retrieved from the database in my view through the controller. Despite trying various solutions found on similar questions, none of them seem to be effective. My main goal is to showcase a list of employees on my admin page. ...

Deactivate the button when an item is selected from the dropdown menu in JavaScript

I am working on a dropdown menu containing values that link to functions, along with a textbox and submit button. My goal is to have the submit button disabled or hidden when a specific value is selected from the dropdown, possibly alongside clearing the c ...

Using the onclick event in JavaScript to create transition effects for swapping images

I am working on a website where I display 3 images. Instead of redirecting the users to a new page, I want to keep them on the same page. Question: How can I implement a functionality where clicking a <button> will display 3 new images in the view w ...

Ways to heighten the `RadComboBox` `DropDownHeight` featuring `Templates`

One of the challenges I'm facing involves a RadComboBox setup like this: <telerik:RadComboBox ID="RadComboBoxNames" runat="server" Width="470px" DropDownAutoWidth="Enabled" MaxHeight="363px" Skin="MySkin" EmptyMessage="Select" High ...

The property 'innerHTML' cannot be assigned a value because the object is null or undefined

Whenever I attempt to show the output of these two functions on my webpage, I keep encountering an error message. Error: Unable to set value of the property 'innerHTML': object is null or undefined The scripts themselves are functional, although ...

Surprising outcomes when using Mongoose

Questioning Unusual Behavior There is a model in question: //test.js var mongoose = require('../utils/mongoose'); var schema1 = new mongoose.Schema({ name: String }) var schema2 = new mongoose.Schema({ objectsArray: [schema1] }); schema1.pre( ...