Javascript Alert Box - Not Working.

DarkLight's picture

He has: 274 posts

Joined: Oct 2007

Hi there.

I have written a small code that will call an alert box if it detects Internet Explorer. Trouble is, it don't work Sad

Here is my code... Can someone please point me in the right direction?

<script type="text/javascript">
var browserName=navigator.appName;
if (browserName=="Microsoft Internet Explorer")
{

var answer = confirm("Browser Incompatible - Firefox Needed")
if (answer){
alert("You won't be disappointed!'")
window.location = "http://www.mozilla-europe.org/en/products/firefox/";
}
else{
alert("Some elements of this Website will not be displayed correctly.");

}
</script>

For all your music needs, there's SongRepo!

pr0gr4mm3r's picture

He has: 1,507 posts

Joined: Sep 2006

It's missing a closing brace and some semicolons. I copied in a revision that works under Firefox. I replaced if (browserName=="Microsoft Internet Explorer") to if (true) and it showed the appropriate alert on my browser. I don't have IE to fully test it though.

<script type="text/javascript">
var browserName=navigator.appName;
if (browserName=="Microsoft Internet Explorer")
{
var answer = confirm("Browser Incompatible - Firefox Needed");
if (answer)
{
alert("You won't be disappointed!'");
window.location = "http://www.mozilla-europe.org/en/products/firefox/";
}
else
{
alert("Some elements of this Website will not be displayed correctly.");
}
}
</script>

DarkLight's picture

He has: 274 posts

Joined: Oct 2007

Aha! This works great. Thank you so much.

He has: 630 posts

Joined: May 2007

Yes - that will work most of the time. But be aware that some browsers, such as Opera, can fake the navigator string. I do it so that I can get into my bank account, which normally bans Opera.

You may be better off using Microsoft's Conditional Comments in your script.

Cordially, David
--
delete from internet where user_agent="MSIE" and version < 8;

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.