473,770 Members | 1,948 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

XMLHttpRequest to fetch a specific div

Is it possible to fetch a specific div from a source html document
using XMLHttpRequest, rather than fetching the entire file?

I was going to use Ajax on mouseover but keep the normal link intact,
so that if the user does not have javascript they can click the link
as normal and this would take them to the new page, but if they have
javascript (and ActiveX enabled I guess, in the case of IE) it would
just fetch the appropriate div into the current page. This would save
having to duplicate content so that it can be served by Ajax.

Of course, changing content on mouseover could annoy many users... is
there a way to change a link location using DOM on page load, as you
might a class name?

eg an onload function changes <li id="product1">< a
href="product1. html".... to <li id="product1">< a href="#", so that the
XMLHttpRequest can be moved to an onlick event--but if there is no
javascript present, the hard link would remain intact.

thanks for any pointers!

Feb 20 '07 #1
17 3549
br*******@gmail .com said the following on 2/20/2007 6:03 AM:
Is it possible to fetch a specific div from a source html document
using XMLHttpRequest, rather than fetching the entire file?
No.
I was going to use Ajax on mouseover but keep the normal link intact,
so that if the user does not have javascript they can click the link
as normal and this would take them to the new page, but if they have
javascript (and ActiveX enabled I guess, in the case of IE) it would
just fetch the appropriate div into the current page. This would save
having to duplicate content so that it can be served by Ajax.
I love it when people want to use "Ajax" so they can say "we use Ajax".
Of course, changing content on mouseover could annoy many users... is
there a way to change a link location using DOM on page load, as you
might a class name?
Yes, but why?
eg an onload function changes <li id="product1">< a
href="product1. html".... to <li id="product1">< a href="#", so that the
XMLHttpRequest can be moved to an onlick event--but if there is no
javascript present, the hard link would remain intact.
That is one crazy idea you have there. Why not:

<a href="product1. html" onclick="someAJ AXFunction();re turn
false">Product</a>

End of problem.

--
Randy
Chance Favors The Prepared Mind
comp.lang.javas cript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Feb 20 '07 #2
modified some existing code... think it's nearly there, but where am I
falling down?

var XMLHttpRequestO bject = false;

if (window.XMLHttp Request) {
XMLHttpRequestO bject = new XMLHttpRequest( );
} else if (window.ActiveX Object) {
XMLHttpRequestO bject = new ActiveXObject(" Microsoft.XMLHT TP");
}

function getData(fetchdi v, targetdiv)
{
if(XMLHttpReque stObject) {
var obj = document.getEle mentById(target div);
XMLHttpRequestO bject.open("GET ", fetchdiv);
XMLHttpRequestO bject.onreadyst atechange = function()
{
if (XMLHttpRequest Object.readySta te == 4 &&
XMLHttpRequestO bject.status == 200) {
obj.innerHTML =
XMLHttpRequestO bject.responseX ML.getElementBy Id('div2')

}
}

XMLHttpRequestO bject.send(null );
}
}

Feb 20 '07 #3
On 20 Feb, 06:10, Randy Webb <HikksNotAtH... @aol.comwrote:
brough...@gmail .com said the following on 2/20/2007 6:03 AM:
Is it possible to fetch a specific div from a source html document
using XMLHttpRequest, rather than fetching the entire file?

No.
It appears to be possible to process an XML file fetched using
XMLHttpRequest and extract specific data, so I think this should be
able to be applied to a html document in order to extract the contents
of one div.

That is one crazy idea you have there. Why not:

<a href="product1. html" onclick="someAJ AXFunction();re turn
false">Product</a>

End of problem.
Ah ha! So it doesn't follow the link.

How do you add return false to mouse events that are in an external
script? eg:

var nav = document.getEle mentById('test' ).getElementsBy TagName('a');
for (var i=0;i<nav.lengt h;i++)
{
nav[i].onmousedown = someAJAXFunctio n;
}

I've had a play but "someAJAXFuncti on;return false" doesn't seem to
work.

Feb 20 '07 #4
I love it when people want to use "Ajax" so they can say "we use Ajax".
Ajax is dreamy.
Feb 20 '07 #5
OK I don't think it's convenient to do this using responseXML as the
xhtml document would need to be marked up with the XML namespace and
wouldn't validate (?) as well as a host of other complications.

a better solution from an old thread on this subject:

On 22 Jun 2002, 08:30, "Joe Fawcett" <j...@rightway. co.ukwrote:
1) Parse the responseText property using traditional string processing
techniques, regular expressions etc. to retrieve the text in the div with id
equal to 'Descriptions'
this should be very simple to do... can anyone help me out? a html
page fetched using responseText displays correctly, so this technique
should work?

Feb 20 '07 #6
<br*******@gmai l.comwrote:
Is it possible to fetch a specific div from a source html document
using XMLHttpRequest, rather than fetching the entire file?
either using xslt or innerHTML...
--
Une Bévue
Feb 20 '07 #7
On Feb 20, 6:43 pm, unbewusst.s...@ google.com.inva lid (Une Bévue)
wrote:
<brough...@gmai l.comwrote:
Is it possible to fetch a specific div from a source html document
using XMLHttpRequest, rather than fetching the entire file?

either using xslt or innerHTML...
--
Une Bévue
depending on whether you and your users have too much bandwidth, or
you have spare server CPU, you could use XHR to bring back a page and
use jQuery or some such to parse through it using javascript in the
client for a specific section of the xmlDOM, or you could write a
proxy script using python or php (the simpleXML functions are very
useful) which would take the request and send back only the content
reuired.
Both ways would mean you wouldn't have to duplicate content. The 2nd
method would be extrememly fast and would not require that your users
had spare CPU and fast internet.

li id="product1">< a
href="product1. html".... to <li id="product1">< a href="#", so that the
XMLHttpRequest can be moved to an onlick event--but if there is no
javascript present, the hard link would remain intact.

Your method of actually changing the href is not needed, as Randy
showed, however his method of adding extra markup in the html is also
not the best way out, instead look into using one of the many
libraries out there which use reg expressions to parse your markup and
attach an onclick dynamically to any given tag, class, or id. These
classes are fast, and are unobtrusive javascript, ie, javascript that
is only present in the DOM if the user agent requests it. Adding extra
markup makes it harder to maintain your xhtml, and causes
accessibility validation issues, and leaves you less time to be polite
to others.

Feb 20 '07 #8
On 20 Feb, 13:08, "shimmyshac k" <matt.fa...@gma il.comwrote:

Your method of actually changing the href is not needed, as Randy
showed, however his method of adding extra markup in the html is also
not the best way out, instead look into using one of the many
libraries out there which use reg expressions to parse your markup and
attach an onclick dynamically to any given tag, class, or id. These
classes are fast, and are unobtrusive javascript, ie, javascript that
is only present in the DOM if the user agent requests it. Adding extra
markup makes it harder to maintain your xhtml, and causes
accessibility validation issues, and leaves you less time to be polite
to others.

Thanks for all the info, little out of my depth but I'm looking into
it.

as for adding onclicks dynamically, is
{
var nav = document.getEle mentById('conta ctform')
nav.onclick = openform;
}

(which doesn't seem to work with .getElementsByT agName('a') appended?)

preferable to:
var nav =
document.getEle mentById('conta ctform').getEle mentsByTagName( 'a');
for (var i=0;i<nav.lengt h;i++)
{
nav[i].onmousedown = openform;
}

or is there no real difference? Does the latter consume more cpu due
to the loop?

excuse all the thick questions.

Feb 20 '07 #9
Une Bévue said the following on 2/20/2007 1:43 PM:
<br*******@gmai l.comwrote:
>Is it possible to fetch a specific div from a source html document
using XMLHttpRequest, rather than fetching the entire file?

either using xslt or innerHTML...
Neither of which can retrieve a specific div from a source html document
without retrieving the entire document.

--
Randy
Chance Favors The Prepared Mind
comp.lang.javas cript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Feb 21 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
1462
by: Unknown User | last post by:
I am trying to implement XMLHttpRequest to a new website, but when I include HTML, the code appears as is, instead of the formated HTML. Please have a look and click the 1st link ("L'Association") on top (yello horizontal bar on top): http://www.auriance.com/docs/alcan/index.php How can I fix it? Thanks, -- Using Opera's revolutionary e-mail client: http://www.opera.com/mail/ Discover Opera: http://members.surfeu.fi/jerkku/
11
2136
by: K | last post by:
Hey Guys, The problem i'm having is that our company is provided with a xml link. My job is to create a javascript menu based on the information retrieved from that link. We/I do not have access to MainServer1 as it belongs to another company whoe provides us with the data. The issue here is that the xml link and the XMLHttpRequest will not/dont sit on the same server/domain. MainServer1 ---- Server2 ---- Client
7
3537
by: Bernard Lebel | last post by:
Hello, I'm stumbled at a serious problem, and quite frankly getting desparate. This is a rather long-winded one so I'll try to get straight to the point. I have this Python program, that performs MySQL queries to a database. These queries are performed at regular intervals. Basically it is looking for fields that match certain criterias. Such fields are not present at all time. When the program finds such field, it then takes
1
896
by: vikas.khengare | last post by:
Hi Friends.... I have AJAX code which giving "Permission denied to call method XMLHttpRequest.open" error. This error fired by FireFox 1.0 and IE 6 and with Tomacat 5.x. This code work very correctly on the other machine and giving correct output also. I am trying to fetch "html" pages from others server.
5
2408
by: Peter Michaux | last post by:
Hi, The FAQ correctly says the following: "Mozilla (NN6.2+, Firefox, Ice Weasle etc), Opera 7.6+, Safari1.2+, the Windows version of IE versions 5+, and some other browsers provide the XML HTTP Request object." In my haze of testing yesterday it seems that NN6.1 provides an non-functional XMLHttpRequest object and NN6.2 XMLHttpRequest object
1
4033
by: geevaa | last post by:
http://www.phpbuilder.com/columns/kassemi20050606.php3 XMLHttpRequest and AJAX for PHP programmers James Kassemi Introduction: Although the concept isn't entirely new, XMLHttpRequest technology is implemented on more sites now than ever. Compatibility is no longer an issue (IE, Mozilla and Opera all support it), and the benefits to using it are amazing. There are too many PHP programmers avoiding any
0
9618
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9454
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10038
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8933
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7456
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6710
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5354
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5482
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4007
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.