"prototype.js" & ResponseXML
Feb 2, 2006Doesn't "prototype.js" support the "responseXML" property of the XMLHttpRequest object?
View 2 RepliesDoesn't "prototype.js" support the "responseXML" property of the XMLHttpRequest object?
View 2 RepliesAccording 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:
var x = function (n) {return n+1;};
what is the relationships of x, Object.Prototype and Function.Prototype
I am trying to get to the bottom of javascript object, prototypes etc. I have a fairly good grasp of it, but I get confused the closer I get to the base object.prototype. FIrst of all, I was under the impression that all objects descend directly from Object. But some objects (like Array) seem to inherit properties and methods from the function.prototype. So does this mean that the chain is like this:
object -- function -- array Second, I noticed (on the mozilla javascript reference site that object.prototype inherits properties and methods from function.prototype and vice versa!? How can this be? I must be missing something important about understanding the chain?
In processing the response from an AJAX call, assume the returned xml string as:
"<?xml version="1.0" encoding="UTF-8"?>
<options>
<option text="new search..." value="search..."/>[code].....
Now, in the javascript:
1)If I use ajaxRequest.responseXML, I can directly use it as DOM document, such as: ajaxRequest.responseXML.getElemetnByTagName("option");
2) Or, I can use another way(assume on Firefox):
var parser=new DOMParser();
doc=parser.parseFromString(ajaxRequest.responseText,"text/xml");
doc.getElemetnByTagName("option");
My questions are.Since I can use ajaxRequest.responseXML.getElemetnByTagName("option") directly, I do not need DOMParser to convert.On the other hand, if the ajaxRequest.responseText is NOT of the XML format, the parser.parseFromString will not work any way.Then, why (under what circumstance) should one use DOMParser?
I'm having trouble figuring out what's going on with IE6's
Msxml2.XMLHTTP object. I have two feed addresses in this stripped down
version of my code below. Both work fine in Firefox (using the
XMLHttpRequest object), but only the thinkgeek one works in IE. In the
processFeed function, it shows the problem - the first alert shows 0
for the wikihow feed in IE, though it can still display the
responseText. Any insight? Code:
I'm trying to create a function that will return the value of responseXML so I can assign a variable to it.
Code:
ajax.downloadXml = function (url)
{
var XMLHttpRequestObject = false;
[code]....
Stepping through the function, the onreadystatechange seems to never be executed, so nothing is returned. There's probably just a fundamental I'm messing up on.
I'm not getting any response for the following ajax request. I'm getting the following error when i try to run request_get_xml.html im mozilla firefox:
XML Parsing Error: not well-formed Location: moz-nullprincipal:{d4e6a0fb-93c0-4ef4-82cf-7ccbd5c1e02e} Line Number 4, Column 2:
-<CATALOG>
request_get_xml.html
<script type="text/javascript">
function loadXMLDoc()[code].....
I am trying to build a very simple Ajax example with JavaScript and PHP. Basically, the goal is to populate a select list with values dependent on the selection in another list. I am able to do a GET from a server-side script and I see the XML displayed correctly in XMLHttpRequest.responseText. However, when I try to check XMLHttpRequest.responseXML, I am not able to get anything out of it. The alert() shows it as an "Object", so it's not null. I have tried getElementByTagName() from both responseXML and responseXML.documentElement, each time giving a legitimate inner tag, but nothing comes out. What is the correct way of doing it? This is my JavaScript and HTML code:
[Code]...
I tried POSTing from XMLHttpRequest, i can get the XML right on server
but responseXML from server is coming null. I can see the XML right in
responseText. but responseXML is null. responseText to DOM conversion
also fails while the XML in responseText seems valid ..
-- here is the javascript code for sending ---
I have several functions with code along the lines of:
var xmlDoc = requestXML("ajax.asp?SP=SelectRelatedTags&tag=" +
array[i]);
The requestXML() function includes the code:
var xmlDoc = null;
http_request.onreadystatechange = function() {
if (http_request.readyState == 4) {
if (http_request.status == 200) {
xmlDoc = http_request.responseXML;
} else {
alert('There was a problem with the request.' +
http_request.status);
}}};
http_request.open('GET', url, true);
http_request.send(null);
return xmlDoc;
However, the last line (the return) executes before the readyState
reaches 4. How do I return the xmlDoc to the functions only once the
xmlDoc has been assigned? I tried putting the return statement in a
while loop with the condition that the readyState must = 4 - this
worked, but makes the browser popup a message saying the script is
slowing down the system.
The situation is; I receive a response back from the server which i only a part of html code. e.g. a table like <table <tr><td>high</td></tr>........ </table>. In other words, I receive
file as a response which has a part of html file.
My aim is to replace an existing document node with the new response.
If I run the following lines (JavaScript):
var parsedText = document.createTextNode(req.responseText);
body.replaceChild(parsedText, toReplace)
It replaces the node 'toReplace' with html codes with the tags include which is not what I want. The html tags are not wanted but should display in correct format as specified by html (a tabular form if htm tag is <table>)
If I try:
My code gives me an error only the first time when i press the checkbox. If i press it for the second time or just to uncheck the checkbox and it gives me the right answer.I use IE7, xampp with appache server and Windows7.
Message: 'null' is null or not an object
Line: 76
Char: 3
Code: 0
<!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">
<body>[code]............
Im developing a JSP site... my user information validation page is not working and its giving that responseXML object is null...Im not using any XML file to fill the data here.... all the messages custom messages are loaded from the JSP page....My Valildation.js file as below.....
// holds an instance of XMLHttpRequest
var xmlHttp = createXmlHttpRequestObject();
// holds the remote server address[code].....
I have tried debugging using the alert function and have come to the conclusion that the responseXML property keeps returning null even though I have set everything correctly.
Here is the source code:
HTML Code:
I can use "with" like this:
function MyObject(message)
{
this.message = message;
}
function _MyObject_speak()
{
alert(this.message);
}
with (MyObject)
{
prototype.speak = _MyObject_speak;
}
I was wondering why I can't use "with" like this:
with (MyObject.prototype)
{
speak = _MyObject_speak;
}
One of the complaints about prototype.js (google for it if you're not
familiar with it) is that it's poorly documented. I have this inkling
that the key to understanding prototype.js is in the bind function.
The problem with Javascript is that the "this" operator is poorly
overloaded and it is often hard to understand in the context of
object-oriented javascript
So, let's start with the definition:
Function.prototype.bind = function(object) {
var method = this;
return function() {
method.apply(object, arguments);
}
}
As I read this, it states that all functions (which are themselves
objects) will, in the future, have an associated method called "bind".
The function() function, so to speak, simply instantiates a Function
object with the parameter list and then evals the statement, sticking
the resulting execution-tree in the current code frame.
The "this" there refers to the function object associated with the call
to bind(), right? But the word "arguments" there refers to the
arguments passed to the function object *generated by* the call to
bind().
In every example within prototype.js, bind() is called either in a
constructor or within a method contexted to a javascript object, and is
always called with "this" as its argument, e.g.:
this.options.onComplete = this.updateContent.bind(this);
As I read the code it seems to be stating that the "this" object
referred to within bind()'d functions are being coerced into always
referring to the current instantiated object.
But isn't this always the case anyway? Is this a rather confusing
attempt to ensure "this" purity whereby the call
method.apply(object, arguments)
is forced to always have the reference to the containing object present?
I think I've got it. Bind() generates uniq functions that contain live
references to the objects to which they belong, such that the function
object can then be passed to setTimeout() or onMouseOver(), handlers
that accept functions but not objects.
Lets say we run: window.alert = function() { };
Is there anyway to 'restore' the original alert() method or is it gone
forever?
I know you can do window.alert = Window.prototype.alert, but lets say
you also set Window.prototype.alert = function() { } or lets say we're
in Opera, which doesnt have a Window "class".
I would like to set up an event observer outside of an object, so I
can't use this.bindAsEventListener. How can I pass the correct object
reference?
I tried something like this, and various other variations, but no luck.
This works when I set it up from inside the object, using "this.",
Event.observe(targetId,'click',targetId.select.bin dAsEventListener(this),false);
I wanted to add an object as a prototype to separate my methods more
nicely, however, I ran into a couple of problems. Apart from the
obvious "scope" issues I found that any instances of my class shared
the objects methods and properties.
I realise (now) that this is actually how prototypes work, they share
functions and objects rather than create new instances of them for
every "class", but is there any way around it? (or shouldn't I be doing
things like this at all?) Code:
I am working on my own pop up calendar, mainly because the one I am currently using crashes the Safari browser at times.
So, I want to verify that what I am doing will work, in that I want to be able to have multiple calendars open at the same time, each independent of the other.
So, I start it off with:
var Calendar = {
dateSelected: null,
topPos:null,
leftPos:null,
somefunction:function(e) {
...
}
};
If I create more than one calendar object, will they have their own variables, in that the dateSelected, topPos and leftPos will be unique to that instance?
Or, is there a better way to do this, that is cross-platform.....
In my research in the javascript language I have encountered problems
with implementing prototype inheritance while preserving private
methods functioning properly. Here is an example: Code:
I want ask you if, for a web portal/application, is better prototype or Jquery? I don't want to innesc some type of flame, but after the announce that drupal use JQuery and that the new Wordpress
2.2 use Jquery I ask myself if my choice of use prototype.js is the bettere choice.
I have <div id ="changeable"> with some html in it. I have a link that calls the function to replace the info in the div. The problem is that the "creative_development.inc" file is added to the top of the div and does not replace the content. How do I replace the content, and not just add content?
View 1 Replies View RelatedI'm trying to make an addEvent function that will automatically attach itself to the object using a class.
My question is how can I add the function so when I write obj.addEvent("click",myfunction); it will add the event?
Here's my current function:
this.prototype.addEvent = function(type,fn)
{
if(window.attachEvent)
this.attachEvent("on"+type,fn);
else if(window.addEventListener)
this.addEventListener(type,fn,false);
}
I need help to workaround the following problem:
First, about the environment:
- The web pages automatically generated.
- Pages can be inserted to each other
- When a subpage to be inserted is being generated, it does not know will it be inserted or not.
- That because each subpage uses the <script src=...> tags to load the script it needs.
- in the external script files I'm trying to protect it from repeated execution checking (if(){}) the value of a variable which is created after in the script.
Everything worked fine until I tried to use the .prototype to declare the method of my objects.
In the Internet Explorer, I got "Object does not support this property or method", on the access to a prototype function, because the prototype of my object was deleted by itself after the second load of the external script. Code:
This function will return an array of the elements in a page that contain a certain attribute, you can also give it a value that the attribute has to match, a tag name that the element has to match and a parent element.
I know there are other functions for doing this, but this one is written for use with the prototype (http://prototype.conio.net/) JavaScript library, in fact its really just a modified version of the getElementsByClassName() function that’s part of prototype.
document.getElementsByAttribute = function(attribute, value, tagName, parentElement) {
var children = ($(parentElement) || document.body).getElementsByTagName((tagName || '*'));
return $A(children).inject([], function(elements, child) {
var attributeValue = child.getAttribute(attribute);
if(attributeValue != null) {
if(!value || attributeValue == value) {
elements.push(child);
}
}
return elements;
});
}
Usage is pretty simple, this will return all elements with a width attribute: