473,888 Members | 2,194 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 3568
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
1467
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
2145
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
3546
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
2416
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
4040
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
11173
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10772
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10434
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9593
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
7988
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
7143
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
5810
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
6011
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4239
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.