Why can't I use jQuery in amazon (in console)? - javascript

using firebug console in firefox for example when execute this script
$("body").css("border","4px solid red");
it will return an error with message:
TypeError: $("body") is null
same in chrome the error:
TypeError: undefined is not a function
any one knows why? and how to use it?

$ is not a reference to jQuery at that site.
It does appear as though an old version jQuery is loaded...
You can tell by doing this...
jQuery.fn.jquery; // 1.2.6
Also, keep in mind that consoles are often not a pure environment. If there's no other code using the $ variable, they may take it over.
It appears as though Firebug does exactly that. I'm guessing it's a shortcut for document.getElementById, which will return null if there's no element with the ID "body".
The error is different in Chrome because $ is apparently undefined, so you're trying to use undefined as a function.

In the case of Amazon, only jQuery is defined, not $, you can use the jQuery() function instead of $() or simply define $=jQuery
Note the version of jQuery is an old one: 1.2.6
Edit
$=jQuery.noConflict() sounds even cleaner

As "am not i am" stated, Amazon either doesn't use jQuery or doesn't alias it as "$".
If you want to use jQuery on a site like that, you can use a jQuerify-ing bookmarklet like this one:
http://www.learningjquery.com/2009/04/better-stronger-safer-jquerify-bookmarklet
to add jQuery (with the standard "$" alias) to the page temporarily.

Related

Can I combine jQuery with Javascript?

I want to use the Javascript selector document.querySelector insted of $ or jQuery jQuery selector but I want to combine the Javascript selector with jQuery functions (like .getJSON(), .html(), .append() , etc.).
For example:
$.getJSON("list.json", function(data) {
document.querySelector("#content").html(data.name);
});
Here when I use the document.querySelector I get Uncaught TypeError: undefined is not a function and when I use $ I don't get any error.
jsFiddle Test
Is it possible to run jQuery and Javascript together?
Thanks!
Off couse yes! It is possible to run jQuery and JavaScript together in you application.
All you need to know is, which is a JavaScript Object when trying to run some methods of it. jQuery can be handy sometimes. But, yes! You can work with them together.
Secondly, remember that jQuery is a JavaScript's Library. It isn't anything other than JS. To be simple, jQuery needs JavaScript to run. JavaScript doesn't need jQuery to run.
From this MDN source, it is stated that you can use that method just the way it is.
document.querySelector(".someclass");
https://developer.mozilla.org/en-US/docs/Web/API/document.querySelector
All you now need to make sure is of that, that class you're trying to access exists.
$.getJSON("list.json", function(data) {
$(document.querySelector("#content")).html(data.name);
});
PS:
But there isn't any sense to use it everywhere. Check the #afzaal-ahmad-zeeshan answer & read how to use native functional of DOM elements. jQuery isn't a panacea.
jslayer's answer gave me an idea, which seems to work.
Wrapping js code in $() seems to work (though I'm not sure why).
For example, to use slideToggle() (which is only available in jQuery, I think), doing
event.target.nextElementSibling.slideToggle()
does not work, but
$(event.target.nextElementSibling).slideToggle()
does.

Cannot get jQuery to work on site

Okay, I have been working for quite some time on a website for a friend..
My coding skills are .. questionable, and I've been having quite a few problems.
Currently the jQuery on my site simply stopped working, I could not find the reason, and I have done everything I could to try to get it to work.
( I have followed countless guides all over the internet, for troubleshooting etc. and I still cannot get it to work)
-EDIT-
I have moved all the files to the top of the code.. Yet the problem persists.
Sincerely yours, Malmoc
You are trying to use jQuery code before jQuery.js is loaded.
jQuery.js must load before any dependent code or plugins. Use a browser console and look at errors thrown on page load. "$" is not defined error is a quick indication of loading problem with jQuery
Think of it this way. jQuery library contains a number of functions, including defining "$". If these functions or "$" aren't already available when you call them, they are undefined and errors get thrown and your code won't work
Once you have jQuery script tag before other code, you may still run into complications if you recently added prototype library which also uses "$" alias. This can cause conflicts but there is a workaround using jQuery.noConflict()
Very odd, I suggest you set the source to jQuery to the website here:
http://code.jquery.com/jquery-1.8.2.min.js
Seems to be quicker response time, and in your source code you appear to link jQuery twice, that may be causing some issues.
Your $ is also getting overwritten by another script. Best that you use jQuery.noConflict() for this
http://api.jquery.com/jQuery.noConflict/
And then you can put your code in a closure like this:
(function(){
var $ = jQuery;
// jQuery code using $
})(this);
Looks like you're mixing mootools and jquery. Please resolve your conflict between jquery's $ namespace and mootools' $ namespace.
google "jquery no conflict"

strange syntax in javascript to access a specific id in the dom

I am currently working on an old project with ton of legacy code.
A syntax I have never met is used to access a specific id in the dom in javascript.
Instead of using document.getElementById("btnsubmit"), $('btnsubmit') is used.
I have never met this syntax. Moreover it seems that firebug doesn't like it either as it seems to break the debugger. I have issues where the code doesn't seem to be executed in a debugging environment although this code is used on a production site and seems to work.
Does any one have a reference on this syntax? Where does it comes from, is it deprecated?
It's from a javascript library, and in general it's more modern than getElementById. You need the script include though.
Your example looks like Prototype
$ is just a regular character in javascript and it is often used by javascript libraries and defined to be a function name so that $() is just a function call. In some cases, $ might be defined to be a synonym for document.getElementById() as shorthand to save typing and in other cases, it's a more robust CSS3 style selector engine (as in the jQuery library).
In either case, if its undefined in your code, then you are probably missing a library reference that your code relies on. You will need to find out what library your code was written to use and make sure that library is included in the code before this spot so that the $() function is defined properly.

IE8 returns “object doesn't support this property or method” when I use the equalHeights jQuery plug-in

I'm getting a "object doesn't support this property or method" error in IE8 on my webpage and was just wondering if anyone could see anything wrong with the following code that it is flagging it up on:
excerptWrapper.equalHeights().find('.position_excerpt').css({display:'block', opacity:0, position:'absolute'});
var excerptWrapperHeight = excerptWrapper.height();
Either:
excerptWrapper is not a jQuery object. I.e. it wasn't created with $(somethingHere).
jQuery is not loaded.
The equalHeights plugin is not loaded.
If your equalHeights plugin isn't the one I found here, then it may not return the jQuery object after it executes, therefore the rest of the statement is invalid.

Fix JQuery-1.4.1 and json-2.0 conflict in json.parse regex conflict with "$" character

After upgrading to jquery 1.4.1 i noticed there was an error anytime i tried calling json.parse. The issue is part of the regex used in json. it uses a $ in the pattern that conflicts with JQuery's $ shortcut.
I don't want to use the non-conflict option with jquery because i have tons of places i'd have to replace the $ with the new corrected shortcut.
Is there a way to wrap a regex pattern in single quotes or something so the pattern string is handled as literally a string?
Broken section in json-2.0.js: (fails on the $)
if (/^[\],:{}\s]*$/.
test(text.replace(/\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g, '#').
replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, ']').
replace(/(?:^|:|,)(?:\s*\[)+/g, ''))) {
.....
}
Thanks
Update:
The problem was not as it appeared and didn't have to do with a $ conflict. From the OP:
The error was bombing on test.replace because the object that was passed in was already deserialized so the method replace was not found. I guess upgrading to JQuery 1.4.1 had some changes in the way the result object is handled on the success event of the $.ajax function.
Are you sure it's failing on the $, and for that reason? Because that's a massive namespacing/parsing failure if so. There's no reason at all the JavaScript engine should be looking for an external symbol there. It's already encapsulated in the way you asked about (by the slashes, which are effectively quotes for a regex). If that were really the problem, it would be every bit as surprising as the interpreter choking on a $ inside a string. I think your problem lies elsewhere.
Here's a page that pulls in jQuery 1.4.1 and json2.js, and it calls JSON.parse(), and it gets no exceptions or errors: http://gutfullofbeer.net/json.html
The error was bombing on test.replace the object that was passed in was already deserialized so the method replace was not found. I guess upgrading to JQuery 1.4.1 had some changes in the way the result object is handled on the success event of the $.ajax function.
This question should be deleted
Apparently this question cannot be deleted with the amount of answers that have been suggested.

Categories