Apply javascript for mobile or desktop only - Stack Overflow

I have 2 scripts one that I would like to use for any screen width less than 990px and another for anyt

I have 2 scripts one that I would like to use for any screen width less than 990px and another for anything greater. These scripts e from a 3rd party and only work on the actual domain (so testing will be hard for anyone else).

Here are the scripts they gave me:

For mobile:

<script>
    (function(){
        var t = document.getElementsByTagName("script")[0];
        var s = document.createElement("script"); s.async = true;
        s.src = "//integration.nfusionsolutions.biz/client/jh/widget/module/accordionchart/nfaccordion";
        t.parentNode.insertBefore(s, t);
    })();
    </script>

For Desktop:

<script>
    (function(){
                var t = document.getElementsByTagName("script")[0];
                var s = document.createElement("script"); s.async = true;
                s.src = "//integration.nfusionsolutions.biz/client/jh/widget/module/spottableextended/nfspotextended";
                t.parentNode.insertBefore(s, t);
    })();
    </script>

I have tried this for the desktop view but I know something is off.

For Desktop:

 <script>

        (function(){
        var viewportWidth = $(window).width();
        if (viewportWidth > 900) {
                    var t = document.getElementsByTagName("script")[0];
                    var s = document.createElement("script"); s.async = true;
                    s.src = "//integration.nfusionsolutions.biz/client/jh/widget/module/spottableextended/nfspotextended";
                    t.parentNode.insertBefore(s, t);
       } })();
        </script>

Any suggestions?

UPDATE*** Code based on response below

    <script>
if ( $(window).width() > 990) {     
  (function(){
                var t = document.getElementsByTagName("script")[0];
                var s = document.createElement("script"); s.async = true;
                s.src = "//integration.nfusionsolutions.biz/client/jackhunt/widget/module/spottableextended/nfspotextended";
                t.parentNode.insertBefore(s, t);
    })();
}
else {
  (function(){
        var t = document.getElementsByTagName("script")[0];
        var s = document.createElement("script"); s.async = true;
        s.src = "//integration.nfusionsolutions.biz/client/jackhunt/widget/module/accordionchart/nfaccordion";
        t.parentNode.insertBefore(s, t);
    })();
}
</script>

I have 2 scripts one that I would like to use for any screen width less than 990px and another for anything greater. These scripts e from a 3rd party and only work on the actual domain (so testing will be hard for anyone else).

Here are the scripts they gave me:

For mobile:

<script>
    (function(){
        var t = document.getElementsByTagName("script")[0];
        var s = document.createElement("script"); s.async = true;
        s.src = "//integration.nfusionsolutions.biz/client/jh/widget/module/accordionchart/nfaccordion";
        t.parentNode.insertBefore(s, t);
    })();
    </script>

For Desktop:

<script>
    (function(){
                var t = document.getElementsByTagName("script")[0];
                var s = document.createElement("script"); s.async = true;
                s.src = "//integration.nfusionsolutions.biz/client/jh/widget/module/spottableextended/nfspotextended";
                t.parentNode.insertBefore(s, t);
    })();
    </script>

I have tried this for the desktop view but I know something is off.

For Desktop:

 <script>

        (function(){
        var viewportWidth = $(window).width();
        if (viewportWidth > 900) {
                    var t = document.getElementsByTagName("script")[0];
                    var s = document.createElement("script"); s.async = true;
                    s.src = "//integration.nfusionsolutions.biz/client/jh/widget/module/spottableextended/nfspotextended";
                    t.parentNode.insertBefore(s, t);
       } })();
        </script>

Any suggestions?

UPDATE*** Code based on response below

    <script>
if ( $(window).width() > 990) {     
  (function(){
                var t = document.getElementsByTagName("script")[0];
                var s = document.createElement("script"); s.async = true;
                s.src = "//integration.nfusionsolutions.biz/client/jackhunt/widget/module/spottableextended/nfspotextended";
                t.parentNode.insertBefore(s, t);
    })();
}
else {
  (function(){
        var t = document.getElementsByTagName("script")[0];
        var s = document.createElement("script"); s.async = true;
        s.src = "//integration.nfusionsolutions.biz/client/jackhunt/widget/module/accordionchart/nfaccordion";
        t.parentNode.insertBefore(s, t);
    })();
}
</script>
Share Improve this question edited Apr 4, 2016 at 14:25 Matt L asked Apr 4, 2016 at 14:18 Matt LMatt L 453 silver badges13 bronze badges 3
  • the site does not use jquery. – Matt L Commented Apr 4, 2016 at 14:21
  • If the site doesn't use jQuery you can't use a jQuery object such as $(window) and neither methods such as .width(). Get the size of the screen, current web page and browser window has an vanilla javascript method for getting the window size. Please note that this javascript is only executed ONCE and it checks the page width when the page LOADS - if you resize your window afterwards it won't reexecute the code. – h2ooooooo Commented Apr 4, 2016 at 14:28
  • I added jquery to load, this on in particular but it still does not work. <script src="ajax.googleapis./ajax/libs/jquery/2.2.2/…> – Matt L Commented Apr 4, 2016 at 14:29
Add a ment  | 

2 Answers 2

Reset to default 3

If you're not using jQuery, you can't use a jQuery method. Use the following condition instead:

if (window.innerWidth > 900) { // code for large screens

You can use sompething like this, if you have size screen informations :

if ( $(window).width() > 990) {     
  //Add your javascript for large screens here
}
else {
  //Add your javascript for small screens here
}

EDIT : if really jQuery can't be used, you can try to use :

window.innerWidth

to get the width. But it'll depend too of the resized window...!

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745502098a4630462.html

相关推荐

  • Apply javascript for mobile or desktop only - Stack Overflow

    I have 2 scripts one that I would like to use for any screen width less than 990px and another for anyt

    4小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信