Objects
Aug 3, 2005I am trying to figure out how to use objects properties in my code so
when the page loads All my properties are in object where I can use
elsewhere in my page. Code:
I am trying to figure out how to use objects properties in my code so
when the page loads All my properties are in object where I can use
elsewhere in my page. Code:
Is there a way in Javascript or Jquery to return an array of all objects underneath a certain point, ie. the mouse position. Basically, I have a series of images which link to various web pages but I have a large semi transparent image positioned over the top of the other images. I want to find the href of the background image that the mouse pointer clicks over.
View 1 Replies View RelatedIs there a better way to extend object with internal objects?
$.fn.bestShow = function(s) {
var d = {
width: 0,
height: 0,
order: "numeric",
orderBy: "",
[Code]...
I'm trying to use the following code to load xml files:
ImportXML = function (ts) {
this.file = ts;
if (document.implementation &&
document.implementation.createDocument) {
this.doc = document.implementation.createDocument("", "", null);
this.doc.obj = this;
this.doc.onload = this.callBack;
this.doc.load(this.file);
} else if (window.ActiveXObject) {
this.doc = new ActiveXObject("Microsoft.XMLDOM");
this.doc.onreadystatechange = this.ready;
this.doc.obj = this;
this.doc.load(this.file);
} else {
alert("Error");
}
}
ImportXML.prototype.ready = function () {
if (myDoc[counter].readyState == 4) this.obj.callBack();
}
ImportXML.prototype.callBack = function () {
alert('loaded');
}
var xmlDoc = new ImportXML("bar.xml");
The problem I'm having is with the second line that reads
this.doc.obj = this;
IE tells me that object doesn't support this property or method. How is
it possible to get a reference to the object (xmlDoc) in the prototypes
'ready' and 'callBack" with IE?
Where is the list of js objects (Anchor, Applet, document, etc)? It's not in the Netscape Guide or Core documents.
View 2 Replies View RelatedIf you have a javascript interpreter running in an environment unknown
to you (as in what objects it exposes to the language) is it possible
to loop through all objects in some way?
This is a non-browser environment, specifically the scripting engine
for doing xslt extensions in msxml.
I have the following code:
var img = new Image();
img.myProperty = 'something';
I've tried Image.prototype.myProperty and a few other things. It seems IE just doesn't want me extending the Image object.
"When we combine FUNCTIONS with OBJECTS we get METHODS". Then he creates an empty ARRAY:
var a = [];
then he uses the "push() method" to add elements to the array.
a.push(1,2,3);
uh, methods are for *objects* right? Yet he is using them on an ARRAY.how an array can magically becomes an object that is manipulated by a "method"?I mean, the array is still an array, no? It never actually becomes an object, right? Yet we still use a *method* to manipulate it. See my conceptual quandry?
I've often write javascripts that use this rather common code to get all tags in an XHTML document:
var alltags = document.getElementsByTagName('*')
... and then use a for loop to access the elements as an array, for example:
for (i=0;i<alltags.length;i++) {
elementClass=alltags.className
[i]Do stuff
}
This has always seemed to work in the past, but I recently learned that the getElementsByTagName method returns a DOM NodeList, not an array. I'm currenly working on a project that needs to access the children of an element conditionally upon its class.
I have three questions. First: How do I declare a global variable to be a DOM NodeList object? It wouldn't be assigned until called from a function, so something akin to var elementList = document.getElementsByTagName() in my global declarations is out of the question. Is there something like var elementList = new NodeList() in javascript? (I know that I can assign it in a function without the var and it will be global, but other people may have to work with this code, and I'd like to have it clearly declared at the head of the program.)
Second question: Assuming alltags is a NodeList returned from a getElementsByTagName call, which of these is proper? This:
childElements = alltags[i].getChildNodes()
... or this:
childElements = alltags.item(i).getChildNodes()
And finally, which of the above techniques has better browser support?
I recently had a problem where I needed to build up an array of strings, that would be join()ed into a string when the array building was complete. however, each value could only be in the array once.
This was a problem for a few reasons. Once a value has gone into an array the only way to check for it that works cross-platform is to scan the array looking for the value. FireFox has the every() and some() functions but they don't work in anything else.
Using an object to simulate an assocaiative array would allow me to avoid this problem by storing key/values with the keys having the same value as the value I was storing. I could then use the (a in b) construct to check that I had not already added a value.
However, array type methods won't work with objects, so I had no access to size () or join () meaning I'd have to manually build the string by iterating over the object.
My solution was to use an array object, but to store the provided data i nboth the array proper and as a property of the array object.
var myArray = new Array;
function addVal (val)
{
if (!(val in myArray))
{
myArray [val] = 1;
myArray.push (val);
}
}
addVal ('one');
addVal ('two');
addVal ('three');
addVal ('one');
addVal ('two');
addVal ('three');
console.log (myArray.length);
console.log (myArray.join (', '));
This approach does use up more memory but it does give me the advantages of both arrays and objects for little extra work. (if you don't have FireBug then replace console.log with alert)
I know if I use this code:
var arrTD = document.getElementByType('td')
That arrTD will have an array of "HTML DOM TableData Objects".
Now what is the best way to access each object and see what it contains. I would like see which properties each object has and what their values are. I also guess there could be some other objects within the TD object and maybe even some events and methods.
Now I have been reading an on-line reference about DOM, but I think it is a little dated.
http://www.w3schools.com/htmldom/default.asp
For example I know that a <TD> can contain class="something" but I could not find a property called class, but it did list others like id, align etc.
I would like to learn how to access and view the contents of a DOM object.
I would like to have page which uses a few xhr objects and automatically (after 30 seconds) starts the same few requests again It works for me well in IE7, IE8, but not IE6 (where is in browsing history chosen option "automatically" ). It works there only for first time and I can't set it for circular reloading. It looks like page is reloaded, but xhr objects are not doing their job ...only - as I wrote - for first time after page load.
I have tried a lot of things:
- headers: no-caching
- window.reload(true)
- window.setInterval
- window.location.assign
I tried to set an onclick event with:
node_a.setAttribute("onClick","visibility_on_off_by_el('"+node_b+"');");
where node_b is a pointer to a DOM object: node_b = document.getElementById("a") The reason I passed a DOM object into a variable, was to avoid searches document.getElementById() each time. However, it will not work. I am confused I cannot figure out if it a a mistake of mine or just the way javascript works.
Could anyone explain why I cannot use variables as DOM objects in order to avoit each time a tree search document.getElementById("a")? Bellow is given the code with explanation when it works and when it fails.
[Code]...
Does anyone know of any generic code to clone an object (which only contains properties), which copies by value, not just reference?
It seems as though there should be some sort of prefab recursive function that could work for any old object, so I didn't want to re-invent the wheel Code:
I am dabbling with objects and have successfully created an object with various properties, one of which is an Array, and all is fine. the Question I have is can I make an Array of objects? I have the following object:
dataSeries.Type = value
dataSeries.Name = value
dataSeries.dataPoints[n] = Array of values
dataSeries.color = value
What I would like to do is have an Array of multiple objects supposedly like:
dataSeries[0].Type = value
dataSeries[1].Name = value
dataSeries[2].dataPoints[n] = Array of values
dataSeries[3].color = value
Is this possible?
I've tried breaking out an application I'm working on into smaller objects. I have always had the objects built out, but they were nothing more than local variables. So I tried to create a few methods and include them in one of the objects (the Map object). Code:
View 5 Replies View RelatedI have a data structure that is composed of an array of JavaScript objects (my own created classes) that I need to pass as a parameter to an URL:
I would like to know if there is a way to serialize this array, send it as a string, then deserialize it on the other side, resulting with the data structure again I am trying to avoid doing the serialization myself.
Each entry in the array holds an object of type Person:
function Person(name, age, children) {
this.name = name;
this.age = age;
this.children = children;
}
where children is an array of Person objects
Is this sort of thing possible?
Can anybody explain to me what it means when a Date object method "operates" under UTC? I know that local time and Universal Time Coordinated (GMT) are different times, but what does that have to do with using the Date methods?
For example, let's say that I create a new Date object like this:
var lovelyDate = new Date(2006, 3, 30);
What then, would be the difference in using the methods
lovelyDate.getDate()
lovelyDate.getUTCDate()
?
Shouldn't they both return 30?
Can someone explain to me the difference in the book and the sample code? I am not an expert in javascript, but I am an experienced programmer in C++ (Borland) and other programming languages (mainly CVI which does not support OOP).
The book uses try and catch statements to init the XMLHTTPRequest object, but the sample code uses what looks like (to me) an array of functions. Can someone please explain the sample code? It is probably straightforward to a javascript programmer, and I can see what is happeneing, but I have never seen the square brackets construct before. Code:
In the method nextImage, I can't figure out how to access thumbs. It keeps coming back as undefined. (Using Firefox)
function runPortal(portal_number){
// there are multiple runPortals on each webpage
this.portal = document.getElementById('portal'+portal_number); // represents the div that holds the images
this.thumbs = this.portal.getElementsByTagName('a').length; // represents all the images within the div that will be rotated
this.length = this.thumbs.length; // that's how many images will be rotated
// Hide everything
for (var j=0;j<this.thumbs.length;j++){
if (j==0) continue; // Don't hide the first one
this.thumbs[j].childNodes[0].style.display = 'none'
}
this.nextImage = function (){
// there are a fixed number of images to rotate. Start over
if (this.i >= this.length){
this.i = 0;
}
// One fades away, the next appears
Effect.dglPuff(this.thumbs[this.last].childNodes[0], {duration:.6, from:.7});
Effect.Appear(this.thumbs[this.i].childNodes[0]);
// iterate to the next image for the next run
this.last = this.i;
this.i++;
}
// Set up the image rotator
// here is where I started guessing
// thumbs needs to belong to the object rotator, I guess.
this.rotator = new PeriodicalExecuter(this.nextImage, 4); // This object runs the function every 4 seconds
this.rotator.portal = document.getElementById('portal'+portal_number); // represents the div that holds the images
this.rotator.thumbs = this.rotator.portal.getElementsByTagName('a'); // represents all the images within the div that will be rotated
this.rotator.length=this.length; // that's how many images will be rotated
this.rotator.i=0; // the counter for what image we're one
this.rotator.last=0; // the counter for the previous image
}
I am currently working on building a library and having my functions(methods) within my library. I am having trouble find some code for JavaScript that can help me to do the following:
I need to give an array of objects and the name of a key, return the array sorted by the value of that key in each of the objects: "a" + [{a:2},{a:3},{a:1}] → [{a:1},{a:2},{a:3}]
I am trying to a onMouseover to the change the background of <td>s when
the mouse is on, the problem is that does not work when we put the
content of these <td>s inside another table thanks to innerHtml.....
I recently found out that the below code works in FireFox 1.0.4.
str = "<b>Hello world!!!</b>";
var doc = new DOMParser().parseFromString(str, "text/xml");
document.getElementById("div1").appendChild(doc.documentElement);
I knew how to manipulate document object. Now, I see that there is this
DOMParser object that one can use in the browser in Javascript.
What other objects are accessible in FireFox?
I went to www.w3.org and searched for DOMParser and did not find
parseFromString as one of its method.
Where can I find about DOMParser and objects available in the browser.
Given the following in the <body> of an HTML page:
<form name="test" action="action">
<input name="test1" value="abc">
<script type="text/javascript">
alert(document.forms['test'].elements['test1'].value);
</script>
I have two questions:
1. Is there anything in the specs/standards that says the <form> and <input>
objects must be available for reference in the DOM as soon as they are
parsed in the document, so that the script will succeed? I couldn't find
anything.
2. If nothing is in the specs/standards, then does anyone know of any
browsers which would not make the objects available for script immediately?
I'm wondering if, for example, a browser would only make the DOM available
to script once it is fully loaded and parsed?
I basically want to be able to store and retrieve a tree structure in
greasemonkey. Unfortunately the GM_setValue and GM_getValue functions
only take string key value pairs. Is there a native javascript function
that will serialize the object so that it can be stored and retrieved
as strings?
I'm using a simple show function, called from a button.
function show(rowId) {
var hideRow = "View_" + rowId
document.getElementById(showRow).style.display ="block";
}
QUESTION: I'm using xsl and can have more than one table row with the
specific rowId, and I want ALL of them to show when the button is
clicked for that rowId. But although the html source is there, I only
get the first row with the rowId.