Undefined error when trying to identify descendant of parent using hasAttribute - javascript

I'm trying to determine if an element is a descendant of another elect that has a specific attribute. So far, I have no issue when the value is true, but I get TypeError: undefined is not a function (evaluating 'node.hasAttribute(attribute)') when hasAttribute is false. Any ideas?
document.body.addEventListener('touchmove', function(event) { stopScroll(event); });
var stopScroll = function(event) {
var target = event.target;
var parent = target.parentNode;
if (isDescendantOf.attribute('data-scroll', target)) {
} else {
event.preventDefault();
}
};
var isDescendantOf = {};
isDescendantOf.parent = function(parent, child) {
var node = child.parentNode;
while (typeof node !== 'undefined') {
if (node == parent) {
return true;
}
node = node.parentNode;
}
return false;
};
isDescendantOf.tag = function(tag, child) {
var node = child.parentNode;
while (typeof node !== 'undefined') {
if (node.tagName == tag) {
return true;
}
node = node.parentNode;
}
return false;
};
isDescendantOf.attribute = function(attribute, child) {
var node = child.parentNode;
while (node !== null) {
console.log(typeof node);
if (node.hasAttribute(attribute) === true) {
return true;
}
node = node.parentNode;
}
return false;
};
Here's the fiddle, though it acts even weirder for some reason: http://jsfiddle.net/x95tgmkn/

Your loop goes from the child.parentNode up to the document, and on each node it uses the hasAttribute method of the Element interface. However, Documents are plain Nodes that do not implement this interface.
Change your condition to
while (node != null && node.nodeType == 1)
or node.nodeType != 9 or typeof node.hasAttribute == "function".

Related

binary tree add an undefined node

Problem
I'm trying to study better the tree theory, i can add one or more node without problems but i noticed that if i tried to launch multiple add function without pass a parameter, simply it doesn't work. Could you explain where is my mistake ?
Code
BinarySearchTree.prototype.makeNode = function(value) {
var node = {};
node.value = value;
node.left = null;
node.right = null;
return node;
};
BinarySearchTree.prototype.add = function(value) {
var currentNode = this.makeNode(value);
if (!this.root) {
this.root = currentNode;
} else {
this.insert(currentNode);
}
return this;
};
BinarySearchTree.prototype.insert = function(currentNode) {
var value = currentNode.value;
var traverse = function(node) {
if (value > node.value) {
if (!node.right) {
node.right = currentNode;
return;
} else traverse(node.right);
} else if (value < node.value) {
if (!node.left) {
node.left = currentNode;
return;
} else traverse(node.left);
}
};
traverse(this.root);
};
Now if i try to
var bst = new BinarySearchTree();
bst.add(3).add(2);
console.log(bst);
i will have this console.log
if i try to pass an undefined value
var bst = new BinarySearchTree();
bst.add().add(2);
console.log(bst);
Expectation
I expect that the last console.log doesn't lost the value of 2.
. i read this post to understand better what if i didn't pass any value to a function
What happens if I don't pass a parameter in a Javascript function?
and other posts ( like medium and stack overflow ) and guide related to the tree theory but i didn't find any solution
SOLUTION
Thanks to the recommendation and the correction of #Nina Scholz i just added this lines to this function
BinarySearchTree.prototype.add = function(value) {
if (typeof value == 'undefined') {
value = null;
}
var currentNode = this.makeNode(value);
if (!this.root) {
this.root = currentNode;
console.log('sei qui')
} else {
this.insert(currentNode);
}
return this;
};
Nothing happens, because both conditions evaluate with undefined as value to false.
if (value > node.value) {
// ...
} else if (value < node.value) {
// ...
}
function BinarySearchTree() {}
BinarySearchTree.prototype.makeNode = function(value) {
var node = {};
node.value = value;
node.left = null;
node.right = null;
return node;
};
BinarySearchTree.prototype.add = function(value) {
var currentNode = this.makeNode(value);
if (!this.root) {
this.root = currentNode;
} else {
this.insert(currentNode);
}
return this;
};
BinarySearchTree.prototype.insert = function(currentNode) {
var value = currentNode.value;
var traverse = function(node) {
if (value > node.value) {
if (!node.right) {
node.right = currentNode;
return;
} else traverse(node.right);
} else if (value < node.value) {
if (!node.left) {
node.left = currentNode;
return;
} else traverse(node.left);
}
};
traverse(this.root);
};
var bst = new BinarySearchTree();
bst.add(3).add(2).add();
console.log(bst);

Load child nodes without expand in ExtJs Tree Panel

I have created a tree using Treepanel in Extjs. The following is the code snippet which is used which to traverse the node using drag and drop
beforenodedrop:function(dropEvent){
try {
targetNode = dropEvent.target;
sourceNode = dropEvent.dropNode;
sourceNode = getDeepAllChildNodes(sourceNode);
treeTarget = sourceNode.getOwnerTree().id;
if (treeTarget == 'tree' && sourceNode.attributes.lvl > 0){
changeAlignTrees = true;
if (sourceNode.childNodes != null){
var targetFather = {};
var node = sourceNode;
if(targetNode.id==-2){
targetFather = targetNode;
}else{
targetFather = targetNode.parentNode;
}
while(sourceNode.hasChildNodes() == true){
node = node.firstChild;
if (node.hasChildNodes() == false){
node = node.parentNode;
targetFather.appendChild(node.firstChild);
if(node.hasChildNodes() == false){
node = node.parentNode;
}
}
}
}
if(targetNode.id==-2){
dropEvent.point = 'append';
}else{
dropEvent.point = 'below';
targetNode.leaf='true';
}
Ext.Ajax.request({
url: "testURL",
params: {
option: 'alUnit',
unitNum: sourceNode.id,
parentUnitNum: '-1'
},
scriptTag: true,
success: function(response, options){
}
});
}else{
dropEvent.cancel=true;
}
}catch (e) {
logException("Exception: " + e.description);
}
}
This is Async Tree Node, if i do not expand the nodes then the following line node = node.firstChild; returns null. I have one more function to get all the child nodes, the following is the code snippet
getDeepAllChildNodes = function(node){
var allNodes = new Array();
if(!Ext.value(node,false)){
return [];
}
if(!node.hasChildNodes()){
return node;
}else{
allNodes.push(node);
node.eachChild(function(Mynode){allNodes = allNodes.concat(getDeepAllChildNodes(Mynode));});
}
return allNodes;
};
When executed the line !node.hasChildNodes() the following error "Object doesn't support property or method 'hasChildNodes'"
How can i achieve traversing all child nodes in treepanel without expanding.

Binary Search Tree JavaScript implementation - remove function

Here's my implementation for a Binary Search Tree in JavaScript. All functions appear to be working properly except for the remove function. Specifically, it seems to be removing nodes properly until there's 2 nodes left in the tree:
var binaryTreeNode = function (value) {
return {
value : value,
left : null,
right : null
};
};
var binarySearchTree = function () {
var tree = Object.create( binarySearchTreeMethods );
tree.root = null;
return tree;
};
var binarySearchTreeMethods = {
insert: function (value, node) {
var newNode = binaryTreeNode( value );
// check if tree is empty
if ( this.isEmpty() ) {
this.root = newNode;
return;
}
// initialize node
if ( node === void 0 ) node = this.root;
// compare value with node.value
if ( value <= node.value ) {
// check if left exists
if ( node.left ) {
this.insert( value, node.left );
} else {
node.left = newNode;
}
} else {
if ( node.right ) {
this.insert( value, node.right );
} else {
node.right = newNode;
}
}
},
remove: function (value, node) {
var nextRightValue, nextLeftValue, minRight;
if ( !this.isEmpty() ) {
// initialize node
if ( node === void 0 ) node = this.root;
// compare the node's value with the value
if ( value < node.value ) {
// check if there is a left node
if ( node.left ) {
node.left = this.remove( value, node.left );
}
} else if ( value > node.value ) {
// check if there is a right node
if ( node.right ) {
node.right = this.remove( value, node.right );
}
} else {
// at this point, value === node.value
// check if node is a leaf node
if ( node.left === null && node.right === null ) {
// edge case of single node in tree (i.e. root node)
if ( this.getHeight() === 0 ) {
this.root = null;
return this.root;
} else {
node = null;
}
} else if ( node.left === null ) {
node = node.right;
} else if ( node.right === null ) {
node = node.left;
} else {
// node has both left and right
minRight = this.findMinValue( node.right );
node.value = minRight;
node.right = this.remove( minRight, node.right );
}
}
return node;
}
},
contains: function (value, node) {
if ( this.isEmpty() ) return false;
// tree is not empty - initialize node
if ( node === void 0 ) node = this.root;
// check if node's value is the value
if ( value === node.value ) return true;
if ( value < node.value ) {
// check if left node exists
return node.left ? this.contains( value, node.left ) : false;
} else {
// check if right node exists
return node.right ? this.contains( value, node.right ) : false;
}
},
findMaxValue: function (node) {
if ( !this.isEmpty() ) {
if ( node === void 0 ) node = this.root;
while ( node.right ) {
node = node.right;
}
return node.value;
}
},
findMinValue: function (node) {
if ( !this.isEmpty() ) {
if ( node === void 0 ) node = this.root;
while ( node.left ) {
node = node.left;
}
return node.value;
}
},
getHeight: function (node) {
if ( !this.isEmpty() ) {
// initialize node
if ( node === void 0 ) node = this.root;
// base case
if ( node.left === null && node.right === null ) return 0;
if ( node.left === null ) return 1 + this.getHeight( node.right );
if ( node.right === null ) return 1 + this.getHeight( node.left );
return 1 + Math.max( this.getHeight( node.left ), this.getHeight( node.right ) );
}
},
isEmpty: function () {
return this.root === null;
}
};
Inserting values into the binary search tree works fine:
var bst = binarySearchTree();
bst.insert(10);
bst.insert(5);
bst.insert(20);
bst.insert(30);
bst.insert(22);
bst.insert(18);
I come across an issue when I start removing the root value each time:
bst.remove(10); // this works fine and the resulting bst tree is structurally correct
bst.remove(18); // this works fine and the resulting bst tree is structurally correct
bst.remove(20); // this works fine and the resulting bst tree is structurally correct
bst.remove(22); // this works fine and the resulting bst tree is structurally correct
bst.remove(30); // THIS IS WHERE THE ISSUE OCCURS
Prior to removing 30, the tree only has two values: 30 as the root value and 5 as the root.left value. I would expect that removing 30 would give me a tree which has 5 as the root. However, removing 30 doesn't do anything to the tree; it remains the same.
Further testing shows that if I had removed 5 first and then 30, then everything works fine as well:
bst.remove(10); // this works fine and the resulting bst tree is structurally correct
bst.remove(18); // this works fine and the resulting bst tree is structurally correct
bst.remove(20); // this works fine and the resulting bst tree is structurally correct
bst.remove(22); // this works fine and the resulting bst tree is structurally correct
bst.remove(5); // Results in a tree with 30 as the root value
bst.remove(30); // Results in the empty tree where root === null
Can anyone help me understand why removing 30 initially didn't work?
Your code has a provision for the case when the found node is the root and it is the only node in the tree, and if a node has both a left and a right child, you overwrite its value. But when the node to remove is the root and it has only one child, there is nothing in your code that overwrites this.root, and you don't overwrite the root's value, so it is not removed and the tree remains unmodified.
You can fix this by changing this:
if ( node === void 0 ) node = this.root;
// compare the node's value with the value
if ( value < node.value ) {
to this:
if ( node === void 0 ) {
this.root = this.remove(value, this.root);
// compare the node's value with the value
} else if ( value < node.value ) {
Once that is fixed, you can simplify your logic a bit:
remove: function (value, node) {
if (!this.isEmpty()) {
// initialize node
if (!node) {
this.root = this.remove(value, this.root);
} else if (value < node.value && node.left) {
node.left = this.remove(value, node.left);
} else if (value > node.value && node.right) {
node.right = this.remove(value, node.right);
} else if (value === node.value) {
// check if node is a leaf node
if (node.left && node.right) {
// node has two children. change its value to the min
// right value and remove the min right node
node.value = this.findMinValue(node.right);
node.right = this.remove(node.value, node.right);
} else {
// replace the node with whichever child it has
node = node.left || node.right;
}
}
return node;
}
},
and then you can simplify it further by separating it into two methods:
remove: function (value) {
this.root = this._removeInner(value, this.root);
},
_removeInner: function (value, node) {
if (node) {
if (value < node.value) {
node.left = this._removeInner(value, node.left);
} else if (value > node.value) {
node.right = this._removeInner(value, node.right);
} else if (node.left && node.right) {
node.value = this.findMinValue(node.right);
node.right = this._removeInner(node.value, node.right);
} else {
node = node.left || node.right;
}
}
return node;
},
Demo
#wmock has asked how I went about solving this problem so I'll elaborate on that a bit.
The first thing I did was walk the code in the debugger, focusing on the bst.remove(30) part. I noticed that 30 was the root at that point and that it remained there after remove() was done. This led me to noticing that the code never modifies the root in that particular case.
I then looked at how the return value of this.remove() was being assigned to node.left and node.right, and with some recollection of BST algorithms, thought it would make sense to emulate that for the root as well. And that was indeed the answer.
There were a few things that motivated splitting the method into two methods:
I noticed that the method had a fair amount of special-case functionality that was only relevant for the initial call to bst.remove()
Checking this.isEmpty()
Using this.root for the value of node if node was null
Resetting this.root to null under certain cases when the tree height is 0
It seemed sloppy to be doing all of that in every pass through remove()
I also repeatedly found myself wanting to use if (!node) to check whether I had reached the edge of the tree, but I couldn't because there was that special case logic to use this.root when node was null.
Splitting the method into two parts resolved all of the above issues.
Note that in a lot of BST implementations, the functionality in _removeInner() would be a method on the binaryTreeNode type, and the tree would just interact with the root node. That eliminates the need to pass a node from one method call to the next:
In binarySearchTree:
remove: function (value) {
this.root && this.root.remove(value);
},
In binaryTreeNode:
remove: function (value) {
if (value < this.value) {
this.left = this.left && this.left.remove(value);
} else if (value > this.value) {
this.right = this.right && this.right.remove(value);
} else if (this.left && this.right) {
this.value = this.right.findMinValue();
this.right = this.right.remove(this.value);
} else {
return this.left || this.right;
}
return this;
},
findMinValue: function () {
return this.left ? this.left.findMinValue() : this.value;
}
Demo
Here is the full example of a Binary tree with insert and remove functionalities
function Node(val) {
this.data = val;
this.right = null;
this.left = null;
}
function BST() {
this.root = null;
this.insert = insert;
this.inOrder = inOrder;
this.remove = remove;
this.removeNode = removeNode;
this.kthSmallestNode = kthSmallestNode;
}
function insert(val) {
if (val == null || val == undefined)
return;
if (this.root == null) {
this.root = new Node(val);
return;
}
var current = this.root
var newNode = new Node(val);
while (true) {
if (val < current.data) {
if (current.left == null) {
current.left = newNode;
return;
}
current = current.left;
} else {
if (current.right == null) {
current.right = newNode;
return;
}
current = current.right;
}
}
}
function remove(val) {
this.root = removeNode(this.root, val);
}
function removeNode(current, value) {
if (value == null || value == undefined)
return;
if (value == current.data) {
if (current.left == null && current.right == null) {
return null;
} else if (current.left == null)
return current.right;
else if (current.right == null)
return current.left;
else {
var tempNode = kthSmallestNode(current.right);
current.data = tempNode.data;
current.right = removeNode(current.right, tempNode.data);
return current;
}
} else if (value < current.data) {
current.left = removeNode(current.left, value);
return current;
} else {
current.right = removeNode(current.right, value);
return current;
}
}
function kthSmallestNode(node) {
while (!(node.left == null))
node = node.left;
return node;
}
function inOrder(node) {
if (!(node == null)) {
inOrder(node.left);
console.log(node.data + " ");
inOrder(node.right);
}
}
var tree = new BST();
tree.insert(25);
tree.insert(20);
tree.insert(30);
tree.insert(27);
tree.insert(21);
tree.insert(16);
tree.insert(26);
tree.insert(35);
tree.remove(30)
console.log("Inorder : ")
console.log(tree.inOrder(tree.root))
Good Luck!!!
I have a pretty simplified answer that I think most people will understand and it takes into account children nodes. The key is that if you are removing a value with a right and left child that you first go left and then all the way right because this assures you that it will have no children and be easier to update.
removeNode(val) {
let currentNode, parentNode, nextBiggestParentNode=null, found=false, base=[this.root];
while(base.length > 0 && !found) {
currentNode = base.pop();
if(currentNode.value === val) {
found=true;
if(!currentNode.left && !currentNode.right) {
parentNode.right === currentNode ? parentNode.right = null : parentNode.left = null;
}
else if(!currentNode.right && currentNode.left) {
parentNode.right === currentNode ? parentNode.right = currentNode.left : parentNode.left = currentNode.left;
}
else if(!currentNode.left && currentNode.right) {
parentNode.right === currentNode ? parentNode.right = currentNode.right : parentNode.left = currentNode.right;
}
else {
let _traverse = node => {
if (node.right) {
nextBiggestParentNode = node;
_traverse(node.right);
}
else {
currentNode.value = node.value;
nextBiggestParentNode ? nextBiggestParentNode.right = null : currentNode.left = null;
}
}
_traverse(currentNode.left);
}
}
else {
parentNode = currentNode;
val > currentNode.value && currentNode.right ? base.unshift(currentNode.right) : base.unshift(currentNode.left);
}
}
return this;
}
that code is part of a class, here is the rest of my constructor code if anybody is interested
let TreeNode = class {
constructor(value, left=null, right=null) {
this.value = value;
this.left = left;
this.right = right;
}
}
let BST = class {
constructor(root=null) {
this.root = root;
}
insert(nodeToInsert) {
if (this.root === null) {
this.root = nodeToInsert;
} else {
this._insert(this.root, nodeToInsert);
}
}
_insert(root, nodeToInsert) {
if (nodeToInsert.value < root.value) {
if (!root.left) {
root.left = nodeToInsert;
} else {
this._insert(root.left, nodeToInsert);
}
} else {
if (!root.right) {
root.right = nodeToInsert;
} else {
this._insert(root.right, nodeToInsert);
}
}
}
here is some demo code to create a bst and remove a value
let bst = new BST();
const nums = [20,10,5,15,3,7,13,17,30,35,25,23,27,37,36,38];
function createBst() {
for (let i of nums) {
bst.insert(new TreeNode(i));
}
console.log(JSON.stringify(bst, null, 2));
bst.removeNode(35);
}
createBst();
console.log(JSON.stringify(bst, null, 2));

Getting Error: Could not convert JavaScript argument arg 0 [nsIDOMWindow.getComputedStyle]

I have a bit of javascript code to find and replace text into an image. I then gather the font size of the original text and use that to set the size of the new image.
Problem is, I keep getting the error: Could not convert JavaScript argument arg 0 [nsIDOMWindow.getComputedStyle]
Code:
function findAndReplace(searchText, replacement, searchNode) {
if (!searchText || typeof replacement === 'undefined') {
// Throw error here if you want...
return;
}
var regex = typeof searchText === 'string' ?
new RegExp(searchText, 'g') : searchText,
childNodes = (searchNode || $("body").get(0)).childNodes,
excludes = 'html,head,style,title,link,meta,script,object,iframe';
var cnLength = childNodes.length;
while (cnLength--) {
var currentNode = childNodes[cnLength];
if (currentNode.nodeType === 1 &&
(excludes + ',').indexOf(currentNode.nodeName.toLowerCase() + ',') === -1) {
arguments.callee(searchText, replacement, currentNode);
}
if (currentNode.nodeType !== 3 || !regex.test(currentNode.data) ) {
continue;
}
var parent = currentNode.parentNode;
var frag = (function(){
var html = currentNode.data.replace(regex, replacement);
var wrap = document.createElement('div');
var frag = document.createDocumentFragment();
wrap.innerHTML = html;
while (wrap.firstChild) {
frag.appendChild(wrap.firstChild);
}
console.log(currentNode);
var jQNode = $(currentNode);
console.log("yay");
// var fontSize = jQNode.css('font-size');
if (!currentNode || currentNode == document) currentNode = document.body
var fontSize = getStyle(currentNode, 'font-size');
console.log("tast");
var heightPixels = fontSizeToPixels(fontSize);
$(".InLogo",frag).each(function(){
$(this).css("height", heightPixels+"px");
});
return frag;
})();
parent.insertBefore(frag, currentNode);
parent.removeChild(currentNode);
}
}
function getStyle(el,styleProp) {
var camelize = function (str) {
return str.replace(/\-(\w)/g, function(str, letter){
return letter.toUpperCase();
});
};
if (el.currentStyle) {
return el.currentStyle[camelize(styleProp)];
} else if (document.defaultView && document.defaultView.getComputedStyle) {
return document.defaultView.getComputedStyle(el,null)
.getPropertyValue(styleProp);
} else {
return el.style[camelize(styleProp)];
}
}
The error occurs at this line return document.defaultView.getComputedStyle(el,null).getPropertyValue(styleProp); of getStyle()
something.childNodes includes textNodes as well as Elements, and that's a problem for the getStyle() function.
Nodes don't have a style (Elements do), so who knows what will happen when you feed getStyle something that has .data; a plain Node.
Check for the existence of style to avoid the run-time error:
FIX:
var fontSize = currentNode.style ? getStyle(currentNode, 'font-size') : 0;

Javascript IE 7 getting OBJECT element

I'm making a code that removes a videoplayer from the page and then places it back when needed (even if the element doesn't have an id).
I'm finding issues with IE7
Here is my code:
var weboElem, weboElemPar, weboElemIndex, weboStored;
function weboRemoveVideoplayer(vpId){
weboElem = document.getElementById(vpId);
if(!weboElem) return false;
weboElemPar = weboElem.parentNode;
weboElemIndex = 0;
var child = weboElem;
while( (child = child.previousSibling) != null )
weboElemIndex++;
weboElemPar.removeChild(weboElem);
return true;
}
function weboPlaceVideoplayerBack(){
if(weboElemPar.insertBefore !== undefined && weboElemPar.childNodes !== undefined)
{
weboElemPar.insertBefore(weboElem, weboElemPar.childNodes[weboElemIndex]);
return true;
}
return false;
}
var result = document.evaluate(
'//*/param[contains(#value, "autoplay=1")]/..', // XPath expression
document, // context node
null, // namespace resolver
XPathResult.ORDERED_NODE_SNAPSHOT_TYPE
);
if(result.snapshotLength > 0)
{
var node = result.snapshotItem(0);
node.id = "webo";
document.getElementById('info').innerHTML = node.nodeName.toLowerCase()+" -> "+node.id;
} else document.getElementById('info').innerHTML = "not found";
(Note that document.evaluate WORKS because I imported javascript-xpath library)
On IE7 if the XPath finds an IFRAME there are no problems and it works but if it finds an OBJECT does nothing and stops at weboElem = document.getElementById(vpId); as if it didn't find the id.
I tried modifying the code like this:
if(result.snapshotLength > 0)
{
var node = result.snapshotItem(0);
node.id = "webo";
node.parentNode.removeChild(node);
document.getElementById('info').innerHTML = node.nodeName.toLowerCase()+" -> "+node.id;
if(node.nodeName.toLowerCase() == "object") weboStored = node;
else weboStored = null;
} else document.getElementById('info').innerHTML = "not found";
and it works, the videoplayer disappears at page load. I want to use the function though, so I edited everything like this (storing the node into a global var that later I get in the weboRemoveVideoplayer function):
var weboElem, weboElemPar, weboElemIndex, weboStored;
function weboRemoveVideoplayer(vpId){
if(!weboStored) weboElem = document.getElementById(vpId);
else weboElem = weboStored;
if(!weboElem) return false;
weboElemPar = weboElem.parentNode;
weboElemIndex = 0;
var child = weboElem;
while( (child = child.previousSibling) != null )
weboElemIndex++;
weboElemPar.removeChild(weboElem);
alert("5");
return true;
}
function weboPlaceVideoplayerBack(){
if(weboElemPar.insertBefore !== undefined && weboElemPar.childNodes !== undefined)
{
weboElemPar.insertBefore(weboElem, weboElemPar.childNodes[weboElemIndex]);
return true;
}
return false;
}
// bind XPath methods to document and window objects
// NOTE: This will overwrite native XPath implementation if it exists
//XPathJS.bindDomLevel3XPath(); //solo per xpathJs
var result = document.evaluate(
'//*/param[contains(#value, "autoplay=1")]/..', // XPath expression
document, // context node
null, // namespace resolver
XPathResult.ORDERED_NODE_SNAPSHOT_TYPE
);
if(result.snapshotLength > 0)
{
var node = result.snapshotItem(0);
node.id = "webo";
node.parentNode.removeChild(node);
document.getElementById('info').innerHTML = node.nodeName.toLowerCase()+" -> "+node.id;
if(node.nodeName.toLowerCase() == "object") weboStored = node;
else weboStored = null;
} else document.getElementById('info').innerHTML = "not found";
This way the code blocks itself when trying to retrieve the parent node.
Could someone suggest me what to do here?
PS: with chrome and firefox the code works perfectly in the first version I posted.
Fixed it!
I solved the issue by wrapping the OBJECT inside a div with an id of my choice which I can retrieve whenever I want. I do this in the resolveXpath function.
Here the code:
var weboElem, weboElemPar, ieObject = false;
var weboElemIndex = 0;
function weboRemoveVideoplayer(vpId){
var child;
if(!ieObject) weboElem = document.getElementById(vpId);
else weboElem = document.getElementById('my_usage');
if(!weboElem) return false;
weboElemPar = weboElem.parentNode;
weboElemIndex = 0;
child = weboElem;
while( (child = child.previousSibling) != null ) weboElemIndex++;
if(typeof weboElemPar.removeChild !== 'undefined') weboElemPar.removeChild(weboElem);
else return false;
return true;
}
function weboPlaceVideoplayerBack(){
if(typeof weboElemPar.insertBefore !== 'undefined' && typeof weboElemPar.childNodes !== 'undefined' && typeof weboElemPar.appendChild !== 'undefined'){
if(weboElemPar.childNodes.length > 0 && weboElemIndex < weboElemPar.childNodes.length) weboElemPar.insertBefore(weboElem, weboElemPar.childNodes[weboElemIndex]);
else weboElemPar.appendChild(weboElem);
return true;
}
return false;
}
function resolveXpath(path)
{
//XPathJS.bindDomLevel3XPath(); //solo per xpathJs
var result = document.evaluate(path,document,null,XPathResult.ORDERED_NODE_SNAPSHOT_TYPE);
if(result.snapshotLength > 0){
var child, node = result.snapshotItem(0);
if(node.nodeName.toLowerCase() == 'object'){
ieObject = true;
child = node;
while( (child = child.previousSibling) != null ) weboElemIndex++;
var div = document.createElement('div');
div.id = 'my_usage';
if(typeof node.parentNode.insertBefore !== 'undefined' && typeof node.parentNode.childNodes !== 'undefined' && typeof node.parentNode.appendChild !== 'undefined'){
if(node.parentNode.childNodes.length > 0 && weboElemIndex < node.parentNode.childNodes.length) node.parentNode.insertBefore(div,node.parentNode.childNodes[weboElemIndex]);
else node.parentNode.appendChild(div);
div.appendChild(node);
} else return false;
} else node.id = 'my_usage';
return true;
} else return false;
}
resolveXpath('//*/param[contains(#src, "autoplay=1")]/..');

Categories