1

我已经制作了一个工作网页,但是当我尝试将 jscript 放入外部文件时,它不再被调用。我已经在我的标题中输入了代码以包含文件名,但仍然无法调用它。这是我的 jscript 请顺便帮我快速编辑一下我从这里获得了 isMobile 功能http://www.abeautifulsite.net/blog/2011/11/detecting-mobile-devices-with-javascript/

<script>        
    var isMobile = {
      Android: function() {
        return navigator.userAgent.match(/Android/i);
      },
      BlackBerry: function() {
        return navigator.userAgent.match(/BlackBerry/i);
      },
      iOS: function() {
        return navigator.userAgent.match(/iPhone|iPad|iPod/i);
      },
      Opera: function() {
        return navigator.userAgent.match(/Opera Mini/i);
      },
      Windows: function() {
        return navigator.userAgent.match(/IEMobile/i);
      },
      any: function() {
        return (isMobile.Android() || isMobile.BlackBerry() || isMobile.iOS() || isMobile.Opera() || isMobile.Windows());
      }
    };
    function load()
    {
      var w=((screen.availWidth)/50)*49;
      var h=(w/4)*3;
      if( isMobile.any() ){
        var t=w;
        w=h;
        h=t;

      }
      var a=document.getElementById('banner');
      a.style.width=w+'px';
      a.style.height=(h/4)+'px';
      var b=document.getElementById('main');
      b.style.width=w+'px';
      b.style.height=Math.round((h/7)*4)+'px';
    }
  </script>
4

1 回答 1

0

When referencing an external JavaScript file, its contents should not be surrounded by a <script> tag. The <script> tag in the main file is what informs the browser that the contents should be parsed as JavaScript. Remove the surrounding <script> tag from the external file.

Proper reference in the main file:

<script type='text/javascript' src='path/to/external.js'></script>

The language='JavaScript' attribute has been deprecated and is unnecessary. You may include type='text/javascript'.

The contents of the external file should contain only JavaScript code, not surrounding <script> tags as in:

// File external.js
alert('Look, no script tag!');
于 2012-10-21T21:36:28.170 回答