努力學習 Java

          知之者不如好之者,好之者不如樂之者,樂之者不如癡之者

          外部引入js文件的當前路徑是html文件的當前路徑


          我的文件結構如下:
          ---current?? 這個文件夾放html文件
          ?????????????? ? |
          ???????????? ?? ?|----jscript?? 這個文件夾放javascript文件
          ???????????? ????|
          ??????????? ?? ? |----images??? 這個文件夾放圖片,有圖片icq01.gif、icq02.gif、google01.gif 等圖片


          ?? current/jscript/RandomBackgroundPicture.js 內容如下:

          /* *
          *??隨機顯示背景圖片
          */


          function ?setRandomBackground()? {
          ????
          var ?pic? = ? new ?Array( 8
          )?;
          ????pic[
          0 ]? = ? " ../images/icq01.gif "
          ?;
          ????pic[
          1 ]? = ? " ../images/icq02.gif "
          ?;
          ????pic[
          2 ]? = ? " ../images/icq03.gif "
          ?;
          ????pic[
          3 ]? = ? " ../images/icq04.gif "
          ?;
          ????pic[
          4 ]? = ? " ../images/icq05.gif "
          ?;
          ????pic[
          5 ]? = ? " ../images/google01.gif "
          ?;
          ????pic[
          6 ]? = ? " ../images/google02.gif "
          ?;
          ????pic[
          7 ]? = ? " ../images/google03.gif "
          ?;

          ????
          var ?index? = ?Math.floor(?Math.random() *
          pic.length?)?;
          ????document.writeln(
          " <body?background=\ "" ?+?pic[index]?+? " \ " > " ?)?;?? // ?

          }



          function ?setBackground()? {
          ????
          var ?pic? = ? new ?Array( 8
          )?;
          ????pic[
          0 ]? = ? " images/icq01.gif "
          ?;
          ????pic[
          1 ]? = ? " images/icq02.gif "
          ?;
          ????pic[
          2 ]? = ? " images/icq03.gif "
          ?;
          ????pic[
          3 ]? = ? " images/icq04.gif "
          ?;
          ????pic[
          4 ]? = ? " images/icq05.gif "
          ?;
          ????pic[
          5 ]? = ? " images/google01.gif "
          ?;
          ????pic[
          6 ]? = ? " images/google02.gif "
          ?;
          ????pic[
          7 ]? = ? " images/google03.gif "
          ?;

          ????
          var ?index? = ?Math.floor(?Math.random() *
          pic.length?)?;
          ????document.writeln(
          " <body?background=\ "" ?+?pic[index]?+? " \ " > "
          ?)?;??
          }
          ? // setBackground


          ?? current/index.html文件如下:

          < html >
          < head >
          < meta? name ="GENERATOR" ?content ="Microsoft?FrontPage?5.0" >
          < meta? name ="ProgId" ?content ="FrontPage.Editor.Document" >
          < meta? http-equiv ="Content-Type" ?content ="text/html;?charset=gb2312" >
          < title > 新建網頁?1 </ title >


          </ head >

          < script? language ="javascript" ?src ="jscript/RandomBackgroundPicture.js" ></ script >

          < script >
          setBackground()??
          // ?不能是?setRandomBackground()
          </ script >


          </ body >
          </ html >


          結論:html文件外部引入javascript時,javascript的當前路徑是html的當前路徑,而不是js文件的當前路徑。例如此例中,對于RandomBackgroundPicture.js本身而言,icq01.gif的相對路徑是"../images/icq01.gif",但這是不正確的;當它被index.html引入時,相對路徑是相對與index.html的路徑而言,所以應該是"images/icq01.gif"才對。

          ?

          posted on 2006-07-08 13:23 飛仙 閱讀(6790) 評論(0)  編輯  收藏 所屬分類: Javascript

          主站蜘蛛池模板: 桑日县| 神木县| 苏尼特左旗| 淮滨县| 茶陵县| 宣汉县| 虞城县| 江门市| 临西县| 竹山县| 周至县| 天祝| 朝阳县| 宝应县| 邵东县| 汨罗市| 临湘市| 龙口市| 淅川县| 沅陵县| 水城县| 建始县| 凭祥市| 汉川市| 肇源县| 张家口市| 安泽县| 手游| 县级市| 上虞市| 前郭尔| 荥经县| 航空| 长乐市| 桃园市| 兴宁市| 银川市| 天长市| 兖州市| 温宿县| 于田县|