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.
Related
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
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
Firebug has the ability to log calls to a particular function name. I'm looking for a bug that sometimes stops a page from rendering, but doesn't cause any errors or warnings. The bug only appears about half the time. So how do I get a list of all the function calls for the entire program, or some kind of stack trace for the execution of the entire program?
Firefox provides console.trace() which is very handy to print the call stack. It is also available in Chrome and IE 11.
Alternatively try something like this:
function print_call_stack() {
var stack = new Error().stack;
console.log("PRINTING CALL STACK");
console.log( stack );
}
When i need a stack trace i do the following, maybe you can draw some inspiration from it:
function logStackTrace(levels) {
var callstack = [];
var isCallstackPopulated = false;
try {
i.dont.exist += 0; //doesn't exist- that's the point
} catch (e) {
if (e.stack) { //Firefox / chrome
var lines = e.stack.split('\n');
for (var i = 0, len = lines.length; i < len; i++) {
callstack.push(lines[i]);
}
//Remove call to logStackTrace()
callstack.shift();
isCallstackPopulated = true;
}
else if (window.opera && e.message) { //Opera
var lines = e.message.split('\n');
for (var i = 0, len = lines.length; i < len; i++) {
if (lines[i].match(/^\s*[A-Za-z0-9\-_\$]+\(/)) {
var entry = lines[i];
//Append next line also since it has the file info
if (lines[i + 1]) {
entry += " at " + lines[i + 1];
i++;
}
callstack.push(entry);
}
}
//Remove call to logStackTrace()
callstack.shift();
isCallstackPopulated = true;
}
}
if (!isCallstackPopulated) { //IE and Safari
var currentFunction = arguments.callee.caller;
while (currentFunction) {
var fn = currentFunction.toString();
var fname = fn.substring(fn.indexOf("function") + 8, fn.indexOf("(")) || "anonymous";
callstack.push(fname);
currentFunction = currentFunction.caller;
}
}
if (levels) {
console.log(callstack.slice(0, levels).join('\n'));
}
else {
console.log(callstack.join('\n'));
}
};
Moderator's note: The code in this answer seems to also appear in this post from Eric Wenderlin's blog. The author of this answer claims it as his own code, though, written prior to the blog post linked here. Just for purposes of good-faith, I've added the link to the post and this note.
I accomplished this without firebug. Tested in both chrome and firefox:
console.error("I'm debugging this code.");
Once your program prints that to the console, you can click the little arrow to it to expand the call stack.
Try stepping through your code one line or one function at a time to determine where it stops working correctly. Or make some reasonable guesses and scatter logging statements through your code.
Try this:
console.trace()
I don't know if it's supported on all browsers, so I would check if it exists first.