Conditional IE statement - run script - javascript

I have a lame problem:
If I use something like:
<!--[if IE]>
Random text
<![endif]-->
and I open page in IE (9) I naturally see the text "Random text". Everything is ok.
If I use:
<!--[if IE]>
<script type="text/javascript">
alert("aaa);
</script>
<![endif]-->
Nothing happens.
I need to run a specific script for IE... can any1 help me with it?

You have a syntax error in your Javascript. Try this:
<!--[if IE]>
<script type="text/javascript">
alert("aaa");
</script>
<![endif]-->

Use the script to check if it's IE.
EDIT: actually check out some of the answers here, they're exactly what you want.
For example one person posted this solution which uses a very different approach
<script>runFancy = true;</script>
<!--[if IE]>
<script type="text/javascript">
runFancy = false;
</script> // <div>The HTML version for IE went here</div>
<![endif]-->

Related

Add Class on commented code or conditional code

Is possible manage conditional code by jQuery or javascript
<!--[if IE]>
<div class="ie">IE ALERT</div>
<![endif]-->
$('.ie').addClass('ie-additional-class');
The jsfiddle here.

Loading different versions of jquery depending on browser

I have a website which is running on Jquery 2.x. This is supported by IE 9+ and Chrome and Firefox. But I want the website to work with IE 8.
How can I unload Jquery 2.x and load 1.x for only IE 8 ?
I have tried using the following:
<!--[if lt IE 9]>
<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
<![endif]-->
But both versions will load in this case.
Try using a condition to check if the browser is IE8, like you did, and add a class to the <html> element:
<!--[if IE 8]> <html class="ie8"> <![endif]-->
<!--[if gt IE 8]><!--> <html> <!--<![endif]-->
Then, in your Javascript code, you can easily check if the <html> element has the class specified for older browsers and load the script dynamically inside your page's <head>, like so:
var head = document.getElementsByTagName('head')[0];
var script = document.createElement('script');
if(document.getElementsByTagName('html')[0].className == 'ie8')
script.src = "jquery_1.x.js";
else
script.src = "jquery_2.x.js";
script.type = 'text/javascript';
head.appendChild(script);
Note that this method might be quite slow, however.
You can add required jQuery version with following special conditional comments:
<!--[if lte IE 8]>
<script src="/js/jquery-1.12.4.min.js"></script>
<![endif]-->
<!--[if (gte IE 9)]><!-->
<script src="/js/jquery-3.3.1.min.js"></script>
<!--<![endif]-->
Note the additional <!--> just after the second if and its special closing <!--<![endif]-->. This will cause that modern browsers like as Chrome/FireFox don't consider it as comment and so include the new jQuery version.

disable a javascript only in IE7

I got a scenario where I need a JavaScript to load in all browsers except IE7. Any help is greatly appreciated. I've tried the below code:
<!--[if gt IE 7]>
<script type="text/javascript">
//Some script XXX to execute
</script>
<![endif]-->
<!--[if (gt IE 9)|!(IE)]><!-->
<script type="text/javascript">
//Some script XXX to execute
</script>
<!--<![endif]-->
I think this works too
<!--[if !(IE 7)]>
// tag here
<![endif]-->
You can also do a combination of
<![if !IE]>
// script tag
<![endif]>
<!--[if (gte IE 8)&(lt IE 7)]>
// script tag
<![endif]-->
see docs here http://msdn.microsoft.com/en-us/library/ms537512%28v=vs.85%29.aspx
I am not sure if you are looking to load an entire file or just a few lines of script. If it's only a few lines the jquery way is easier
Use navigator.userAgent to check the browser identification.
also see here
try this
if ( $.browser.msie == true && $.browser.version < 8) {
//code for ie 7
}
else
{
document.write("<script src=\"test.js\">");
}
Looks like you're already on the right lines with conditional comments
<!--[if !(IE 7)]>--><script>alert("I'm not IE7");</script><!--<![endif]-->
To any non-IE browser, this becomes
<!--[if !(IE 7)]>--> Comment
<script>alert("I'm not IE7");</script> Element
<!--<![endif]--> Comment
To any IE browser, this becomes
<!--[if !(IE 7)]> If not IE 7
--> (continued) AND Comment
<script>alert("I'm not IE7");</script> Element
<!--<![endif]--> Comment AND End if
From which, if it is IE 7, it gets snipped.
<!--[if !(IE 7)]> If - not rendered
SNIP
<![endif]--> End If
You could reduce this down if you don't mind invalid HTML
<![if !(IE 7)]><script>alert("I'm not IE7");</script><![endif]>

AngularJS Routing not working in IE7

I have been implementing Routing in my app following the tutorial
http://docs.angularjs.org/tutorial/step_07
I couldn't get my version to work in IE7, and after spending a while trying to work out what I have missed / done wrong I have noticed that the example doesn't work.
http://angular.github.com/angular-phonecat/step-7/app/
Anyone know how to get this to work?
OK I had the same problem so I started the bounty, but after that I found the working solution (for me at least):
Use HTML5 shim
Use JSON2.js
Add all these attributes to your html node:
class="ng-app:myapp" id="ng-app" ng-app="myapp" xmlns:ng="http://angularjs.org"
(where myapp is really your app name)
So to recap, here is my IE7/8/9 working HTML page:
<!DOCTYPE html>
<html lang="en" class="ng-app:myapp" id="ng-app" ng-app="myapp" xmlns:ng="http://angularjs.org">
<head>
<!--[if lt IE 9]>
<script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<!--[if lte IE 8]>
<script>
document.createElement('ng-include');
document.createElement('ng-pluralize');
document.createElement('ng-view');
document.createElement('ng:include');
document.createElement('ng:pluralize');
document.createElement('ng:view');
</script>
<![endif]-->
<!--[if lt IE 8]>
<script src="js/json2.js"></script>
<![endif]-->
</head>
<body>
<div ng-view></div>
</body>
</html>

How can i prevent ie8 from loading some javascripts in the head section?

I wonder if there is the possibilaty to load some javascript files only if its not the IE 8 like this:
<!--[if NOT IE 8]>
<script type="text/javascript" src="assets/scripts/slideshow.js"></script>
<![endif]-->
<!--[if IE 8]>
<script type="text/javascript" src="assets/scripts/slideshowOnlyForIE8.js"></script>
<![endif]-->
Try this (cited from here)...
<!--[if !IE 8]><!-->
<script type="text/javascript" src="assets/scripts/slideshow.js"></script>
<!--<![endif]-->
<!--[if IE 8]>
<script type="text/javascript" src="assets/scripts/slideshowOnlyForIE8.js"></script>
<![endif]-->
I assume that you are either limited by a feature that IE8 lacks or taking advantage of an IE8 specific feature. In this case, the best thing to do is test for that gating feature.
Basically, combine the two scripts into one, and kick it off with a simple if test to see if your feature is available:
if(crucialIEFunction()) {
//run IE8 slideshow code
}
else
{
//run non IE slideshow code
}

Categories