1

I have various JS libraries in my web application, which are loaded before my main JS file (main.js). One of these libraries is jshashtable, but when I try to create a new Hashtable object in main.js, Google Chrome and Firefox throw a ReferenceError, complaining that the variable does not exist.

Here is the <head> of the application:

<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
 <script type="text/javascript" src="/static/jquery-1.4.4.min.js"></script>
 <script type="text/javacsript" src="/static/jshashtable-2.1.js"></script>
 <script type="text/javascript" src="/static/main.js"></script>

Here is the problem line in main.js:

posts = new Hashtable();

This line is inside a function called init which is called when the page has finished loading (using the jquery $(document).ready() function).

Any reason why Hashtable is not global? Google maps and jquery objects work with no such problem. The source of jshashtable can be seen on Google code.

1 Answer 1

2

Updated answer: The problem is that you've got a typo in the script tag:

<script type="text/javacsript" src="/static/jshashtable-2.1.js"></script>
<!--                   ^^---- here (the letters are transposed)       -->

I couldn't understand why you would be running into a problem and decided to actually copy-and-paste your script tags and replicate the structure exactly on my machine. And things stopped working and my world tilted 3° counter-clockwise until I finally stared at them long enough to see it.

Provided that the jshashtable code really is at /static/jshashtable-2.1.js and your server is serving it up correctly (double-check on Chrome's resources tab in the dev tools), I can't see any reason for that. Your scripts are in the right order, and jshashtable's docs show using a global Hashtable (and the code link you gave clearly shows it creating one).


Edit: I've just replicated that same structure (same scripts, same order, using jQuery(document).ready(function() { ... });) on my own server, and am not having that problem. I can create a Hashtable and use its functions.

My HTML:

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-type" content="text/html;charset=UTF-8">
<title>Test Page</title>
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
<script type='text/javascript' src='jquery-1.4.4.js'></script>
<script type='text/javascript' src='jshashtable-2.1.js'></script>
<script type='text/javascript' src='main.js'></script>
</head>
<body>
</body>
</html>

My main.js:

jQuery(document).ready(function() {
    try {
        var ht = new Hashtable();
        display("typeof ht = " + typeof ht);
        display("ht.size() = " + ht.size());
    }
    catch (e) {
        display("Exception: " + e);
    }

    function display(msg)
    {
        $("<p>").html(msg).appendTo(document.body);
    }
});

Only difference is I'm not using a /static prefix, and I'm absolutely certain that makes no difference.

Sign up to request clarification or add additional context in comments.

9 Comments

I have verified that the jshashtable script is loading properly; the code shows up in dev tools with no errors.
@James: Very strange. I've replicated the structure (just edited my answer) and am not seeing the behavior you're describing. Unless something is actively clearing Hashtable, I can't see why it happens to you.
@James: Found it. I actually copied and pasted your script tags and things stopped working. Ultimately found the typo. Updated.
Brilliant! Thank you - I thought I was missing something simple.
@James: No worries, we both were. Note to self: If the path looks right, and I even see the script load in Resources, check the type attribute. :-) BTW: You can drop the type entirely. Every browser on the planet defaults to JavaScript, and it's now even the official default as of HTML5. Best,
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.