天天看點

Javascript彈出視窗總結

//關閉,父視窗彈出對話框,子視窗直接關閉

this.Response.Write("<script language=javascript>window.close();</script>");

//關閉,父視窗和子視窗都不彈出對話框,直接關閉

this.Response.Write("<script>");

this.Response.Write("{top.opener =null;top.close();}");

this.Response.Write("</script>");

//彈出視窗重新整理目前頁面width=200 height=200菜單。菜單欄,工具條,位址欄,狀态欄全沒有

this.Response.Write("<script language=javascript>window.open('rows.aspx','newwindow','width=200,height=200')</script>");

//彈出視窗重新整理目前頁面

this.Response.Write("<script language=javascript>window.open('rows.aspx')</script>");

this.Response.Write("<script>window.open('WebForm2.aspx','_blank');</script>");

//彈出提示視窗跳到webform2.aspx頁(在一個IE視窗中)

this.Response.Write(" <script language=javascript& gt;alert('注冊成功');window.window.location.href='WebForm2.aspx';< /script> ");

//關閉目前子視窗,重新整理父視窗

this.Response.Write("<script>window.opener.location.href=window.opener.location.href;window.close();</script>");

this.Response.Write("<script>window.opener.location.replace(window.opener.document.referrer);window.close();</script>");

//子視窗重新整理父視窗

this.Response.Write("<script>window.opener.location.href=window.opener.location.href;</script>");

this.Response.Write("<script>window.opener.location.href='WebForm1.aspx';</script>");

//彈出提示視窗.确定後彈出子視窗(WebForm2.aspx)

this.Response.Write("<script language='javascript'>alert('發表成功!');window.open('WebForm2.aspx')</script>");

//彈出提示視窗,确定後,重新整理父視窗

this.Response.Write("<script>alert('發表成功!');window.opener.location.href=window.opener.location.href;</script>");

//彈出相同的一頁

<INPUT type="button" value="Button" οnclick="javascript:window.open(window.location.href)">

//

Response.Write("parent.mainFrameBottom.location.href='yourwebform.aspx?temp=" +str+"';");

<SCRIPT LANGUAGE="javascript"> 

<!-- 

window.open ('page.html', 'newwindow', 'height=100, width=400, top=0, left=0, toolbar=no, menubar=no, scrollbars=no, resizable=no,location=n o, status=no') // 這句要寫成一行

--> 

</SCRIPT>  

  

  參數解釋: 

  

  <SCRIPT LANGUAGE="javascript"> js腳本開始; 

  window.open 彈出新視窗的指令; 

  'page.html' 彈出視窗的檔案名; 

  'newwindow' 彈出視窗的名字(不是檔案名),非必須,可用空''代替; 

  height=100 視窗高度; 

  width=400 視窗寬度; 

  top=0 視窗距離螢幕上方的象素值; 

  left=0 視窗距離螢幕左側的象素值; 

  toolbar=no 是否顯示工具欄,yes為顯示; 

  menubar,scrollbars 表示菜單欄和卷軸框。 

  resizable=no 是否允許改變視窗大小,yes為允許; 

  location=no 是否顯示位址欄,yes為允許; 

  status=no 是否顯示狀态欄内的資訊(通常是檔案已經打開),yes為允許; 

  </SCRIPT> js腳本結束

'newwin':隐藏菜單欄位址欄工具條

width=50:寬度

height=50:高度

scrollbars=yes/n滾動條

top=50:視窗距離螢幕上方

left=50:視窗距離螢幕左側

例:window.open('detail.aspx?ID="+e.Item.Cells[1].Text+"','newwin','width=750,height=600,scrollbars=yes,top=50,left=50');"); 

this.Response.Write("<Script>window.open('WebForm2.aspx','','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=yes,width=750,height=470,left=80,top=40');</script>");

例:

this.Response.Write("<script>alert('發表成功!');window.opener.location.href=window.opener.location.href;</script>");

this.Response.Write("<script>");

this.Response.Write("{top.opener =null;top.close();}");

this.Response.Write("</script>");

例: linkcolumn1.DataNavigateUrlFormatString="javascript:varwin=window.open('edit_usr.aspx?actid={0}','newwin','width=750,height=600,scrollbars=yes,top=50,left=50');window.close()"; 

1、最基本的彈出視窗代碼

  其實代碼非常簡單:

  < SCRIPT LANGUAGE="javascript">

  < !--

  window.open ("page.html")

  --

>

  < /SCRIPT>

  因為這是一段Javascript代碼,是以它們應該放在< SCRIPT LANGUAGE="javascript">之間。 < !-- 和 -->是對一些版本低的浏覽器起作用,在這些老浏覽器中不會将标簽中的代碼作為文本顯示出來。要養成這個好習慣啊。

  window.open ("page.html") 用于控制彈出新的視窗page.html,如果page.html不與主視窗在同一路徑下,前面應寫明路徑,絕對路徑(http://)和相對路徑(../)均可。

  用單引号和雙引号都可以,隻是不要混用。

  這一段代碼可以加入HTML的任意位置,< head>和< /head>之間可以,< body>間< /body>也可以,越前越早執行,尤其是頁面代碼長,又想使頁面早點彈出就盡量往前放。 也可以,越前越早執行,尤其是頁面代碼長,又想使頁面早點彈出就盡量往前放。

  2、經過設定後的彈出視窗

  下面再說一說彈出視窗的設定。隻要再往上面的代碼中加一點東西就可以了。我們來定制這個彈出的視窗的外觀,尺寸大小,彈出的位置以适應該頁面的具體情況。

  < SCRIPT LANGUAGE="javascript">

  < !--

  window.open ("page.html", "newwindow", "height=100, width=400, top=0, left=0, toolbar=no, menubar=no, scrollbars=no, resizable=no,location=no, status=no")

  //寫成一行

  -->

  < /SCRIPT>

  參數解釋:

  < SCRIPT LANGUAGE="javascript"> js腳本開始;

  window.open 彈出新視窗的指令;

  "page.html" 彈出視窗的檔案名;

  "newwindow" 彈出視窗的名字(不是檔案名),非必須,可用空"代替;

  height=100 視窗高度;

  width=400 視窗寬度;

  top=0 視窗距離螢幕上方的象素值;

  left=0 視窗距離螢幕左側的象素值;

  toolbar=no 是否顯示工具欄,yes為顯示;

  menubar,scrollbars 表示菜單欄和卷軸框。

  resizable=no 是否允許改變視窗大小,yes為允許;

  location=no 是否顯示位址欄,yes為允許;

  status=no 是否顯示狀态欄内的資訊(通常是檔案已經打開),yes為允許;

  < /SCRIPT> js腳本結束

  3、用函數控制彈出視窗

  下面是一個完整的代碼:

  < html>

  < head>

  < script LANGUAGE="JavaScript">

  < !--

  function openwin() {

  window.open ("page.html", "newwindow", "height=100, width=400, toolbar=no, menubar=no, scrollbars=no, resizable=no, location=no, status=no")

  //寫成一行

  }

  //-->

  < /script>

  < /head>

  < body οnlοad="openwin()">

  ...任意的頁面内容...

  < /body>

  < /html>

  這裡定義了一個函數openwin(),函數内容就是打開一個視窗。在調用它之前沒有任何用途。

  怎麼調用呢?

  方法一:< body οnlοad="openwin()"> 浏覽器讀頁面時彈出視窗;

  方法二:< body οnunlοad="openwin()"> 浏覽器離開頁面時彈出視窗;

  方法三:用一個連接配接調用:< a href="#" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" οnclick="openwin()">打開一個視窗< /a>

  注意:使用的"#"是虛連接配接。

  方法四:用一個按鈕調用:< input type="button" οnclick="openwin()" value="打開視窗">

  4、同時彈出2個視窗

  對源代碼稍微改動一下:

  < script LANGUAGE="JavaScript">

  < !--

  function openwin() {

  window.open ("page.html", "newwindow", "height=100, width=100, top=0, left=0,toolbar=no, menubar=no, scrollbars=no, resizable=no, location=no, status=no")

  //寫成一行

  window.open ("page2.html", "newwindow2", "height=100, width=100, top=100, left=100,toolbar=no, menubar=no, scrollbars=no, resizable=no, location=no, status=no")

  //寫成一行

  }

  //-->

  < /script>

  為避免彈出的2個視窗覆寫,用top和left控制一下彈出的位置不要互相覆寫即可。最後用上面說過的四種方法調用即可。

  注意:2個視窗的name(newwindows和newwindow2)不要相同,或者幹脆全部為空。OK?

  5、主視窗打開檔案1.htm,同時彈出小視窗page.html

  如下代碼加入主視窗< head>區:

  < script language="javascript">

  < !--

  function openwin() {

  window.open("page.html","","width=200,height=200")

  }

  //-->

  < /script>

  加入< body>區:

  < a href="1.htm" target="_blank" rel="external nofollow" οnclick="openwin()">open< /a>即可

  6、彈出的視窗之定時關閉控制

  下面我們再對彈出的視窗進行一些控制,效果就更好了。如果我們再将一小段代碼加入彈出的頁面(注意是加入到page.html的HTML中,可不是首頁面中,否則...),讓它10秒後自動關閉是不是更酷了?

  首先,将如下代碼加入page.html檔案的< head>區:

  < script language="JavaScript">

  function closeit() {

  setTimeout("self.close()",10000) //毫秒

  }

  < /script>

  然後,再用< body οnlοad="closeit()"> 這一句話代替page.html中原有的< BODY>這一句就可以了。(這一句話千萬不要忘記寫啊!這一句的作用是調用關閉視窗的代碼,10秒鐘後就自行關閉該視窗。)

  7、在彈出視窗中加上一個關閉按鈕

  < FORM>

  < INPUT TYPE="BUTTON" VALUE="關閉" onClick="window.close()">

  < /FORM>

  呵呵,現在更加完美了!

  8、内包含的彈出視窗---一個頁面兩個視窗

  上面的例子都包含兩個視窗,一個是主視窗,另一個是彈出的小視窗。通過下面的例子,你可以在一個頁面内完成上面的效果。

  < html>

  < head>

  < SCRIPT LANGUAGE="JavaScript">

  function openwin()

  {

  OpenWindow=window.open("", "newwin", "height=250, width=250,toolbar=no,scrollbars="+scroll+",menubar=no");

  //寫成一行

  OpenWindow.document.write("< TITLE>例子< /TITLE>")

  OpenWindow.document.write("< BODY BGCOLOR=#ffffff>")

  OpenWindow.document.write("< h1>Hello!< /h1>")

  OpenWindow.document.write("New window opened!")

  OpenWindow.document.write("< /BODY>")

  OpenWindow.document.write("< /HTML>")

  OpenWindow.document.close()

  }

  < /SCRIPT>

  < /head>

  < body>

  < a href="#" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" οnclick="openwin()">打開一個視窗< /a>

  < input type="button" οnclick="openwin()" value="打開視窗">

  < /body>

  < /html>

  看看OpenWindow.document.write()裡面的代碼不就是标準的HTML嗎?隻要按照格式寫更多的行即可。千萬注意多一個标簽或少一個标簽就會出現錯誤。記得用OpenWindow.document.close()結束啊。

  9、終極應用--彈出的視窗之Cookie控制

  回想一下,上面的彈出視窗雖然酷,但是有一點小毛病(沉浸在喜悅之中,一定沒有發現吧?)比如你将上面的腳本放在一個需要頻繁經過的頁面裡(例如首頁),那麼每次重新整理這個頁面,視窗都會彈出一次,是不是非常煩人?:-(

  有解決的辦法嗎?當然有!我們使用cookie來控制一下就可以了。首先,将如下代碼加入首頁面HTML的< HEAD>區:

  < script>

  function openwin(){

  window.open("page.html","","width=200,height=200")

  }

  function get_cookie(Name) {

  var search = Name + "=" var returnvalue = "";

  if (document.cookie.length > 0) {

  offset = document.cookie.indexOf(search)

  if (offset != -1) {

  offset += search.length

  end = document.cookie.indexOf(";", offset);

  if (end == -1)

  end = document.cookie.length;

  returnvalue=unescape(document.cookie.substring(offset, end))

  }

  }

  return returnvalue;

  }

  function loadpopup(){

  if (get_cookie("popped")=="){

  openwin()

  document.cookie="popped=yes"

  }

  }

  < /script>

  然後,用< body οnlοad="loadpopup()">(注意不是openwin而是loadpop啊!)替換首頁面中原有的< BODY>這一句即可。你可以試着重新整理一下這個頁面或重新進入該頁面,視窗再也不會彈出了。真正的Pop-Only-Once!

  寫到這裡彈出視窗的制作和應用技巧基本上算是完成了,俺也累壞了,一口氣說了這麼多,希望對正在制作網頁的朋友有所幫助俺就非常欣慰了。

  需要注意的是,JS腳本中的的大小寫最好前後保持一緻。

繼續閱讀