JavaScript - Object 'exists' Function
I am tasked with making a function that can accept many different items and return whether or not they exist.
Currently I am struggling on seeing if an object that holds all the methods for a button exists. I thought I could pass in the string name of the object then do a $('buttonObject').length >0 in the 'Exists' method to see if it was created yet. However this will never return true, and I cannot pass in the object into my exists function without it being created yet (throws error). So something like Exists(buttonObject) won't work, I'll have to pass in the string name of it (Exists('buttonObject')) or something else (I'm new to JS). So how do I go about testing whether or not it exists if I cannot pass in buttonObject, without quotes? Similar TutorialsI have a script that I had written for me that works in IE (I guess its a ActiveX?) but I display it in the HTML page as the below HTML. I want to be able to tell in Javascript if this object is available in IE Code: <object id="IEPrinterChecker" classid="CLSID:506974F1-EA53-40C6-940A-4A1D47D5934B" width="0" height="0"></object> Listed below you can tell I can check if the plugin is available in Firefox without having to specify in the html the object. How can I do this in IE? Code: <script language="JavaScript"> var plugin; if ( window.ActiveXObject ) { // IE alert("How do I check for the plugin here?"); } else { var type = "application/printer-checker-component;version=1.0.0.1"; // your mimeType if (navigator && navigator.mimeTypes){ var mt = navigator.mimeTypes; for (var i = 0; i < mt.length; i++){ if (mt[i].type == type) { plugin = mt[i].enabledPlugin; if (plugin) break; } } } } if(plugin) { // insert object and/or test the plugin with script alert("plugin is available"); } else { alert("plugin not available or disabled"); } </script> Hi All, I'm trying to convert an anonymous function to a real function (nesting is getting out of hand), however the msg object becomes undefined after conversion. Here is the converted anonymous function which fails: https://gist.github.com/2587613 and here is the original anonymous function which works: https://gist.github.com/2587667 Any help would be greatly appriciated EDIT***** I'm nearly done with my script. I just lack one thing! I have 4 prompts asking a user for information. Basically all I want to do is.. If lastname, and first name are BOTH entered, seperate them with a comment like so : lastname,firstname but leave the comma out, if only one of the names are entered. How could I do this? <script type="text/javascript"> last=name=area=num=''; function info ( ) { last = prompt("Please enter your last name",''); name = prompt("Please enter your first name",''); area = prompt("Please enter your area code",''); num = prompt("Please enter your phone number",''); }; function Person (info) { this.name = name this.last = last this.area = area this.num = num } </script> <title></title> </head> <body> <script type="text/javascript"> info ( ); teacher = new Person(); tempString=''; document.writeln ( last ); document.writeln ( name) ; if(area==''||num==''){tempString='';} else{tempString = teacher.area +''+ teacher.num} document.writeln ( tempString); </script> Hi I need help with a function inside an object that is inserted into a function argument which is then used in a string that is returned. For a quick example which may help you understand. Code: function fn(o) { var text = '<a onclick="function(){'+o.func+'};">Click me!</a>'; document.write(text); } fn({ func: function() {alert('clicked');} }); The above code should display a link on the page saying 'Click me!' and once you click it, an alert box should pop up but it doesn't seem to be working, can anyone help please, thank you. Hello, I have a code: Code: var device = function(pos_X,pos_Y,serial){ this.pos_x = pos_X; this.pos_y = pos_Y; this.serial_number = serial; this.click = function(){ $("#Control").html("Menu"); }; this.draw = function() { $("#MainArena").append("<div style='position: absolute; top: "+this.pos_y+"px; left: "+this.pos_x+"px;' onclick='"+alert('click')+";'>device</div>"); }; this.move = function(newX, newY) { this.pos_x = newX; this.pos_y = newY; }; [...] <body> <div id="MainArena"></div><div id="Control"></div> devices = new device(100,100,1); devices.draw(); </body> The main problem is in "onclick" in <div> element. This script generates: <div id="1" style="position: absolute; top: 100px; left:100px;" onclick="undefined;">device</div> Hello was following a tutorial online from bucky. and created a new example with different variables and such. My code in my eyes looks absolutely fine but however it does not display can anyone spot the problem? [code] <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Untitled Document</title> <script type="text/javascript"> function cigerettes (brand, tar, price) { this.brand=brand; this.tar=tar; this.price=price; this.ReducedCost = discount; } function discount() { var reduction = 20; var finalCost = this.price - reduction; return finalCost; } var packet1 = new cigerettes("Mayfair", "2.0 mg", 399); var packet2 = new cigerettes("Sovereign", "3.4 mg", 425); </script> </head> <body> <script type="text/javascript"> document.write(packet1.brand() + " they contain " + packet1.tar() + "of tar " + "the packet cost me " + packet1.price() + ". With discount I got them for: " + packet1.ReducedCost()); </script> </body> </html> [code] Hello, I am attempting to implement a javascript joystick plugin. The project has a google code page located at: http://code.google.com/p/javascript-joystick/ After installing the plugin, the javascript works flawlessly in IE and FF, however chrome has some issues with executing it. A hosted version of what I am having trouble with can be found at http://www.jimmyblaze.net/joytest/simple.html The issue seems to be with a snippet of code from the main script that attempts to create an object and embed it on the page. The trouble area can be found he Code: try { ctrlFF.type = "application/x-vnd.numfum-joystick"; ctrlFF.width = 0; ctrlFF.height = 0; /* * Before accessing the plug-in's script interface it needs to be * added to the page. If the 'setDevice' call fails, the plug-in * is assumed to either not be installed or not working in this * browser, in which case it is removed in the catch. */ document.body.appendChild(ctrlFF, document.body); if (ctrlFF.setDevice(0) != null) { /* * As with the code for IE, any non-null value is a * success. */ return ctrlFF; } Chrome doesn't seem to support the .setDevice function, as when running the chrome developer tools inspect element tool, I can see it throw an exception at that line, and prompts the user to update java, because it thinks it is out of date. Has anyone run into this issue with chrome and know of a possible fix? Thanks a lot for your time, Legomaniac Hi, all~..According to ECMAScript, the root of the prototype chain is Object.Prototype. Each object has an internal property [[Prototype]] that could be another object or NULL.... However, it also says that every function has the Function prototype object: Function.Prototype, it confused me, because a function is an object, for a function object, what is its function prototype and object prototype..For example: Code: var x = function (n) {return n+1;}; what is the relationships of x, Object.Prototype and Function.Prototype Thx a lot !!!!!!!!! Hi, every time I try and alert: [ { number:0, secondnumber:0 }, { number:2, secondnumber:1 }, { number:1, secondnumber:2 } ] it just shows [object object], [object object], [object object]. Why is this and what can I do to make the record be shown as it is above in an alert? Thanks. I can't get any info from Firebug except that one line, uncaught exception [object Object]. The code fully worked, then I needed to make it dynamically create Sortables from the scriptaculous library based on how many X were in a table in my database, which I've done, and I'm thinking it may be a simple slight parse error of some type, I'm not too good with Javascript, because now my script barely works. I've double checked the script's source code, the PHP variables are exactly what they should be. Code: print<<<HERE Sortable.create('sortlist$box', { tag: 'img', overlap:'horizontal',constraint:false, containment: $list, dropOnEmpty: true, onChange: function(item) { var list = Sortable.options(item).element; if(changeEffect) changeEffect.cancel(); changeEffect = new Effect.Highlight('changeNotification', {restoreColor:"transparent" }); }, onDrop: function(item) { var thing=Sortable.options(item).element.identify(); var anchors = document.getElementById(thing).childNodes.length-2; if(anchors > 20){ alert('This box had 20 creatures in it already, your last action has not been saved.'); window.location.reload(); } else{ new Ajax.Request("saveImageOrder.php", { method: "post", parameters: { data: Sortable.serialize("sortlist$box") } }); } } }); HERE; $box++; } ?> }); </script> if you solve this I'll send ya $10 via paypal I created a method for displaying an object's properties: Code: renderfunction = false; function showProperty (object, property) { document.write ('<td class="type">' + (typeof object[property]) + '</td>' + '<td class="name">' + property + '</td>'); document.writeln('<td class="value">' + ( (typeof object[property] != 'function') ? object[property] :( (property != 'showProperties') ? ( renderfunction ? object[property]() : ('<span class="self">NOT RENDERED</span>') ) : ('<span class="self">THIS</span>') ) ) + '</td>'); document.writeln('<td class="hasOwnProperty" >' + ( object.hasOwnProperty(property) ? "Local" : "Inherited" ) + '</td>'); if (typeof object[property] == 'function') { document.writeln ('<td class="function">' + object[property] + '</td>'); } else { document.writeln ('<td class="function"> </td>'); } } As long as renderfunction = false, the object is fine coming out of this function. However, if I change renderfunction to true, all my properties become undefined. Why isn't this working as I expect it to? How should I fix it? Thanks in advance, -Brian. Ignore post (if mod, please delete)
Hi all, I'm stumped on finding a way in javascript to create an object factory whose instances are also object factories. In short I want something like that below, but no joy ... any clues? Code: function createClass () { return new createClass() function createClass() { return new createInstance () function createInstance () { //Default properties, values and methods which might later be extended } } } var createDoor = createClass(); var door1 = createDoor(); var door2 = createDoor(); var createChair = createClass(); var chair1 = createChair (); var chair2 = createChair (); Hello together! I generate html code with jsp. In that jsp there a several framesets and frames. And yes i know, frames are not really up to date but it's an old program and i have to deal with it now. Anyway, in the top frameset i have an onload attribute like onload="load()". In the function load i want to access the Element.prototype object. But unfortunately typeof Element gives me "undefined". So i looked a little deeper and found that window.toString() gives me "[object]" and not as expected "[object window]" so somehow my window doesn't know that its construcor is Window. window.construcor is "undefined" as well. And i don't have access to the Element object. I really don't know where the error could be. When the page is loaded and i access the same window over the console, then everything is right. But in my function a can't get access to the objects i need. I also don't know what part of the code could be useful to post here, but maybe someone had a similar problem before? i should say that this problem only occurs in IE8. In IE9 it works perfectly. Has anyone any idea?? I'm writing a program that involves a network of interconnected nodes (or simply objects in my example below). It depends on being able to access properties of an object's linked objects (a bit oddly worded, sorry)... Problem is I'm not sure how to properly access those properties... see below please. <script> //This is an example of a problem im having in my own code... //I want to access the name of the object within the links array wintin the object... var objA = {name: "Object A", links: [objB, objC]}; var objB = {name: "Object B", links: [objC, objD, objE]}; var objC = {name: "Object C", links: [objB]}; var objD = {name: "Object D", links: [objE]}; var objE = {name: "Object E", links: [objD]}; //ex: I want to access the name of Object A's first link... console.log(objA.links[0].name); </script> I'm hoping to get "Object B"... But instead I get: TypeError: Result of expression 'objA.links[0]' [undefined] is not an object. Is there another way around this? Any thoughts are appreciated. Hello. Is there any way to get the variable name of an object from inside the object? E.g. PHP Code: function Bla(){ this.write = function(){ document.write(<objectname>); } } obj1 = new Bla(); obj1.write(); //Outputs obj1 Here is my script: PHP Code: function myTimer(seconds, obj){ this.seconds = seconds; this.obj = obj; this.startTimer = function(){ if(this.seconds>0){ this.seconds--; this.obj.innerHTML = this.seconds; this.timer = setTimeout("Timer.start()",1000); } } } Timer = new Timer(10, obj); Timer.startTimer(); Now the problem is that the variable that contains the object must be named "Timer". This way I cannot create new timer objects with different variable names I have tried: this.timer = setTimeout("this.start()",1000); but it doesn't work. That's why it would be good to detect the variable name and instead use something like this: this.timer = setTimeout(varname+".start()",1000); I would rather not have to pass the variable name through a parameter like this: Timer1 = new Timer(10, obj, "Timer1"); Thanks in advance. Quote: menu: function( a, b ) { $( b ).observe( 'click', function( event ) { event.stop(); if( $( a ).visible() ) { $( a ).hide(); $( b ).removeClassName( 'selected' ); document.stopObserving( 'click' ); } else { $( a ).show(); $( b ).addClassName( 'selected' ); document.observe( 'click', function( e ) { if( e.target.id != a && e.target.id != b && !Element.descendantOf( e.target, $( a ) ) ) { $( a ).hide(); $( b ).removeClassName( 'selected' ); document.stopObserving( 'click' ); } }); } }); $$( '#' + b + ' > a' ).each( function( element ) { element.observe( 'click', function( event ) { $( a ).hide(); $( b ).removeClassName( 'selected' ); document.stopObserving( 'click' ); }); }); } This work's perfrect accept when i use it with others on the menu it leaves the other ones open, how do i get it to close the open one when i open a new menu.. Thanks. HI all. How do I determine whether a variable exists in a form. I have a form called "theForm". I want to determine whether the variable "THIRD_PARTY" exists in the form and if it has been set. Tracy Hello, through a javascript injection im trying to find out if a cookie exists on my computer. Here's the code I inject: Code: javascript: if (document.cookie.indexOf('_idp_session') != -1) alert('cookie is here'); else alert('cookie is not here'); and when visiting the options of my browser, I can locate the cookie, and i can see its there. Nevertheless my script keeps telling me its not there.. what am i doing wrong? Greetings, If I have a form like so: Code: <form> <input type="text" /> </form> How can I check if the input exists using DOM 0? Code: while (document.forms[0].elements[0].exists){ ...do something } I guess that I could assign a default value and check for something in "document.forms[0].elements[0].value" but that is not the approach I wish to take. I need to loop through all of the elements of a form until there are no more regardless of ID/name/value, am I thinking about this wrong? |