Ok .. this is a strange one as I have NOT seen this before. I have an application that is strictly a Service ... no browser involved ... and all I want to do is use alert(); for debugging. The only problem is that it causes an Object Expected error even if it is a simple alert("Show me!");
Remember ... this code is not attached to any form or browser. So what am I missing? I thought I could use the alert call at any time in Javascript ... Please folks, help a poor programmer out!
Thank you in advance,
Eric
Like praneeth already suggested in his reply, it is a Windows WScript thing, or rather just context in which the script is being run in.
This also works and isn't quite as verbose as what praneeth offered:
WScript.Echo("Hello");
if you are executing this script on a windows machine you can do like this in javascript/Jscript
Var Shell = new ActiveXObject("WScript.Shell");
Shell.Popup("Your Debug message");
The alert() method is one of the JavaScript browser Window object's methods which displays an alert box with a message and an OK button.
The window object represents an open window in a browser. If a document contain frames, the browser creates one window object for the HTML document and one additional window object for each frame.
I believe that in the specified case, the error means that the Window expected object has not been found.
Have you tried window.alert("show me");?
Since alert() is a Window object method.
If you're not running in a browser, you might have better luck using the console.log method - again, it's very hard to tell you specifically what to do without any detail of what environment you're executing the script in.
Related
I made a function called test() in javascript file.Placed a simple alert into it.
In html file, called the method on click of a button. But,it was not being invoked.
Problem was in the 11th function, nowhere related to mine !!!! But, how can a person making his first javascript function suppose to find that out ???
I am looking for best ways to debug javascript.
You can debug javascript using many modern browsers. See this question for details on how to debug in Google Chrome:
How do you launch the JavaScript debugger in Google Chrome?
Furthermore, you shouldn't use alert() for debugging as this can give different results to a production version due to alert() causing a pause in the script.
It is best practice to use console.log() and view the output in the browsers Console.
You can also put debugger in your javascript code to force a breakpoint. However I prefer not to use this as forgetting to remove this before deployment will cause your script to pause, which can be quite embarrassing!
You should use the debug console provided by the browser.
Chrome has it inbuilt, press CTRL + SHIFT + j. In Firefox, install Firebug plugin.
In your code, add alert() to show flow and get values of variables.
Also, use console.log() which will only output to the debug console.
Depending on your browser choice there are debugging options - I tend to use Firefox, so Firebug in my case. There is a question that list options for other browsers - What is console.log and how do I use it?
Unless the project you're working on has already adopted a mechanism for debugging, console.log() tends to be a simple and useful option when tracking down a problem.
Whilst debugging you could take the approach to log out a line when entering a function, like so:
var myFunc = function(el) {
console.log('Inside myFunc');
// Existing code
};
This will enable you to see which functions have been called and give you a rough idea of the order of execution.
You can also use console.log() to show the contents of variables - console.log(el);
Be mindful to remove/disable console.log() calls once you're done as it will likely cause some issues in production.
To answer your question within question,
how can a person making his first javascript function suppose to find that out ???
Well, when something is wrong in JavaScript, for example, you made a syntax error - the script will stop working from there. However, this won't stop HTML from rendering on, so it might look as if everything is correct (especially if your JS is not changing the look of the page) but all the functionality of JS will be dead.
That's why we use the debug tools (listed in the other answers here) to see what's wrong, and in cases like this, it's very easy to notice which function has errors and is causing the whole script to break. This would probably have save a few minutes to your seniors as well.
The best approach would be to test frequently so that whenever you run into errors, you can fix them right away.
I am a chronic user of Firebug, and I frequently need to log various stuff so that I can see what I am doing. The console.log function is a lot to type. Even if I assign it to a single letter variable like q = console.log, I have to do it every time I fire up Firebug. Is there any way to do it such that q always refer to console.log (unless, of course, I override it in my session)?
To answer your question, the functionality doesn't currently exist, however I have found the firebug developers to be very responsive in the past. Why don't you put in a feature request on their forum, or better yet, code it up yourself, and ask them to add it?
Depending on your IDE, simply setup a code snippet (I use Flash Develop, so Tools -> Code Snippets).
I believe this to be a better way than setting up redirect scripts and what not, because it stops the Firebug namespace from being polluted, and makes it easier/more consistent to debug if your debugging breaks down.
The screenshot shows me using Flash Develop, hitting Ctrl+B, then hit enter. The pipe (|) in the snippet indicates where the cursor will be placed to start typing after inserting the snippet.
I'd like to write a test case (using Selenium, but not the point of this question) to validate that my web application has no script errors\warnings or unhanded exceptions at certain points in time (like after initializing a major library).
This information can easily be seen in the debug consoles of most browsers. Is it possible to execute a javascript statement to get this information programatically?
It's okay if it's different for each browser, I can deal with that.
not so far read about your issue (as far as I understood your problem) here
The idea be the following:
I found, however, that I was often getting JavaScript errors when the page first loaded (because I was working on the JS and was introducing errors), so I was looking for a quick way to add an assert to my test to check whether any JS errors occurred. After some Googling I came to the conclusion that there is nothing built into Selenium to support this, but there are a number of hacks that can be used to accomplish it. I'm going to describe one of them here. Let me state again, for the record, that this is pretty hacky. I'd love to hear from others who may have better solutions.
I simply add a script to my page that will catch any JS errors by intercepting the window.onerror event:
<script type="text/javascript">
window.onerror=function(msg){
$("body").attr("JSError",msg);
}
</script>
This will cause an attribute called JSError with a value corresponding to the JavaScript error message to be added to the body tag of my document if a JavaScript error occurs. Note that I'm using jQuery to do this, so this specific example won't work if jQuery fails to load. Then, in my Selenium test, I just use the command assertElementNotPresent with a target of //body[#JSError]. Now, if any JavaScript errors occur on the page my test will fail and I'll know I have to address them first. If, for some strange reason, I want to check for a particular JavaScript error, I could use the assertElementPresent command with a target of //body[#JSError='the error message'].
Hope this fresh idea helps you :)
try {
//code
} catch(exception) {
//send ajax request: exception.message, exception.stack, etc.
}
More info - MDN Documentation
In our (quite large and old) ASP.NET application we use a lot of pages loaded into frames, iframes, and modal dialogs (using window.showModalDialog). We are starting to see the error above quite a bit, and I can't seem to find a single rational explanation for it anywhere.
Popup Blockers. Nope. We're not running them. Not even the built-in blocker.
Trusted Zone. Nope. The application runs on LocalHost right now, and it's in the trusted sites list.
Stray Cosmic Rays. Possible, but not probable. It's way too consistent.
I did eventually find the error message buried on Microsoft's site in some dusty tome about retrieving automation error message information. In it, they were talking about Excel, and they said: "In this example, Microsoft Excel is the server application. Referencing a workbook object once it is destroyed (or closed) generates the error."
That is probably as close as I've ever come to an explanation for the cause of the error, without a real, concrete explanation. Someone tried to use something after their reference to it was disposed of. Oddly, you can still see the windows on the screen. Curiously, however, this smacks suspiciously to me of the accepted answer to this.
So here's what happens.
Page A is the main page.
PageA displays PageB in a frame. PageB is a toolbar.
PageA displays PageC in another frame. That's the content.
PageC displays PageD in a nonmodal dialog.
PageD, for reasons unknown to me, wants to modify the controls in PageB. It's trying to use window.opener to do that, and failing horribly.
If someone could enlighten me as to why this is the case (the code works in FF), I'd appreciate it.
Although my answer isn't directly applicable to this particular question, if you're getting this error (The callee (server [not server application]) is not available and disappeared;) when communicating between a pop-up window and the opener window, it's because the pop-up window created an object which it then passed to the opener window. When the pop-up window is closed, Internet Explorer kills all objects created by the pop-up window. And since the object is passed by reference, the object to which the opener window references is now gone. An easy workaround is to pass by value by converting the object to a JSON string using JSON.stringify. You can then convert the string back to an object in window.opener using JSON.parse().
Example:
Pop-up Window:
window.opener.callback(JSON.stringify({
id: 1,
name: "foo"
}));
Opener Window:
window.callback = function (response) {
var foo = JSON.parse(response);
};
Using this approach, the opener window is no longer referencing the object that was created in the pop-up window, so the object in the opener window will survive after the pop-up is closed.
I should have updated this question earlier, and I apologize for the delay. I've learned a bit since I posted it, and here's what I've learned.
For windows opened with window.showModalDialog, the window.opener method returns null, rather than a reference to the opening window. To get a reference to the opening window, you have to pass it as a dialog argument.
It's unclear to me at this point whether or not this is intended behavior; it's apparently undocumented behavior. Further, according to MSDN, window.opener is only valid for pages loaded into frames and iframes.
I had the exact same error message in following scenario:
window A pop ups wind B, user search something and wind B calls wind A passing some parameter.
Method called on wind A suppose to close popup wind B after it finish doing what it's doing.
I was passing parameter as object:
p ={a: 1, b:"c"}
after I rewrite the code to pass each parameter separately error disappeared.
callingMethod(1,"c");
Hope that helps somebody...
you can get this error in excel
Sub LoadParameterForm()
If frmParm Is Nothing Then
Set frmParm = New frm_setParameters
End If
frmParm.Show
end sub
Here frmParm is a module level variable.
Clicking the OK button, I intend to .HIDE() the form so the user's choices are sticky.
If the user clicks on the control box X and closes the form, then you essentially have frmParm pointing at something that no longer exists.
I have not found a way to test for the condition, I trap for the error, set the form to nothing and try again.
I realize this is not exactly the question being asked, but it is a concrete example of the related excel issue mentioned several times in the posts.
I'm working on a new project which has some complex javascript. I can't post any code so that's not what my question is about.
I have a script which works in Firefox 3.0. It was pointed out that the script did not work in Firefox 3.5, so I'm trying to make it work. Indeed the script didn't produce the expected results, so I installed the latest version of Firebug, enabled the console and refreshed the page.
And wow, it worked.
No errors, warnings nothing.
So I disabled the console, and then it didn't work anymore...
What's going on here? The Firebug console somehow changes something in Firefox that makes my script work? Any advice on what next? (besides asking future visitors to install Firebug...)
Could it be something as simple as forgetting to comment a call to console.log() somewhere in your javascript?
If you have hanging references, and the user doesn't have Firebug installed, you're going to get a runtime error that will halt execution of the script.
It sounds to me like there's a chance you have a threading problem, and FireBug is analyzing and possibly slowing down one of the threads so that it has time to complete before the next step is resolved.
Are you possibly utilizing ajax, and something is waiting on that response? Or possibly you're doing something on or after the load of an object that is depending on something else in the DOM?
UPDATE:
For those stumbling upon this now, "threads" in JavaScript really only exist in abstraction (web workers, etc). I was mis-using the term. I was really thinking of an asynchronous action that returned before another one was ready.
Check in your code for console.log(), console.debug().Calling window.console objects methods throws an error if console is undefined (as expected).
In most cases you can easily delete or comment that lines.
I wrote a simple wrapper for firebug (I just use debug but it should give you what you need to duplicate the other methods) that only writes when the console is there so I can use firebug, don't need to go comment out my debug statements and it doesn't break sites for people without it.
If you use this code then use fbconsole.debug instead of console.debug you will never have this problem:
function fbconsole () {
this.debug = function (val) {
if(typeof(console) !== 'undefined' && console != null) {
console.debug(val);
}
}
}
var fbconsole = new fbconsole();