Edit I solved my actual problem in a way where this was no longer an issue, so I no longer need a solution to this. The question can remain though.
In a nodejs project I am analyzing dependencies by requiring them. This causes problems when some modules were built for the browser and expect window or document to be available. so for example, when I do require('some-module') I can get this kind of error:
hasEventListeners = !!window.addEventListener,
^
ReferenceError: window is not defined
Is there any way I can mimic the window to get this type of code to work? The properties on window does not need to work as the actual version, I just need require('some-module') to return a valid value.
Related
I have a rather large app made in Appcelerator Titanium, which I've not ported from the SDK version 3.2 because the Ti.Ui.Window's "url" property has been removed, which my application uses extensively. Unfortunately, I have not been able to find the new, correct way to do this. The info I'm finding out there does only point to the removal of the url property, or suggests that I should move to Alloy (which at the moment is not doable for me as it would require a complete rewrite of the app). Can anyone point me to an example of the right way this should be done?
If you're not using Alloy, then it's really a two step process. First you need to get the handle to the window. That is usually done with Ti.UI.createWindow (see http://docs.appcelerator.com/platform/latest/#!/api/Titanium.UI-method-createWindow). Now that you have a reference to the window, you simply open it. So,
var win = Ti.UI.createWindow({title: 'My first window'});
win.open();
Documentation on the window object is here. http://docs.appcelerator.com/platform/latest/#!/api/Titanium.UI.Window
If you have windows defined in other js files. ie. myWindow.js, then you can use require to get the js window. Have the code in your window return a "Window" object, then open that.
ie. myWindow.js
var win = Ti.UI.createWindow({title: 'Window from another file'});
return win;
Then in your calling file, don't use url, require the window:
var myNewWindow = require('myWindow');
myNewWindow.open();
You can see information about calling require here: http://docs.appcelerator.com/platform/latest/#!/api/Global-method-require
Hope that helps.
Ray
I am new to GWT and trying to create a small application. I am currently assembling a small framework for the app, a generic layout handler, etc. This may not be the last problem I will bump into, but I just cannot find any solution to this on google.
So I have a class type, which return me Composites. Also, I have another one, which stores these kind of classes in Stack (I also tried Vector, I thought maybe GWT has issues with it). It didn't matter. If I call the .clear method on the Stack, I have the aforementioned error in the inspection menu of Chrome:
Uncaught TypeError: Cannot read property 'clear_31_g$' of undefined
Like if GWT does not know, how to convert this method to javascript or what? Do you know what is the problem here?
eclipse neon, Java 7 setting on Java SDK 1.8 (maybe this?), GWT 2.7.0 and App Engine 1.9.34
Thanks!
edit1: I also found the page, which contains the emulated JRE classes' list (http://www.gwtproject.org/doc/latest/RefJreEmulation.html) with all supported methods. Now I see, that clear is not on that list for Stack, but empty does and that gives me the same error. :-/
This error simply means that you try to call the clear() method on a null object (the object is undefined).
The error message itself is not as clear as it could be. First, it's not always about reading a property but also about calling a method. Second, remember that you run a compiled to javascript code and the property (or method) name may differ from the original one - it has something like _31_g$ added in the runtime.
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.
In an attempt to learn JS OOP I am viewing the source code of jQuery to better understand how they do things. My question may seem simple, but I'm having trouble understanding the reasoning behind several variables that jQuery has defined at the top of their library. The code is shown below.
(function( window, undefined ) {
// Use the correct document accordingly with window argument (sandbox)
var document = window.document,
navigator = window.navigator,
location = window.location;
....rest of code
What I don't understand is why they created variables for the document, navigator, and location objects. Does this resolve some type of browser bug? I don't understand the benefit of doing this.
They're doing that so that, if some other script has mistakenly (or intentionally) created variables called document, navigator, or location, they won't affect jQuery's use of those variables.
I have a javascript file that is normally used in a web browser using a script tag. It is a self-executing function that seems to put an object on the window (the window is passed in).
What would be the cleanest way to use it from node.js on the server?
Thanks,
Gareth
If all it does is add attributes to window, and you want to get those back out, you can create a global called window:
global.window = {};
require('theLibrary');
// now do something with global.window.theThingItAdded
However, if the library was written for the browser, it's possible that it still won't run because it wants to use the DOM. In that case, you might want to look into jsdom, which aims to give you a spec-compliant DOM inside Node.
(If you're using jsdom, I think that you would use it instead of the global.window bit above -- I think jsdom does that for you, but with a more full-featured window object. I haven't actually used jsdom, though, so I don't know for sure.)