问题
I have a html form that appears not to be functional in IE10. when click on go button debugging returns:"SCRIPT5007: Unable to get property 'value' of undefined or null reference" I have added this tag in my html page
<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE9" />.
html contains dropdown menu option for frequency in myform.After selecting any option in dropdown i am calling below javascript function.
function chooseFreqMenu(freqValues,freqTexts)
{
var cf = document.myform;
var freq1=document.myform.freq.value; // freq1="d"
alert(freq1);// displaying freq1 value is "d" (ex:selected 'd' in html dropdown)
// checking some condition for freqValues.
for(var i=4;i<freqValues.length;i++)
selectStr += '<option value="'+freqValues[i]+'">'+freqTexts[i]+'\n';
}
selectStr += '</select>';
// Assinging the selectedStr to innerHTML. After this statement i am getting empty for freq1 value.
//Its working fine in IE<10 browsers
document.all.freqSel.innerHTML = selectStr;
alert(freq1); // displaying freq1 value is empty.
}
Before sending myform to chooseFreqmenu, myform contains freq value="d"(assume i selected 'd' in dropdown) After the above function myform doesn't contain freq value. After the above function passing myform to buildQueryStr.
function buildQueryStr(myform)
{
var freq=myform.freq.value; //Getting an error SCRIPT5007: Unable to get property 'value' of undefined or null reference in this line
//some other fields.
}
How to fix this issue?
Any suggestions?? Thanks in Advance.
回答1:
I don't think IE 10 supports acessing elements with the myform.freq.value syntax anymore.
The standard way of accessing Elements (this is supported in all browsers including IE) is with the document.getElementById function
function buildQueryStr(myform) {
//var freq=myform.freq.value; //Getting an error SCRIPT5007: Unable to get property 'value' of undefined or null reference in this line
var freq=document.getElementById("freq").value;
//some other fields.
}
回答2:
First: you shouldn't be using document.all
, not in 2013 :) It's an old Microsoft extension and I think it's deprecated. Use getElementById.
I think there's something wrong in the way you create your selectStr. You're gonna use it as innerHTML for a Select so:
- it should NOT end with a
</select>
. The closing tag for an element is not part of its innerHTML afaik - you should have closing tags for your
<option>
elements
I'd change the following piece of code:
// checking some condition for freqValues.
for(var i=4;i<freqValues.length;i++)
selectStr += '<option value="'+freqValues[i]+'">'+freqTexts[i]+'\n';
}
selectStr += '</select>';
into this:
// checking some condition for freqValues.
for(var i=4;i<freqValues.length;i++)
selectStr += '<option value="'+freqValues[i]+'">'+freqTexts[i]+'</option>\n';
}
NOTE: I've read somewhere that using innerHTML on SELECT controls has a really browser-dependant behaviour, even when using correctly formatted code. I think a more robust solution would be using removeChild and addChild to dinamically remove and add options nodes .
回答3:
Issue is related to document mode , IE11 default document mode is IE7.
Using F12 in IE Emulation you can change document mode 8,9 and 11
You can use following tag to change document mode using programming.
For versions of Internet Explorer 8 and above, this:
<meta http-equiv="X-UA-Compatible" content="IE=9; IE=8; IE=7" />
Forces the browser to render as that particular version's standards. It is not supported for IE7 and below.
If you separate with semi-colon, it sets compatibility levels for different versions. For example:
<meta http-equiv="X-UA-Compatible" content="IE=7; IE=9" />
Renders IE7 and IE8 as IE7, but IE9 as IE9. It allows for different levels of backwards compatibility. In real life, though, you should only chose one of the options:
<meta http-equiv="X-UA-Compatible" content="IE=8" />
This allows for much easier testing and maintenance. Although generally the more useful version of this is using Emulate:
<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE8" />
For this:
<meta http-equiv="X-UA-Compatible" content="IE=Edge" />
It forces the browser the render at whatever the most recent version's standards are. Just like using the latest version of jQuery on Google's CDN, this is the most recent, but also can potentially break your code since its not a fixed version.
Last , consider adding this little tidbit:
<meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1" />
Adding "chrome=1" will allow the site to render in ChromeFrame for those (intelligent) users who have it, without affecting anyone else.
`enter code here
`https://technet.microsoft.com/en-us/itpro/internet-explorer/ie11-deploy-guide/fix-compat-issues-with-doc-modes-and-enterprise-mode-site-list
https://msdn.microsoft.com/library/ms533876(v=vs.85).aspx
http://www.chromium.org/developers/how-tos/chrome-frame-getting-started
来源:https://stackoverflow.com/questions/18124574/script5007-unable-to-get-property-value-of-undefined-or-null-reference