html - Hide/Show element if IE - Javascript -
i working script pops alert
if user or isn't using ie.
instead of this, i'd show or hide div
element in page.
i have tried unsuccessfully here: http://fiddle.jshell.net/shhv1lx3/2/
working alert demo here: http://fiddle.jshell.net/shhv1lx3/3/
function getieversion() { var sagent = window.navigator.useragent; var idx = sagent.indexof("msie"); // if ie, return version number. if (idx > 0) return parseint(sagent.substring(idx+ 5, sagent.indexof(".", idx))); // if ie 11 updated user agent string. else if (!!navigator.useragent.match(/trident\/7\./)) return 11; else return 0; //it not ie } var e = document.getelementbyid(ie); var e2 = document.getelementbyid(chrome); if (getieversion() > 0) alert("this ie " + getieversion()); e.style.display = 'block'; e2.style.display = 'none'; else alert("this not ie."); e.style.display = 'none'; e2.style.display = 'block';
<div id="ie"> ie </div> <div id="chrome"> chrome </div>
you should use {}
when using if
/else
statements. optional when there 1 statement, mandatory when there multiple statements. highly suggest using {}
always, regardless of number of statements.
you need pass string getelementbyid()
.
function getieversion() { var sagent = window.navigator.useragent; var idx = sagent.indexof("msie"); // if ie, return version number. if (idx > 0){ return parseint(sagent.substring(idx+ 5, sagent.indexof(".", idx))); } // if ie 11 updated user agent string. else if (!!navigator.useragent.match(/trident\/7\./)){ return 11; } else{ return 0; //it not ie } } var e = document.getelementbyid('ie'); var e2 = document.getelementbyid('chrome'); if (getieversion() > 0){ alert("this ie " + getieversion()); e.style.display = 'block'; e2.style.display = 'none'; } else{ alert("this not ie."); e.style.display = 'none'; e2.style.display = 'block'; }
<div id="ie"> ie </div> <div id="chrome"> chrome </div>
Comments
Post a Comment