Javascript Alert Box - Not Working.
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
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>
All the best news here: https://newsbotnet.com
pr0gr4mm3r posted this at 03:47 — 30th April 2008.
He has: 1,502 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 posted this at 12:01 — 30th April 2008.
He has: 287 posts
Joined: Oct 2007
Aha! This works great. Thank you so much.
webwiz posted this at 16:38 — 30th April 2008.
He has: 629 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.