0

(UPDATED) I am trying to make a certain div show when the user's browser is detected. I found the code online and am not sure if it even works or how to put a div in the certain browser sections of the code.

<script>
   function BrowserDetection() {
  if(!navigator || !navigator.userAgent) {
    // Insert condition for old browsers
  }
  else if (navigator.userAgent.search("MSIE") >= 0) {
    // Insert conditional IE code here
  }
  else if (navigator.userAgent.search("Chrome") >= 0) {
    alert('code');
  }
  else if (navigator.userAgent.search("Firefox") >= 0) {
    // Insert conditional Firefox Code here
  }
  //Check if browser is Safari
  else if (navigator.userAgent.search("Safari") >= 0 && navigator.userAgent.search("Chrome") < 0) {
    // Insert conditional Safari code here
  }
  //Check if browser is Opera
  else if (navigator.userAgent.search("Opera") >= 0) {
    // Insert conditional Opera code here
  }
}
</script>

This one does not seem to work. Would anyone know where I could possibly find a working code. Thank you very much..

  • @byxor tried to fix the code, but Chrome was `& lt; – Mr. Polywhirl Oct 11 '19 at 15:09
  • Could you share where you found the code? And can you share the errors you are getting in your console when you run this code? – Mr. Polywhirl Oct 11 '19 at 15:09
  • If I were you I'd look at [browser detection](https://stackoverflow.com/a/14862119/5601284) and [appendChild](https://www.w3schools.com/jsref/met_node_appendchild.asp) (for inserting elements into the webpage) – byxor Oct 11 '19 at 15:11
  • The errors https://repl.it/repls/ThoseAuthenticComputergames And the code https://www.learningjquery.com/2017/05/how-to-use-javascript-to-detect-browser – jessie williams Oct 11 '19 at 15:20
  • You're trying to run the javascript code through a java compiler. They're 2 different languages. – byxor Oct 11 '19 at 15:22
  • https://repl.it/repls/FrighteningDearestSquare – jessie williams Oct 11 '19 at 15:24
  • You can't use repl.it for this. It runs NodeJS on a server, rather than running the javascript in your browser. – byxor Oct 11 '19 at 15:25

3 Answers3

0

basically, there is a window.navigator object in browsers that gives information about the browser.

The above code should be updated to check if this navigator object exists before trying to access the userAgent.

ALSO, &GT; is not a valid comparison operator, replace it with >=

function BrowserDetection() {
  if(!navigator || !navigator.userAgent) {
    // Insert condition for old browsers
  }
  else if (navigator.userAgent.search("MSIE") >= 0) {
    // Insert conditional IE code here
  }
  else if (navigator.userAgent.search("Chrome") >= 0) {
    // Insert conditional Chrome code here
  }
  else if (navigator.userAgent.search("Firefox") >= 0) {
    // Insert conditional Firefox Code here
  }
  //Check if browser is Safari
  else if (navigator.userAgent.search("Safari") >= 0 && navigator.userAgent.search("Chrome") < 0) {
    // Insert conditional Safari code here
  }
  //Check if browser is Opera
  else if (navigator.userAgent.search("Opera") >= 0) {
    // Insert conditional Opera code here
  }
}
Dhananjai Pai
  • 5,397
  • 1
  • 6
  • 23
0

Inspect it using F12 and check if you can see that div in your sources code. Then add debugger to it and try checking your functionality.

0

This one has only 1 error. But I do not know javascript so I do not understand whre the error is.

function BrowserDetection() {
  if(!navigator || !navigator.userAgent) {
    // Insert condition for old browsers
  }
  else if (navigator.userAgent.search("MSIE") >= 0) {
    // Insert conditional IE code here
  }
  else if (navigator.userAgent.search("Chrome") >= 0) {
    // Insert conditional Chrome code here
  }
  else if (navigator.userAgent.search("Firefox") >= 0) {
    // Insert conditional Firefox Code here
  }
  //Check if browser is Safari
  else if (navigator.userAgent.search("Safari") >= 0 && navigator.userAgent.search("Chrome") < 0) {
    // Insert conditional Safari code here
  }
  //Check if browser is Opera
  else if (navigator.userAgent.search("Opera") >= 0) {
    // Insert conditional Opera code here
  }
}
  • Can you add this to your original question? **Edit:** I added your text above to the original post, so people can see it. – Mr. Polywhirl Oct 11 '19 at 15:15