Why isn't document.evaluate working? - javascript

I am using this in a Firefox extension and can't get it to work.
var allLinks = document.evaluate(
'//a[#href]',
window.document,
null,
XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,
null);
window.alert(allLinks);
This alerts [object XPathResult]. However the following always returns 0. (And there are certainly links on the page).
window.alert(allLinks.snapshotLength);

If it's firefox specific and I assume you only use new versions.. can't you use querySelectorAll?
document.querySelectorAll('a[href]')
Also - are you doing it when the DOM has fully loaded? bind it to DOMContentLoaded or something.
document.addEventListener('DOMContentLoaded', fn, false);
EDIT: Your xpath works for me. Did it in the console on an existing, loaded page. Pretty sure you're querying before anything exists.

The answer that you don't want to hear - "works for me".
See this example (alerts 3 for me).
What browser and what version are you testing this on?

This worked.
var allLinks = top.document.getElementById("content").selectedBrowser.contentDocument.evaluate(
treeView.model[0].xpath,
top.document.getElementById("content").selectedBrowser.contentDocument,
null,
XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,
null);

Related

JavaScript Element.requestFullscreen() is undefined

How do I use the code if element.requestFullscreen() is undefined?
if (element.webkitRequestFullScreen) {
element.webkitRequestFullScreen();
}
If requestFullscreen returns undefined, it means that you cannot request fullscreen because that function doesn't exist. Every browser other than IE10 and below supports it but you need to use the right vendor prefix as you commented. A good way you can do that is by doing
var requestFullScreen = elem.requestFullscreen || elem.msRequestFullscreen || elem.mozRequestFullScreen || elem.webkitRequestFullscreen;
I think your issue is you spelled some of the functions wrong. it's confusing because Mozilla uses .mozRequestFullScreen whereas the others use requestFullscreen. The capital S can be annoying.
where elem is the video element from the DOM. You can then do requestFullScreen.call(elem) and it will initiate fullscreen.
And as #ArunPJohny commented, definitely take a look at that MDN article.

Xpath evaluation in javascript not working relative to some element

The code below will reach the "before" and "between" alerts, but not the "after" alert.
alert("before")
var test = document.evaluate('.//*',document,document.createNSResolver(document),XPathResult.ANY_TYPE,null)
alert(element.tagName)//alerts "TABLE"
alert("between")
test = document.evaluate('.//*',element,document.createNSResolver(document),XPathResult.ANY_TYPE,null)
alert("after")
This is written as part of a selenium extension. and alert(document) in the code gives a different result than alert(document) when run in firebug. [object XULDocument] vs [object HTMLDocument].
Your second test has an undefined variable element.
solved.
document does not refer to the HTML document in selenium extensions. however each element has a reference to the document which owns it. in this case
test = document.evaluate('.//*',element,document.createNSResolver(document),
XPathResult.ANY_TYPE,null)
becomes
test = element.ownerDocument.evaluate('.//*',element,
element.ownerDocument.createNSResolver(element.ownerDocument),
XPathResult.ANY_TYPE,null)
and so on...
A variant of Zackkenyon answer, without the resolver seems to work.
var rangee = table.ownerDocument.evaluate("./tbody/tr", table, null,
XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;

jQuery SVG plugin transform animation error

I'm trying to use the jQuery SVG plugin to animate some stuff — scaling and whatnot. I'm totally new to SVG.
var params = {};
params['svgTransform'] = ['scale(1)', 'scale(1.5)'];
$('#TX', svg.root()).animate(params);
This is copied almost verbatim from the developer of the plugin.
Yet when it runs, I'm getting this:
4TypeError: 'undefined' is not a function (evaluating 'f.easing[i.animatedProperties[this.prop]](this.state,c,0,1,i.duration)')
Any ideas?
I think you should check for existence of an element with ID="TX" in your SVG document.
Anyway, I must say that sometimes I found very difficult to remember where to code specific behaviour: there are so many choices, among XML (plain SVG), plain JavaScript+DOM (but what DOM?), jQuery specific, jQuery+SVG.... And all of these with their details... It's daunting! I hope it will be rewarding in the end.
BTW I found that Chrome give a good IDE to workout problems (I'm on Linux now...). Hit Ctrl+Shift+I to enter the debugger and see whatever error...
maybe it doesn't support array inside animate arg object. can you try :
var params = {};
params['svgTransform'] = 'scale(1.5)';
$('#TX', svg.root()).animate(params);

Does IE7 not fully support javascript's insertBefore method?

I have the following code which works perfect in Chrome, IE8, and FF. However, I get an error when I'm testing it with IE7. Does anyone have a clue what's happening here?
function do_replace(s, p1,p2,p3,child_node,syn_text) {
reg = new RegExp('[h\|H][1-7]');
if(p1.length>0){ //this might not be necessary
//create textnode
var text_node = document.createTextNode(p1);
child_node.parentNode.insertBefore(text_node,child_node); //errors out here in IE7
}
The code errors out at the last line- IE7 give an "htmlfile: Invalid argument." error when I look at the code through a debugger. child_node, parentNode, and text_node appear to be formed identical to Firefox and Chrome when running this script.
Any ideas? Or does IE7 just not support this method as well as other browsers?
Thanks
Rather than leave this problem unsolved, I figured out what was wrong with my code:
I was using an extensive frameset(yuck!!) and when I made the text_node = document.createTextNode() call, I was not doing this in the frame that my application was in.
I fixed this by explicitly calling out the frame to create the object in:
var text_node = MainFrame.child_frame.WhySoManyFrames.document.createTextNode(p1);
After doing this, the insertBefore method works perfect!
Hopefully this helps anyone looking at this question- I know this took me a long time and lots of frustration to figure out!
JavaScript 'InsertBefore' function is supported by IE7. Remember that you have to use this function only when page is fully loaded!
Details

JavaScript: iterating over document.evaluate() XPathResult returns null

I am trying to get all objects but it doesn't work.
var tmp = document.evaluate("//tr", document, null, XPathResult.ANY_TYPE, null);
tmp.iterateNext returns me null;
If you are working in IE, then you should know that document.evaluate does not exist there (see this similar question and the last couple of paragraphs here).
Why not just document.getElementsByTagName("tr");?
In my opinion, using an existing JS libraries such as JQuery is usually more reliable in cases like this.
document.getElementsByTagName("tr");
It also will not work, because IE some versions doesn't support this method for elements, which has not such attribute like 'name'.

Categories

Resources