Solved

javascript code for document.evaluate (xpath) in IE

Posted on 2012-12-24
9
596 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
  • 4
  • 4
9 Comments
 
LVL 22

Expert Comment

by:Mrunal
Comment Utility
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 500 total points
Comment Utility
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
Comment Utility
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
 
LVL 11

Assisted Solution

by:mcnute
mcnute earned 500 total points
Comment Utility
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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

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

Expert Comment

by:mcnute
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks.
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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…

762 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now