Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

javaacript button appear depending on browser

Options
  • 11-06-2007 12:58pm
    #1
    Registered Users Posts: 1,552 ✭✭✭


    I tried this
    Code:

    <script type="text/javascript">
    var browserName=navigator.appName;
    if(browserName="Microsoft Internet Explorer")
    {
    <input type="button" value="Close Window" onclick="javascript:window.opener='x';window.close();">
    }

    </script>



    It doesn't work.
    Wonder what I did wrong or can this be done?

    Edit/Delete Message


Comments

  • Registered Users Posts: 950 ✭✭✭jessy


    quinnd6 wrote:
    I tried this
    Code:

    <script type="text/javascript">
    var browserName=navigator.appName;
    if(browserName="Microsoft Internet Explorer")
    {
    <input type="button" value="Close Window" onclick="javascript:window.opener='x';window.close();">
    }

    </script>



    It doesn't work.
    Wonder what I did wrong or can this be done?

    Edit/Delete Message

    Have a look here http://www.javascripter.net/faq/browsern.htm


  • Registered Users Posts: 7,468 ✭✭✭Evil Phil


    You could also have a look at this: http://www.quirksmode.org/js/detect.html - this script may be a little top heavy for your needs.

    But I doubt your asking us about browser detection. You need to use document.write() to render the input button. This code throws an error for me but it does render the button. I'm probably missing something that you have.
    <script type="text/javascript">
    var browserName=navigator.appName;
    if(browserName="Microsoft Internet Explorer")
    {
        [b]document.write("<input type='button' value='Close Window' onclick='javascript:window.opener='x';window.close();'>");[/b]
    }
    
    </script>
    


  • Registered Users Posts: 1,552 ✭✭✭quinnd6


    I rewrote it in a function cos its tideier doing it that way I think.
    Only problem now is calling the function.

    Heres the function I put in the head
    <script type="text/javascript">
    	
    		var browserName=navigator.appName;
    		if(browserName="Netscape")
    		{
    			function browserButton()
    			{
    				document.write(browserName);
    				document.write("");
    			}
    		}
    		else if(browserName="Microsoft Internet Explorer")
    		{
    				function browserButton()
    				{
    						document.write(browserName);
    						document.write("<input type='button' value='Close Window' onclick='window.close();'>");
    				}
    		}
    	}
    	</script>
    

    So I tried calling it this way in the body in the form
    <script type = "text/javascript">
    
    			browserButton();
    
    	</script>
    
    but I guess thats wrong cos it detects the browser as being netscape even if its internet explorer

    ??


  • Registered Users Posts: 7,468 ✭✭✭Evil Phil


    Instead of navigator.appName try and search navigator.userAgent* for MSIE (ie7), Firefox and Opera. Don't have anything else installed on this machine. Follow either of the links quinnd6 or myself gave you above for examples on how to do this.

    *This may have fallen out of favour amoungst some developers.


  • Moderators, Politics Moderators Posts: 39,822 Mod ✭✭✭✭Seth Brundle


    quinnd6 wrote:
    So I tried calling it this way in the body in the form
    <script type = "text/javascript">
    
                browserButton();
    
        </script>
    
    but I guess thats wrong cos it detects the browser as being netscape even if its internet explorer

    ??
    The browserButton() function is inside an if-else block and I don't think it can be called directly. As the if-else block is outside of a function it will run as soon as the browser reads it.


  • Advertisement
  • Registered Users Posts: 950 ✭✭✭jessy


    quinnd6 wrote:
    but I guess thats wrong cos it detects the browser as being netscape even if its internet explorer

    ??

    is this not the problem here? "if(browserName="Netscape")"

    Assignment instead of equality?


  • Registered Users Posts: 1,127 ✭✭✭smcelhinney


    I dont get this? It seems very convoluted. This is the way you should call this function.
    <script type="text/javascript">
    function browserButton() {
    		var browserName=navigator.appName;
    		if(browserName=="Netscape")
    		{
    				document.write(browserName);
    				document.write("");
    		}
    		else if(browserName=="Microsoft Internet Explorer")
    		{
    				document.write(browserName);
    				document.write("<input type='button' value='Close Window' onclick='window.close();'>");
    		}
    }
    	</script>
    

    (taking into account that jessy is right about assignment v equality)


  • Registered Users Posts: 1,552 ✭✭✭quinnd6


    Thats grand yes I changed it since to look pretty much like that and tested in opera browser(used navigator.useragent to check for opera) also so its ok now.

    Of course the ideal thing would have been if firefox and opera and IE worked the same with the close window operation but they dont unfortunately so I guess its best to have a button for IE and no button for the rest of the browsers.

    thanks


Advertisement