Why no $.connection object defined? - javascript

This is my HTML beginning:
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="/Scripts/jquery-1.8.2.min.js"></script>
<script src="/Scripts/jquery.signalR-0.5.3.js" type="text/javascript"></script>
<script src="/Scripts/MyScript.js" type="text/javascript"></script>
<title>Some title</title>
</head>
I tried adding just before </head>:
<script type="text/javascript" src="/signalr/hubs"></script>
But it doesn't help.
I also tried to remove everything besides jQuery and signalR scripts.
Please note: All scripts, including jquery.signalR-0.5.3.js (and hubs, if I add it) are loaded successfully (I can single step it with Chrome "Developer Tools").
So why I don't have $.connection object defined ?
Edit:
I cleaned all unnecessary code, and found out meanwhile that:
$.connection does exist when I'm in MyScript.js, but I tried to test it first using the chrome console. In the console it does not exist... I don't understand why.
I save $.connection for the moment, in order to test it, in someGlobalObj:
someGlobalObj = $.connection;
and that works...
So my updated question would be: how-come $.connection is visible during my script, but invisible in the chrome console ($, or jQuery are globals) ?

how-come $.connection is visible during my script, but invisible in the chrome console ($, or jQuery are globals)?
There are only two explanations: Something that executes after your script (a timeout, some other script)…
…deletes the connection property of the jQuery object
…overwrites the global $ (and even jQuery?) variables with similiar object(s) which don't have the connection property.
The second one could be the case if you also use other libraries (like Prototype).

Related

Why can't I use document in Brackets?

I'm using the Brackets IDE and I tried basic JS. Using the object document sends me to an error saying
"ERROR: 'document' is not defined. [no-undef]",
I can't find out why is this error happening since it works perfectly in dreamweaver, does anyone know why is this happening in Brackets? Thank you.
HTML:
<!DOCTYPE HTML>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<title>Parrafos</title>
</head>
<body>
<p>a</p>
<p>aa</p>
<p id="special">especial</p>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
<script type="javascript" src="parrafos.js"></script>
</body>
</html>
JS:
function parrafo(texto, id) {
document.getElementById(id).textContent = texto;
}
parrafo("Hola qué tal", "especial");
document is not part of JavaScript, it is part of the browser DOM.
Whatever linter (e.g. JSHint, JSLint, ESLint) you have configured Brackets to use to test you code with hasn't been configured to assume the presense of global variables that exist when you run the code through a script element in an HTML document.
Consult the documentation for whichever linter you are using to find out how to do that (likely this will either be a configuration file in the root of your project directory or a specially formatted comment at the top of your JS file).
"ERROR: 'document' is not defined. [no-undef]" Means that every variables should be declared, in top of your code state: var document; or disable lint from checking [no-undef]
UPDATE
After the comments, if you want to pick the first option you can write:
var document = window.document;
to suppress the message and be clear

jQuery code always returns "not defined" in Sublime

I have my jQuery linked in the html file before the js file, and I've ensured that it is loading properly in the browser window. The jQuery code actually WORKS, so I have no idea why sublime thinks it cant understand the sytax. I've tried just about every suggestion in this post (JQuery - $ is not defined) which there was a ton. Also, i dont currently have sublimeLinter installed, currently using a console that i set up with this (http://www.wikihow.com/Create-a-Javascript-Console-in-Sublime-Text).
This code:
<html>
<head>
<script type="text/javascript" src="jquery-3.2.1.min.js"></script>
<script type="text/javascript" src="script.js"></script>
</head>
<body> </body>
</html>
with this js:
var $j = jQuery.noConflict();
$j(document).ready(function() {
$j("body").text("Hello world!");
});
leads to this error message:
Add an ignore comment to the top of your document. What is happening is that your linter is not seeing jQuery as it is not part of the current document. This will force your linter to ignore the jQuery keyword while linting.
/*globals $:false, jQuery:false*/

Uncaught ReferenceError $ is not defined

I'm very new to JavaScript (just started a few hours ago and trying to get a script working). I went through a few tutorials on W3 and the 'hello world' code works when I paste it directly into my HTML but I'm having a problem with a script (I've had problems with other scripts as well but I am not sure what I'm doing wrong).
I have this code that I want to test in my HTML, I copied the HTML in and it looks the same then I made a file in my static folder called edit.js and copied the JavaScript into it (exactly as shown). It didn't work no errors on the page but when I click it nothing happens. I tried to paste a W3 'hello world' code in and that worked but this script does not.
I tried to inspect the code in Chrome and that's where I see the above error (under the resources tab). I can open the js file using Chrome which makes me think the js file is accessible and pointing correctly but I'm not sure how to get it working. I'm using Jinja2 as my template engine to render the HTML and in my header I have:
<script language="JavaScript" type="text/javascript" src="static/edit.js"></script>
and in my main template (the one that gets rendered on all pages) I have:
<script language="JavaScript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" type="text/javascript"></script>
edit.js:
(even putting it within the script tag directly on the page I want to use it on doesn't work)
$('#editvalue').click(function(e){$('#storedvalue').hide();$('#altervalue').show();});
$('#savevalue').click(function(e){
var showNew = $('#changevalue').val();
$('#altervalue').hide();
$('#storedvalue').show();
$('#storedvalue span').text(showNew);
});​
HTML:
(it's embedded in a larger page)
<head>
<script language="JavaScript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" type="text/javascript"></script>
<script language="JavaScript" type="text/javascript" src="static/edit.js"></script>
</head>
... my html code..
<div id="wrapper">
<div id="container">
<div id="storedvalue"><span>Hello</span> [edit]</div>
<div id="altervalue" style="display:none;"><input type="text" name="changevalue" id="changevalue" value="Hello"> [save]</div>
</div>
</div>
I have never been able to successfully run a JavaScript that wasn't on W3 yet. I get the same problem with other scripts even though I see people online saying they work fine for them. Do I need to do anything extra to make this work?
My two questions are:
What am I doing wrong?
Because Javascript seems to just not work when there's a problem, is there a way to get errors or information on what's actually wrong?
I read Uncaught ReferenceError: $ is not defined? and have been trying to figure this out for the last hour and can't see my problem.
First you need to place the jQuery script tag first.
Second, you need to do one of the following things:
Put your code within this function:
$(document).ready(function(){/*CODE HERE*/});
Or like this:
$(function(){
/*CODE HERE*/
});
The DOM needs to be ready before you can use it. Placing your code within anonymous functions that are executed on the ready event of the DOM is how you can do this.
Edit:
$(function(){
$('#editvalue').click(function(e){$('#storedvalue').hide();$('#altervalue').show();});
$('#savevalue').click(function(e){
var showNew = $('#changevalue').val();
$('#altervalue').hide();
$('#storedvalue').show();
$('#storedvalue span').text(showNew);
});​
});
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" type="text/javascript"></script>
Script tag for jQuery should come before your custom javascript.
Follow by edit.js
<script type="text/javascript" src="static/edit.js"></script>
Try removing the language attribute..sometimes work for me. It's obsolete now .. i think
You need to include jquery before you can use it.

Scriptaculous load parameter in FF 2.0.x

Greetings all,
I am attempting to explicitly load the effects.js and builder.js Scriptaculous libraries on a page, using this code:
<script type="text/javascript" src="/javascripts/scriptaculous.js?load=builder,effects"></script>
This works like a charm in FF 3.0.x, IE7 and Opera 9.6.x. It does not work in Firefox 2.0.x, however. The libraries never become loaded. In order to get them to load in FF 2.0.x, I must explicitly reference them with two extra <script /> tags, i.e.:
<script type="text/javascript" src="/javascripts/scriptaculous.js?load=builder,effects"></script>
<script type="text/javascript" src="/javascripts/builder.js"></script>
<script type="text/javascript" src="/javascripts/effects.js"></script>
Does anyone happen to know what the discrepency between FF 2.0 and 3.0 is that causes this behavior? Is there a better solution to my problem?
Thanks for your help!
I've had too much coffee today, so I figure I will give this a go.
One possibility is the load function in scriptaculous.js does not correctly do the processing to include the libraries passed to it as parameters (scriptaculous.js?load=builder,effects).
Try putting in an alert to see if the load function in scriptaculous.js is being entered into, if it is, then the process probably doesn't do what it's supposed to on FF2:
load: function() {
alert('In the load function!');
...rest of code here...
If it isn't, then (maybe) firefox 2 does not want to execute load.
The last part of load seems to do the work for including other libs:
$A(document.getElementsByTagName("script")).findAll( function(s) {
return (s.src && s.src.match(/scriptaculous\.js(\?.*)?$/))
}).each( function(s) {
var path = s.src.replace(/scriptaculous\.js(\?.*)?$/,'');
var includes = s.src.match(/\?.*load=([a-z,]*)/);
(includes ? includes[1] : 'builder,effects,dragdrop,controls,slider,sound').split(',').each(
function(include) { Scriptaculous.require(path+include+'.js') });
});
From the above code, I can see that the includes variable should parse out the library names, see if that's being assigned anything, replace it with something like:
var includes = s.src.match(/\?.*load=([a-z,]*)/);
alert(includes[0] + ' ' + includes[1]);
That should give you a better idea of what's going on. While this is an interesting little problem, I would definitely go with the solution you proposed:
<script type="text/javascript" src="/javascripts/scriptaculous.js"></script>
<script type="text/javascript" src="/javascripts/builder.js"></script>
<script type="text/javascript" src="/javascripts/effects.js"></script>

Cannot get Adobe AIR's ApplicationUpdaterUI() to work

Here is my problem - I'm trying to write a self-updating application, but I keep getting an error saying that runtime.air.update.ApplicationUpdaterUI() does not return a constructor.
Here's the relevant section of the code; there are other javascript files being included, but I don't think that any of them would be actively breaking AIR itself.
<html>
<head>
<script type="text/javascript" src="./js/AIRAliases.js"></script>
<script type="text/javascript" src="./js/jquery-1.3.1.js"></script>
<script src="ApplicationUpdater_UI.swf" type="application/x-shockwave-flash"></script>
<script>
$(document).ready( function() {
var appUpdater = new runtime.air.update.ApplicationUpdaterUI(); // line 64 in this example
}
</script>
</head>
<body> ... stuff ... </body>
</html>
And the error that I get when I test it is
TypeError: Value is not a constructor. Cannot be used with new.
at app:/index3.html : 64
at app:/js/jquery-1.3.1.js : 2912
at app:/js/jquery-1.3.1.js : 686
at app:/js/jquery-1.3.1.js : 2916
at app:/js/jquery-1.3.1.js : 2936
Verify the path in that script tag for the SWF, I'm guessing you do not have the reference to the ApplicationUpdater_UI.swf correct.
Air is basically complaining that it cannot find a runtime.air.update.ApplicationUpdaterUI() method to call anywhere, which likely means it can't find the SWF (or I suppose it's possible the SWF is corrupted).
Not sure if this is related, but neither $(document).ready nor .load will work if you load the .swf before the script. Make sure you put the .swf reference at the very bottom of your page.

Categories