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

Responsive JS inside template

Discussion in 'XenForo Development Discussions' started by dbembibre, Apr 10, 2014.

  1. dbembibre

    dbembibre Active Member

    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>
     
  2. Chris D

    Chris D XenForo Developer Staff Member

    That should work.
     
    dbembibre likes this.
  3. Milano

    Milano Well-Known Member

    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>
     
    Chris D and dbembibre like this.
  4. dbembibre

    dbembibre Active Member

Share This Page