Using jQuery 1.9.1 & IE8, but the page also needs to work in IE7. In code before the part that is causing the issue, I read some data in & build an array that is a SELECT statement. In the section of code where I have an issue, I am doing the following:
var found = $.map( mySelArr, function(val) {
return val.mySelID === zSelID ? val.mySelStatement: null;
});
I then reference it:
var selStmt = found[0];
(there will be only 1 returned, and I know it will be in the array).
In IE7, I saw that it is throwing an exception inside jquery.js. When I step through debug on it, I see that the found length is zero (in IE7). If I change the mode to IE8, everything works fine. But in IE7, nothing gets put into the found variable.
What am I doing wrong to not be able to get this array value out in IE7? Would appreciate any thoughts.
EDIT
mySelArr is an array of Select statements, similar to:
1,<select name='mySelID_1' id='plist' ><option selected='selected' disabled='disabled' value='0'>Select Action</option><option value="1">This one</option><option value="2">That one</option></select>
and so on.
EDIT 2
I may have stumbled on the issue.
The array that held the select statements was being populated correctly in IE8, but not in IE7.
I actually had 2 arrays, one being just a number, and the other being that same number + the Select statement. The array that had the index & select statement was built using:
arr1.push({ fld1:data1, fld2:selstmt})
The one with just numbers was built using:
arr2.push(arrndx)
The arr2 was the outer FOR, the arr1 was the inner FOR. I had been referring to the number index by using:
var z = arr2[x][0];
to get the number & then using that to loop thru arr1 to get everything matching it & building the Select statement. What I discovered was that IE7 was returning undefined in the line of code above, while IE8 was returning the number (Firefox also).
I changed 2 lines of code to fix the issue:
FROM arr2.push(arrndx)
TO arr2.push( {arrindex: arrndx} );
and
FROM arr2[x][0]
TO arr2[x].arrindex
and it works in both IE7 & IE8. IE7 didn't have a problem creating that array, or reading it - it just wasn't valid data in it because of how I attempted to reference the fields in it.
I'm not exactly sure why IE7 had a problem with it & IE8 didn't, but.... Also, the fix didn't appear to break anything else.
Related
I'm trying to create code that requires the least number of bytes and that works for all browsers including IE 7.
In this example, the program calls dosomething('x1') and dosomething('x2').
If I have code like this:
var items,item,index,count;
items=Array('x1','x2');
count=items.length;
for (index=0;index<count;index++){
item=items[index];
dosomething(item);
}
Could I reduce it to this and have it still function exactly the same in all browsers:
var a=Array('x1','x2'),c=a.length,i;
for (i=0;i<c;i++){
f(a[i]);
}
I understand I changed the variable names and calling function name but my goal is to use the least number of bytes possible in the code to make the code execute.
I'm just not sure if declaring a variable equal to a property of a value from a previous variable in the same list of declarations would actually return correct results.
In other words, does var a=Array('x1','x2'),c=a.length... work, or do I have to specifically do var a=Array('x1','x2');var c=a.length; to make it work in all browsers including IE 7?
This is what the Google Closure Compiler service returned:
var a,b,c,d;a=["x1","x2"];d=a.length;for(c=0;c<d;c++)b=a[c],dosomething(b);
You can find many different Javascript compressors online to automate the process you are hand coding now. Yet, it's always good to understand how they work as it helps to write code that is better compressed.
As for IE, you can test your code by changing the emulations settings in the IE debugger panel. Just press F12, click the Emulation tab, and adjust the document mode to 7 (IE7).
Hope this is enough to get you started in the right direction.
You can use Array.map from IE 9
var items = Array('x1','x2');
items.map(dosomething(item));
I have a <textarea> defined like this:
<textarea class="form-control notetext" id="{{this._id}}-notetext" name="notetext">{{this.text}}</textarea>
I use ajax to send data and load a partial webpage.
Then I try to find the <textarea> and list the results to the console.
The problem is that elements[0] is returning undefined in Internet Explorer 11 (haven't tried any other versions), but works fine in Chrome and Firefox. I have tried 3 variations which all give the same result. Is there a solution?
$.get('/page/' + Id + '/partial/', function(res) {
$('#sectional').html(res);
//variation 1
var elements = document.getElementsByClassName('notetext');
//variation 2
var elements = document.querySelectorAll('.notetext');
//variation 3
var elements = document.getElementsByTagName("textarea");
});
Also, please note that id is dynamically created, so I can't .getElementById (which would only return 1 result instead of an array). I saw a post somewhere that had a method to use regex on .getElementById, but it returned null as well.
...and it turned out that it was working in IE11 after all. I was originally using getElementsbyName which doesn't work properly in IE11 (it gets ID instead). So I changed it to the variations listed above, which all gave the same incorrect result. But after I quit and restarted the browser, the code worked fine. I'm not sure if I can change a setting somewhere in IE so this doesn't happen again?
When I evaluate the following javascript in Safari 6.0:
Array.indexOf([1,2,3],3)
It returns -1, essentially saying that 3 is not in the array!
But in Firefox it correctly returns 2.
I know that Array.indexOf is a relatively new function so I have code that tests if it is present and if not defines it. However it appears this function is built in to Safari.
Is this a bug, or am I doing something wrong?
Edit: This is actually a simplified version of the problem. What I am actually doing is trying to locate the index of a TD cell in a TR:
var tr = td.parentNode
var col = Array.indexOf(tr.cells,td)
tr.cells does not have an indexOf. But, using Array.indexOf I can treat it as such, at least in Firefox. In Safari this does not work.
I guess in this case I could actually use cellindex, but if that was not defined, how would I go about getting the index?
I'm wondering why you don't use it like intended:
[1,2,3].indexOf( 3 );
Unless it's an academic question. In that case it's actually a bug special Firefox implementation. If you want to use the Array.prototype version directly, you should go like
Array.prototype.indexOf.call([1,2,3], 3);
so I've been trying to implement this: http://www.fatihkadirakin.com/dev/jquerytag/
it's a really good tag plugin which allows you to type in facebook-like tags into input field....
unfortunately though it works in firefox and chrome, it doesn't seem to work in IE as the demo demonstrates...
has anyone ever got it to work in IE and if so what changes to the js file did you make
Older IE versions don't have the indexOf method on Array, so the plugin adds this method to the Array prototype.
Later in the code, the author loops through an array using for (index in tags) without any hasOwnProperty check. Since indexOf is not a builtin property in this case, "indexOf" is one of the values that index takes on. Since a string is expected, not a function, this fails badly.
Change the loop (starting at line 146) to
var index;
for (index = 0; index < tags.length; index++) {
var item = create_tag(tags[index]);
list.append(item);
}
and it works as expected.
I would like to know if there is anything wrong with the below statement.
document.getElementById(monthId).options[document.getElementById(monthId).selectedIndex].value
Am asking this because, sometimes it seems to work fine and the rest of the time, it throws up an error - Object doesn't support this property or method.
BTW, monthId is the clientID of the dropdown present in a gridview in an asp.net page.
Thanks!
If no value is selected in the dropdown list, selectedIndex would be -1.
It's hard to evaluate without some more code as context. But without sanity checks around this line of code I would expect it to fail with an index out of bounds type exception when there is no selected index.
I tend to error check when using getElementById. I would expect that that is where your problem is.
Try this, and then test it in a debugger, but I will put an alert in.
var elem = document.getElementById(monthId);
if (elem.options) {
options[document.getElementById(monthId).selectedIndex].value
} else {
alert("elem doesn't have an options property");
}
You may want to not assume that the value property exists either, and do the same basic thing as I did here.
Once you get it working smoothly, where you know what is going to happen, you can start to remove the unneeded variables and go back to your original line, but for debugging, it is simpler to have one operation on each line and use separate variables, so that the debugger can show you what is happening.
You may want to understand the difference between undefined and null, and there are various pages on this topic but this one isn't too bad.
http://weblogs.asp.net/bleroy/archive/2005/02/15/Three-common-mistakes-in-JavaScript-2F00-EcmaScript.aspx
You can debug your problem by adding a breakpoint to your code in IE development tools, Firebug, Opera dragonfly or Chrome development tools and check your values.
Or you could add alert statements to check your values. Personally i think the code goes awry when selectedIndex is -1 (selectedIndex = -1 would occur when nothing is selected).
Check for yourself:
alert(document.getElementById(monthId)); // Returns null if nothing is found
alert(document.getElementById(monthId).selectedIndex); // If the selectedIndex is below 0 it could cause your error
document.getElementById(monthId).options[document.getElementById(monthId).selectedIndex].value