English 中文(简体)
Activate <div> based on User Agent
原标题:

I need to activate a "div" based on the user agent string. For example, (and this is my actual use for this,) A user with Firefox comes to my site. I want to tell them something along the lines of "Welcome Firefox User! Thank you for supporting Open Source Software, etc;".

That message is inside a Div. I would like to just show it for firefox users. It has to be compatible with most modern (mainstream) browsers. I am using PHP.

Thank you in advance.

Artur

最佳回答

On server-side with PHP, you can inspect the user s browser string with the built-in $_SERVER[ HTTP_USER_AGENT ] variable. Then, based on its value, you can output a DIV.

问题回答

Try this:

<?php if(strpos($_SERVER["HTTP_USER_AGENT"], "Firefox") !== FALSE) { ?>
  <div>Thanks for supporting Firefox!</div>
<?php } ?>

Not very reliable, though. I answered just so I could do the conditional-drop-out-of-PHP thing, which I abhor.

You can use server side detection, but as you ve not specified what language you are using, I can only suggest a frontend (javascript) method.

jQuery has a utility function jQuery.browser() which will return one of a number of "flags" based on the useragent string. Take a look at the docs at the link below for more information.

jQuery.browser() @ jQuery Docs





相关问题
CSS working only in Firefox

I am trying to create a search text-field like on the Apple website. The HTML looks like this: <div class="frm-search"> <div> <input class="btn" type="image" src="http://www....

image changed but appears the same in browser

I m writing a php script to crop an image. The script overwrites the old image with the new one, but when I reload the page (which is supposed to pickup the new image) I still see the old one. ...

Firefox background image horizontal centering oddity

I am building some basic HTML code for a CMS. One of the page-related options in the CMS is "background image" and "stretch page width / height to background image width / height." so that with large ...

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签