Jump to content

Need to format and calculate in alert


Ron Brinkman

Recommended Posts

<!DOCTYPE html>
<html>
  <head>
  </head>
  <body onload="noticeFunction()">
  
  <script>
  function noticeFunction() {
    let size = 800;
    //let size = min(100, 700);
    //let size = min(window.innerWidth, window.innerHeight);
    if (size < 900) {
      alert("<b>Notice</b>\nYour device screen size is " + size + "px");
    }
  }
  </script>

<p>Hello World</p>

  </body>
</html>

The function as written above works, but:

  1. I would like to use formatting (bold text).  The alert just renders the literal text.  Do alerts not permit formatting?
  2. I need to replace the hard coded value (size) with the detected window size.  If either of the commented lines is uncommented no alert is displayed.

Best Regards,

Ron Brinkman

Link to comment
Share on other sites

You can't have formatting in an alert() box.

For the min() function to work you need to prefix it with Math.

let size = Mahth.min(window.innerWidth, window.innerHeight);

If code isn't working, you should check the developer tools for error messages which can help you figure out where the problem is. In most browsers, pressing F12 on the keyboard will open the developer tools.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...