I'm using Firefox 2.0.0.11 and Greasemonkey 0.8.x and the latest jQuery version (1.3.2) that works with Greasemonkey 0.8.
I try to load this Userscript:
// ==UserScript==
// @name TEST
// @include *
// @require jquery.min.js
// @grant GM_getValue
// ==/UserScript==
$(document).ready(function(){
alert('jQuery!');
});
but I receive the error:
Error: $ is not defined
Source File: file:///G:/Firefox/Data/profile/gm_scripts/huhu/huhu.user.js
Line: 8
I know that I have to update this old browser. But it's not possible. I have to work with that!
I want to load jQuery code on a local PC with no internet connection. I have copied jquery.min.js in the same folder where the userscript is.
G:/Firefox/Data/profile/gm_scripts/huhu/jquery.min.js
Any ideas where the problem is?
Regards, Bernte
There are two ways to install a Greasemonkey script, from a local drive, such that jQuery will work.
But, first, here are the...
Older versions of Greasemonkey (and Firefox) got confused easily. So, use Greasemonkey's Script Manager to uninstall any old versions of the script.
Go to the gm_scripts
folder, in your Profile Folder, and physically delete the folder for your script, if it's present.
Make sure your script source-code is in a file with a user.js
extension. EG: Hello World.user.js
Make sure your script source-code is in a folder that is not in a system TMP
or temp
folder. For example, place the source file in C:\My GM scripts\
.
Likewise, this is not, and do not use, any folder in the Firefox Profile Folder tree.
When in doubt, or if things appear "flaky", restart Firefox.
Merely point the @require
at Google's copy of the correct jQuery version. For GM 0.8, it should be no later than jQuery 1.3.2.
// ==UserScript==
// @name Hiya Ma
// @include http://stackoverflow.com/*
// @require http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js
// ==/UserScript==
$("body").prepend ('<div>Hello world!</div>');
Use the Firefox, Open File menu (CtrlO) to open the source file. (EG: C:\My GM scripts\Hello World.user.js
)
Or:
Drag-and-drop the source file to any open Firefox tab.
Greasemonkey will prompt to install the script. Follow the prompts.
Download the appropriate jQuery version (1.3.2 in this case), and save it as jquery.min.js
, in the same folder as the script source (C:\My GM scripts\
, in this example).
Do not use any path in the @require
directive:
// ==UserScript==
// @name Hiya Ma
// @include http://stackoverflow.com/*
// @require jquery.min.js
// ==/UserScript==
$("body").prepend ('<div>Hello world!</div>');
Install the script as in steps 2 and 3 of Method 1. You do not need to open or drag the jQuery file.
G:/Firefox/Data/profile/gm_scripts/huhu/jquery.min.js
look like a Firefox profile directory. You place no files here. Greasemonkey will copy and rename files as appropriate.
@grant
is not supported until Greasemonkey version 1.0. Do not use it here.
$(document).ready()
is not needed here, nor in most GM scripts. Greasemonkey fires at the correct time by default.
These methods were verified against Firefox 2.0.0.20 and Greasemonkey 0.8.20100408.6, but they pretty much apply to all versions of FF+GM until GM 1.0.
This error was not uncommon with older versions of Greasemonkey. The two most frequent causes were poor temp
-file housekeeping (by FF/GM) and Firefox profile corruption.
Do the following to remedy this in most cases:
Repeat the prerequisites, listed above.
Purge the system temp folder(s) of anything that resembles the script source file, or any @require
d files, or any @resource
d files.
In this example, look for hiya_ma.user.js
and jquerymin.js
, in particular.
In Windows, the temp folder(s) have the address(es): %TMP%
and %TEMP%
.
In the gm_scripts
folder, make sure that config.xml
has no references to deleted or missing scripts. If there are no (other) scripts, delete config.xml
.
If all else fails, uses Firefox's Profile Manager to create a new profile and reinstall your scripts there.