?
Solved

javascript code for document.evaluate (xpath) in IE

Posted on 2012-12-24
9
Medium Priority
?
818 Views
Last Modified: 2012-12-25
Hello,
would you tell me how to write this code to support also IE?
if (!document.evaluate) {
  alert("Sorry, cannot do that. Your browser does not support the evaluate method!");
  return null;
}
var a_el = document.evaluate(".//tr/td/a[contains(@id, 'foo')]", table_node, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;

Open in new window

Thanks, Aryeh.
0
Comment
Question by:tuchfeld
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
9 Comments
 
LVL 22

Expert Comment

by:Mrunal
ID: 38719607
Hi Aryeh,

I think IE does not support this property. My suggestion is go with jQuery.

Check this out:

http://stackoverflow.com/questions/4681968/document-evaluate-cross-browser
0
 
LVL 11

Accepted Solution

by:
mcnute earned 2000 total points
ID: 38719794
To use jquery is your best option:

$('table tr td a').each(function(){
   if ( $(this).attr('id').match(/foo/g) ) {
   //  do something with it
   }
});

Open in new window

0
 

Author Comment

by:tuchfeld
ID: 38720077
Mcnute, can you tell me.
in your jquery code, where do I use the table_node variable?
(this is the ONLY table in the document I want to search in).
Thanks.
0
What is a Denial of Service (DoS)?

A DoS is a malicious attempt to prevent the normal operation of a computer system. You may frequently see the terms 'DDoS' (Distributed Denial of Service) and 'DoS' used interchangeably, but there are some subtle differences.

 
LVL 11

Assisted Solution

by:mcnute
mcnute earned 2000 total points
ID: 38720189
if it is the only table in the page, then my code snippet will do. Otherwise give your specific table an id.

var table = $('#id_of_the_table').find('a');

table.each(function(){
   if ( $(this).attr('id').match(/foo/g) ) {
   //  do something with it
   }
});

Open in new window

0
 

Author Comment

by:tuchfeld
ID: 38720195
ok. thanks.
can I use table_node element instead of $('#id_of_the_table') ?
0
 
LVL 11

Expert Comment

by:mcnute
ID: 38720226
you can give whatever id you want, unless it begins with an alpha value from a-z. id's like 1-item are not allowed since it starts with a number.

So it can be $('#table_node'), yes.
0
 

Author Comment

by:tuchfeld
ID: 38720313
I mean, what if the table_node is DOM element?
retrieved somehow (it has NOT an id property).
e.g. table_node = some_element.parentNode.childNodes[3]; // etc.
0
 
LVL 11

Expert Comment

by:mcnute
ID: 38720325
If you know that it is the third table in the DOM, then you can do something like this:

$('table:eq(2)').find('a');

Open in new window


Considering that index starts at 0 so your first is 0, second table is 1, third is 2.

Am I understanding you right there?

You seam to know which table that is. You have a varialbe with the object of the table stored in it, right? You can just use it as is:

var tablea = table_node.find('a');

tablea.each(function(){
   if ( $(this).attr('id').match(/foo/g) ) {
   //  do something with it
   }
});

Open in new window

0
 

Author Closing Comment

by:tuchfeld
ID: 38720343
Thanks.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question