简体   繁体   中英

How to use if-else option in JSTL when the condition returned by javascript

How to use if else in JSTL when the condition is a variable returned by javascript code

I need to add some Tags HTML when the media is Tablet or ipad and another Tag Html otherwise.

 <c:choose>
   <c:when test="${TabletMedia}">
      // Tags HTML
   </c:when>

   <c:otherwise>
     // other Tags HTML
   </c:otherwise>
  </c:choose>

function TabletteMedia() {

    if (navigator.userAgent.match(/(android|iphone|ipad|blackberry|symbian|symbianos|symbos|netfront|model-orange|javaplatform|iemobile|windows phone|samsung|htc|opera mobile|opera mobi|opera mini|presto|huawei|blazer|bolt|doris|fennec|gobrowser|iris|maemo browser|mib|cldc|minimo|semc-browser|skyfire|teashark|teleca|uzard|uzardweb|meego|nokia|bb10|playbook)/gi))
    {
        if (((screen.width >= 480) && (screen.height >= 800)) || ((screen.width >= 800) && (screen.height >= 480)) || navigator.userAgent.match(/ipad/gi))
        {
            return true;
        }
    }
    return false;

}

TabletMedia = TabletteMedia();

You cannot do this. The JSP page along with the JSTL is compiled into a class and that has nothing to do with running any JavaScript. JSTL is run on server side, when it decides what kind of static content (scripts and HTML) it returns to the client. You can use JSTL to condition things that are added either into JavaScript or HTML, but you cannot call JavaScript from it, since scripts are run in the client, ie browser, when the page is loaded, when all the static content has already been returned by the server.

If your condition is only available at page load, then you'll have to change the page structure with JavaScript, not JSTL tags.

You can't

Because

  • Javascript is client side Technology &
  • JSTL is server side Technology

For more

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM