简体   繁体   中英

Is there any way to get a JavaScript variable value into something JSTL can use?

I am painfully aware that the JSP that contains the JSTL is running server side, while the JavaScript runs client side.

I am trying to detect if a user is visiting a website from China. We are already doing Geolocation based on IP with a cookie, and I can access that value with the following JS:

<script>
        XY.isChina = document.cookie.indexOf('GeoIPCookie=GEOIP_COUNTRY_CODE=CN') > -1;
</script>

(XY is an existing JS structure we already have floating around. Good for global stuff.)

After this, I now have a true or false variable. I have verified that I can access this JS variable in other JSP files since I wrote it in the header, which is obviously included on every page.

What I'd really like to do now is something along these lines:

<c:if test="${not hideFacebookIcon && XY.isChina === false}" >

That's not gonna work, since that's a JavaScript variable.

Does anyone have any thoughts on how I might be able to hack up something to hold that true or false variable in so I can use it similar to the above? I'm not fussy about the method used, but I can't use extra libraries like jQuery or stuff like AJAX.

The way to do this in JSTL is like so:

<c:if test="${fn:indexOf(cookie.GeoIPCookie.value,'CN') > -1}">
    //do stuff
</c:if>

However, that doesn't work when a website is behind Akamai, so the solution instead has to be purely JavaScript

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