I am trying to find all buttonitems. In chrome and modern browsers this works fine but in ie9 I am having the error above. I can't change the HTML on the page only the javascript on the page. In chrome I can use document.getElementsByClassName() but in ie 9 I can't even though I should be able to probably because the website has to run in comparability with Quirks. I have tried rewriting the code to work without this and believe I have got the elements with the code
if(navigator.userAgent.toLowerCase().indexOf('msie') != -1){
if (document.all) {
var allElements = document.all;
} else {
var allElements = document.getElementsByTagName("*");
}
// Empty placeholder to put in the found elements with the class name
var oldHrefs = [];
for (var i = 0, ii = allElements.length; i < ii; i++) {
if (allElements[i].className == 'ButtonItem') {
oldHrefs[oldHrefs.length] = allElements[i];
}
}
}
else
{
var oldHrefs = document.getElementsByClassName('ButtonItem');
}
But I then need to loop through the code and remove the href and add an onclick which is when I revive my next error. Object doesn't support property or method 'item'. Any ideas what I can do to correct the code below?
for (var i = 0; i < oldHrefs.length; i++) {
var thisHref = oldHrefs.item(i).getAttribute('href');
if (thisHref != null) {
if (thisHref.includes('Act=1468')) {
oldHrefs.item(i).removeAttribute("href");
oldHrefs.item(i).setAttribute('onclick', 'window.location.href="' + thisHref + '";this.removeAttribute("onclick");');
}
}
}
Thanks
Related
I am trying to make an automated test using NoghtwatchJS.
I am dealing with a UI that contains elements with dynamic IDs. I have to click on a particular element. I am thinking about using gelElementByTagName('aria-label') in mycase. This is the code I used :
var labels,i
labels = getElementsByTagName('aria-label');
for ( i = 0; i < labels.length; i++) {
if (labels[i].htmlFor == 'Next') {
var elem = getElementById(labels[i].htmlFor)
};
};
I have the issue element not defined :
ReferenceError: getElementsByTagName is not defined
Do you have any idea how I could solve this problem?
I have tried :
getElementsByTagName('aria-label')
element.getElementsByTagName('aria-label')
document.getElementsByTagName('aria-label')
The only way to make this work in Nightwatch is to use the execute comand
Your code should looke something like this:
browser.execute(function() {
return document.getElementsByTagName('aria-label').length;
}, function(result) {
for (let i = 0; i < result.value; i++) {
if (labels[i].htmlFor == 'Next') {
let elem = getElementById(labels[i].htmlFor)
};
});
I am currently having an issue with the getAttribute() method.
This currently works in IE8, but in IE11 I recieve the error Object doesn't support property or method 'getAttribute'.
The same issue happens when I use hasAttribute() at the same point.
The error is thrown when you reach if(discounts[j].getAttribute("id") == discountId), and if I try to console.log the id, I get Undefined.
I did manage to get it to work in IE11 by running in compatibility mode, but that is not an option.
This is the method I am currently using below.
if(discountsXml != null && discountsXml.documentElement != null) {
var invItems = discountsXml.documentElement.getElementsByTagName("invItem");
var invItemsCounter = invItems.length;
var i = 0;
for(i=0; i<invItemsCounter; i++) {
if(invItems[i].getAttribute("id") == invItemId) {
var discounts = invItems[i].childNodes;
var discountsCounter = discounts.length;
var j = 0;
for(j=0; j<discountsCounter; j++) {
if(discounts[j].getAttribute("id") == discountId) {
discount = true;
}
}
}
}
}
You didn't actually ask a question above so I'm not sure on the best answer,
But would it be possible for you to use the id property instead of the id attribute as is generally recommended?
invItems[i].id vs invItems[i].getAttribute("id")
http://www.w3schools.com/jsref/prop_html_id.asp
I have never come across this before, it works fine on both of the other two magento stores I manage.
This store has been running for over a year with no issues in the backend. However today for the first time, rather than a % discount, I tried to add a fixed one to find that there I cannot add an action. Regardless of browser that I use, clicking on 'ALL', 'TRUE' or the Green Plus does absolutely nothing - in other stores it works perfectly.
Magento V1.7.0.2
Screenshot here
This is the error:
TypeError: ({initialize:(function (parent, newChildUrl){
this.parent = $(parent);
this.newChildUrl = newChildUrl;
this.shownElement = null;
this.updateElement = null;
this.chooserSelectedItems = $H({});
this.readOnly = false;
var elems = this.parent.getElementsByClassName('rule-param');
for (var i=0; i<elems.length; i++) {
this.initParam(elems[i]);
}
}), setReadonly:(function (readonly){
this.readOnly = readonly;
var elems = this.parent.getElementsByClassName('rule-param-remove');
for (var i=0; i<elems.length; i++) {
var element = elems[i];
if (this.readOnly) {
element.hide();
} else {
element.show();
}
}
var elems = this.parent.getElementsByClassName('rule-param-new-child');
for (var i=0; i<elems.length; i++) {
var element = elems[i];
Google chrome inspector is showing:
Uncaught TypeError: object is not a function 7ae86150a079d1644bc06caeec8fc3ef.js:20556(anonymous function)
I had the same problem, and although I'm unsure of the source of the problem, it seems to be caused when js files are merged.
In System > Configuration > Developer
Set 'Merge JavaScript Files' to 'No'
Then you should be able to add the price conditions, then just re-merge once complete.
Javascript
function test(){
var tableToSort = document.getElementById('tblid');
for (i=1; i < tableToSort.rows.length; i++)
{
alert("result ============> "+tableToSort.cells(iCurCell).innerText);
iCurCell = iCurCell + tableToSort.cols;
}
}
Upper function not works in IE but not in mozilla so i have change it with
function test(){
var tableToSort = document.getElementById('tblid');
for (i=1; i < tableToSort.rows.length; i++)
{
alert("result ============> "+tableToSort.rows[iCurCell1].cells[2].textContent);
iCurCell = iCurCell + tableToSort.cols;
}
}
In mozilla for first record of loop it works fine but for other it prints undefined.
While in IE all records print correctly.
innerText property is only for IE, see this page, use textContent for most of browser including IE9.
you should really look for the textNode inside the tablecell:
inside your for:
var tcell = tableToSort.rows[iCurCell1].cells[2];
var child = tcell.firstChild;
do {
if(child.nodeType == 3)
break;
} while(child = child.nextSibling);
var textThatYouWant = child.nodeValue;
this is the true DOM implementation that will work with all browsers
I have two frames, the expression running in first frame and calling highlightElements function in another frame. This expression works fine in Firefox:
parent.frames[0].highlightElements(lineNumbers, stringObj);
The highlightElements function (just for sure):
function highlightElements(lineNumbers, stringObj) {
// run through the cycle and highlight them
//for (var ln in lineNumbers) {
var length = lineNumbers.length;
for (var ln=0; ln<length; ln++) {
var elements = $('.no');
//for (var i in elements) {
var el_length = elements.length;
for (var i=0; i<el_length; i++) {
if (parseInt(elements[i].innerHTML) == lineNumbers[ln]) {
var badThing = "yes";
for (var nextElement = elements[i].next();
nextElement.className != '.no'; nextElement = elements[i].next()) {
if (nextElement.innerHTML == stringObj) {
badThing = "no";
nextElement.effect('highlight', {}, 'slow');
scrollIntoView(nextElement);
}
}
if (badThing == "yes") alert("Didn't find the object");
}
}
}
}
But in Chrome it produces the error "Uncaught TypeError: Property 'highlightElement' of object[objectDOMWindow] is not a function".
How to change the expression to make it runnable in Chrome? Thanks
Make sure both frames are under same domain and protocol. Chome blocks javascript access from frames to another if the domains/protocols don't match. If you are working locally, and not under a local domain (i.e. the url is something like file:///C:/etc/etc.html) then it won't work either.