• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

Responsive JS inside template

dbembibre

Active member
#1
I need to load a external js file only in web mode.
I did this inside a new template, is working fine, but i dont know if is the good way.

Code:
<script>
    width = document.documentElement.clientWidth;
    if (width > 750)
    {
            <xen:if is="!{xen:helper ismemberof, $visitor, 19,4,3,5,20,25}">
                var script = document.createElement('script');
                script.type = 'text/javascript';
                script.src = 'http://web.com/show.jsp?id=624449&cor=002E0';
                document.getElementsByTagName('head')[0].appendChild(script); 
            </xen:if>
    }
</script>
 

Milano

Well-known member
#3
That should work but you can replace your script insert part with a single line like this

Code:
<script>
    width = document.documentElement.clientWidth;
    if (width > 750)
    {
            <xen:if is="!{xen:helper ismemberof, $visitor, 19,4,3,5,20,25}">
                  XenForo.loadJs('http://web.com/show.jsp?id=624449&cor=002E0');
            </xen:if>
    }
</script>