努力學習 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 飛仙 閱讀(6788) 評論(0)  編輯  收藏 所屬分類: Javascript

          主站蜘蛛池模板: 元朗区| 保德县| 东明县| 临夏县| 藁城市| 长丰县| 滦南县| 永州市| 华蓥市| 内黄县| 龙胜| 洱源县| 宝兴县| 潮安县| 缙云县| 师宗县| 鄱阳县| 报价| 得荣县| 巴彦淖尔市| 西峡县| 伊宁县| 普陀区| 贵州省| 镇赉县| 开封县| 从化市| 于田县| 长阳| 裕民县| 类乌齐县| 司法| 台中县| 元江| 怀来县| 肃南| 泸溪县| 治县。| 昌邑市| 抚远县| 柞水县|