javascript calling object - javascript

I'm kind of new to this so a bit confused.
I have a js file named rrr.js, in which I have this code:
var rrr_rrr2=
{
// get the domain name from the current url
get_domain_name:function()
{
//code here...
},
// other functions here
}
Now in my HTML page I simply added it like I usually do:
<script type="text/javascript" src="rrr.js">
and called it like this:
Step 1 completed!<br><br>Click here
But that does not work... what am I doing wrong?
(by the way, this is in a firefox addon. FF gives me this error:
Error: rrr_rrr2 is not defined
Source File: javascript:rrr_rrr2.get_domain_name()
Line: 1
Help please!

I think Satyajit was almost right. Try closing it like this instead:
<script type="text/javascript" src="rrr.js"></script>
But also, if this js file is part of your addon, you can't access it directly from an HTML page, unless you put it at a resource: URI or something. Read up on privileged vs. unprivileged code.

Could it be that you have not closed the tag like so
<script type="text/javascript" src="rrr.js"**/**>

Nothin is wrong with the code you have just shown, I have reproduced it in this JsFiddle.
There is one thing to look out for: the variable rrr_rrr2 must be in the global scope to access it with the javascript: href. If it is defined inside of a function, for example, then it is not in the global scope and cannot be accessed.

Related

Javascript function not defined but it is included

What could've happened if I get the error msg ReferenceError: dragInit is not defined when I actually include the js file where dragInit is defined?
How can I resolve the error? Why is the function not known when it is included?
The inclusion looks like:
<script type="text/javascript" src="http://schema.medinet.dev//common/schema6.js?edb560209503a4f78bda807bc3217559"></script>
And then later in the page an inlined call generates the error message:
<script type="text/javascript" language="JavaScript">dragInit();</script>
The actual function in schema6.js look like:
function dragInit() {
document.onmousemove = update; // update(event) implied on NS, update(null) implied on IE
update();
}
Couple of things to check:
Check the script has loaded correctly - look for 404's in the network tab
Check the order in which scripts are loaded - is dragInit() available when it is called?
You can use most browser tools to debug the JavaScript as it is running. Take a look at MDN for details on how to do this.

IE6 Script Tag error

I've never seen this error. We have some clients that use Internet Explorer 6, so we need some pages to work on it.
We have the following HTML code:
<script type="text/javascript">
var bust = 236;
</script>
IE6 is throwing the following error message: 'Undefined' is null or not an object.
Do you have any idea what it might be?
Thanks.
It sounds like bust is conflicting with some other global. The global name space is really crowded.
You may be able to resolve it by wrapping your code in a scoping function so that it's not at global scope anymore:
<script type="text/javascript">
(function() {
var bust = 236;
})();
</script>
Or if it has to be a global, try another name until you find one that doesn't cause the problem. Global variables aren't fundamentally broken, even in IE6.

Error on using alert in Javascript (Property 'alert' of object is not a function)

I am just trying to use alert and put a string variable inside the alert and get an error:
Uncaught TypeError: Property 'alert' of object [Object Window] is not a function
My code is:
var shortenurl = msg.d;
alert(shortenurl);
I've checked the value and it has a string inside, not an object.
Somewhere in your code you overrode alert. Check for var alert = ... or some other kind of declaration like that. Also check for window.alert declarations.
I had that error message due to an alert() blocked by my pop-up-blocker.
I'm adding this one as an addition to this. In my case, when I had a similar problem, it turned out to not be my own code that was causing the problem but a poorly written extension that had been added to a client's browser. Once it was disabled, the script error went away.
If you haven't overridden the method name in your own code anywhere, you may want to try disabling extensions to see if any of those is inadvertently interfering with your script.
Check if you've got a Bootstrap .js declaration if required (after jQuery) i.e.
<script src="//code.jquery.com/jquery-1.11.0.min.js"></script>
<script src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.2/js/bootstrap.min.js"></script>
Mozilla says,
The alert function is not actually a part of JavaScript itself.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Introduction_to_Object-Oriented_JavaScript
You can not see a function called alert here : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects
Adding to Chris's answer... I had mistakenly overrode alert in my own function!
//Don't do this:
function alertOrConsole(node, alert){
if(alert){
alert(node);
}else{
console.log(node);
}
}
//----------------------------
//Fixed:
function alertOrConsole(node, flag){
if(flag){
alert(node);
}else{
console.log(node);
}
}
One of the possible reasons could be that alert is used as variable - for example inside of function:
function MyFunction(v1,alert)
{
alert(v1); //will fail exactly with that message
}
Solution is not to use predefined words as variables.

Uncaught TypeError: undefined is not a function on loading jquery-min.js

I'm building a normal webpage which requires me to load about five CSS files and ten Javascript files.
When loading them separately in the HTML page, my webpage loads fine.
Now for production, I concatenated all the Javascript into a single file, in the order needed, and all the CSS into another file. But when I try to run the web page with the concatenated files it throws an error saying:
Uncaught TypeError: undefined is not a function
On the line where jquery.min.js is being loaded in the concatenated Javascript file.
What can I do to mitigate this? I want to concatenate all files and minify them for production. Please help.
EDIT: I merged the Javascript and CSS in the order they were when they were being loaded individually and were working fine.
Assuming this problem still has not be resolved, a lot of individual files don't end their code with a semicolon. Most jQuery scripts end with (jQuery) and you need to have (jQuery);.
As separate files the script will load just fine but as one individual file you need the semicolons.
You might have to re-check the order in which you are merging the files,
it should be something like:
jquery.min.js
jquery-ui.js
any third party plugins you loading
your custom JS
This solution worked for me
;(function($){
// your code
})(jQuery);
Move your code inside the closure and use $ instead of jQuery
I found the above solution in https://magento.stackexchange.com/questions/33348/uncaught-typeerror-undefined-is-not-a-function-when-using-a-jquery-plugin-in-ma
after seraching too much
I got the same error from having two references to different versions of jQuery.
In my master page:
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
And also on the page:
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"> </script>
I had this problem recently with the jQuery Validation plug-in, using Squishit, also getting the js error:
"undefined is not a function"
I fixed it by changing the reference to the unminified jquery.validate.js file, rather than jquery.validate.min.js.
#MvcHtmlString.Create(
#SquishIt.Framework.Bundle.JavaScript()
.Add("~/Scripts/Libraries/jquery-1.8.2.min.js")
.Add("~/Scripts/Libraries/jquery-ui-1.9.1.custom.min.js")
.Add("~/Scripts/Libraries/jquery.unobtrusive-ajax.min.js")
.Add("~/Scripts/Libraries/jquery.validate.js")
.Add("~/Scripts/Libraries/jquery.validate.unobtrusive.js")
... more files
I think that the minified version of certain files, when further compressed using Squishit, for example, might in some cases not deal with missing semi-colons and the like, as #Dustin suggests, so you might have to experiment with which files you can doubly compress, and which you just leave to Squishit or whatever you're bundling with.
For those out there who still couldn't fix this, I did so by changing my 'this' to '$(this)' when using jQuery.
E.G:
$('.icon').click(function() {
this.fadeOut();
});
Fixed:
$('.icon').click(function() {
$(this).fadeOut();
});
I've run into the very same issue, when mistakenly named variable with the very same name, as function.
So this:
isLive = isLive(data);
failed, generating OP's mentioned error message.
Fix to this was as simple as changing above line to:
isItALive = isLive(data);
I don't know, how much does it helps in this situation, but I decided to put this answer for others looking for a solution for similar problems.
Yes, i also I fixed it changing in the js libraries to the unminified.
For example, in the tag, change:
<script type="text/javascript" src="js/jquery.ui.core.min.js"></script>
<script type="text/javascript" src="js/jquery.ui.widget.min.js"></script>
<script type="text/javascript" src="js/jquery.ui.rcarousel.min.js"></script>
For:
<script type="text/javascript" src="js/jquery.ui.core.js"></script>
<script type="text/javascript" src="js/jquery.ui.widget.js"></script>
<script type="text/javascript" src="js/jquery.ui.rcarousel.js"></script>
Quiting the 'min' as unminified.
Thanks for the idea.
Remember: Javascript functions are CASE SENSITIVE.
I had a case where I'm pretty sure that my code would run smoothly. But still, got an error and I checked the Javascript console of Google Chrome to check what it is.
My error line is
opt.SetAttribute("value",values[a]);
And got the same error message:
Uncaught TypeError: undefined is not a function
Nothing seems wrong with the code above but it was not running. I troubleshoot for almost an hour and then compared it with my other running code. My error is that it was set to SetAttribute, which should be setAttribute.
In case there are any morons out there like me, I had this frustrating problem because I forgot a simple
new
keyword before instantiating a new object.
I just had the same message with the following code (in IcedCoffeeScript):
f = (err,cb) ->
cb null, true
await f defer err, res
console.log err if err
This seemed to me like regular ICS code. I unfolded the await-defer construct to regular CoffeeScript:
f (err,res) ->
console.log err if err
What really happend was that I tried to pass 1 callback function( with 2 parameters ) to function f expecting two parameters, effectively not setting cb inside f, which the compiler correctly reported as undefined is not a function.
The mistake happened because I blindly pasted callback-style boilerplate code. f doesn't need an err parameter passed into it, thus should simply be:
f = (cb) ->
cb null, true
f (err,res) ->
console.log err if err
In the general case, I'd recommend to double-check function signatures and invocations for matching arities. The call-stack in the error message should be able to provide helpful hints.
In your special case, I recommend looking for function definitions appearing twice in the merged file, with different signatures, or assignments to global variables holding functions.
Make sure you have commented out any commentaries. Sometimes when copying and pasting you will leave out the "/*!"
Also when you go into the console they will list your errors and you should take it one at a time. If you see "Uncaught SyntaxError: Unexpected token * " That might mean it is reading your js file and it isn't getting past the first line.
/*!
* jquery.tools 1.1.2 - The missing UI library for the Web
*
* [tools.tabs-1.0.4, tools.tooltip-1.1.2, tools.scrollable-1.1.2, tools.overlay-1.1.2, tools.expose-1.0.5]
*
* Copyright (c) 2009 Tero Piirainen
* http://flowplayer.org/tools/
* File generated: Wed Oct 07 09:40:16 GMT 2009
*/
I got this when I accidentally passed too many parameters into a jquery function that only expected one callback parameter.
For others troubleshooting: make sure you check all your jquery function calls for extra parameters.

Safari generates "Can't find variable" error although it is already defined in another Javascript page

The JavaScript script works with Chrome and Firefox but not in Safari. The code is:
$(document).ready(function(){
$(window).load(function() {
myVariable.start();
});
This generates:
ReferenceError: can't find variable: myVariable
The variable is defined in another JavaScript page that is included in this page, but for some reason Safari doesn't see the definition in the other page. Is Safari executing this script without loading the page that the variable is defined in?
How can I fix this?
Thanks for any help
Try to avoid setting global variables.
Maybe try assigning your variable to the window object, on top of the page:
window.myVariable = { start: function() {} };
Then when you need it:
$(window).load(function() {
window.myVariable.start();
});

Categories

Resources