在asp.net中如何在js中弹出对话框

JavaScript013

在asp.net中如何在js中弹出对话框,第1张

<html>

<head>

<meta http-equiv="content-type" content="text/htmlcharset=gb2312" />

<style>

body{font-size:12pxfont-family:verdanamargin:0padding:0background-color:#FAFDFF}

a { color:#0365BFtext-decoration: none}

a:hover { color:#f60text-decoration: underline}

a.addfile{background:url(images/others/addfile.gif) no-repeatdisplay:blockfloat:leftheight:20pxmargin-top:-1pxposition:relativetext-

decoration:nonetop:0ptwidth:80pxcursor:pointer}

a:hover.addfile{background:url(images/others/addfile.gif) no-repeatdisplay:blockfloat:leftheight:20pxmargin-top:-1pxposition:relativetext-

decoration:nonetop:0ptwidth:80pxcursor:pointer}

input.addfile{cursor:pointerheight:20pxleft:-10pxposition:absolutetop:0pxwidth:1pxfilter:alpha(opacity=0)opacity:0}

#dv_fileinput_list{font-size:12pxfont-family:verdana}

#dv_fileinput_msg{font-size:12pxfont-family:verdana}

</style>

<head>

<script language="javascript">

<!--

/*改进的上传界面 HxyMan 2008-1-22*/

var DvFileInput={

$:function(d){return document.getElementById(d)},

isFF:function(){var a=navigator.userAgentreturn a.indexOf('Gecko')!=-1&&!(a.indexOf('KHTML')>-1||a.indexOf('Konqueror')>-1||a.indexOf

('AppleWebKit')>-1)},

ae:function(o,t,h){if (o.addEventListener){o.addEventListener(t,h,false)}else if(o.attachEvent){o.attachEvent('on'+t,h)}else{try{o['on'+t]=h}

catch(e){}}},

count:0,

realcount:0,

uped:0,//今天已经上传个数

max:1,//还可以上传多少个

once:1,//最多能同时上传多少个

boardid:0,

uploadcode:0,

readme:'',

add:function(){

if (DvFileInput.chkre()){

DvFileInput_OnEcho('<font color=red><b>您已经添加过此文件了!</b></font>')

}

else if (DvFileInput.realcount>=DvFileInput.max){

DvFileInput_OnEcho('<font color=red><b>您最多只能上传'+DvFileInput.max+'个文件。</b></font>')

}else if (DvFileInput.realcount>=DvFileInput.once){

DvFileInput_OnEcho('<font color=red><b>您一次最多只能上传'+DvFileInput.once+'个文件。</b></font>')

}else{

DvFileInput_OnEcho('<font color=blue>可以继续添加附件,也可以立即上传。</font>')

var o=DvFileInput.$('dv_fileinput_'+DvFileInput.count)

++DvFileInput.count

++DvFileInput.realcount

DvFileInput_OnResize()

var oInput=document.createElement('input')

oInput.type='file'

oInput.id='dv_fileinput_'+DvFileInput.count

oInput.name='dv_fileinput_'+DvFileInput.count

oInput.size=1

oInput.className='addfile'

DvFileInput.ae(oInput,'change',function(){DvFileInput.add()})

o.parentNode.appendChild(oInput)

o.blur()

o.style.display='none'

DvFileInput.show()

}

},

chkre:function(){

var c=DvFileInput.$('dv_fileinput_'+DvFileInput.count).value

for (var i=DvFileInput.count-1i>=0--i){

var o=DvFileInput.$('dv_fileinput_'+i)

if (o&&o.value==c&&DvFileInput.realcount>0){return true}

}

return false

},

filename:function(u){

var p=u.lastIndexOf('\\')

return (p==-1?u:u.substr(p+1))

},

show:function(){

var oDiv=document.createElement('div')

var oBtn=document.createElement('img')

var i=DvFileInput.count-1

oBtn.id='dv_fileinput_btn_'+i

oBtn.src='images/others/filedel.gif'

oBtn.alt='删除'

oBtn.style.cursor='pointer'

var o=DvFileInput.$('dv_fileinput_'+i)

DvFileInput.ae(oBtn,'click',function(){

DvFileInput.remove(i)

})

oDiv.innerHTML='<img src="images/others/fileitem.gif" width="13" height="11" border="0" /><font color=gray>'+o.value+'</font>'

oDiv.appendChild(oBtn)

DvFileInput.$('dv_fileinput_show').appendChild(oDiv)

},

remove:function(i){

var oa=DvFileInput.$('dv_fileinput_'+i)

var ob=DvFileInput.$('dv_fileinput_btn_'+i)

if(oa&&i>0){oa.parentNode.removeChild(oa)}

if(ob){ob.parentNode.parentNode.removeChild(ob.parentNode)}

if(0==i){DvFileInput.$('dv_fileinput_0').disabled=true}

if(0==DvFileInput.realcount){DvFileInput.clear()}else{--DvFileInput.realcount}

DvFileInput_OnResize()

},

init:function(){

var a=document

a.writeln('<form id="dv_fileinput_form" name="dv_fileinput_form" action="savefile.asp?t=1&boardid='+DvFileInput.boardid+'" target="_self"

method="post" enctype="multipart/form-data" style="margin:0padding:0"><input type="hidden" id="UploadCode" name="UploadCode"

value="'+DvFileInput.uploadcode+'" /><div id="dv_fileinput_formarea"><img src="images/others/fileitem.gif" width=6 height=6 alt="点击文字添加附件" border="0"

/><a href="javascript:">添加附件<input id="dv_fileinput_0" name="dv_fileinput_0" class="addfile" size="1" type="file" onchange="DvFileInput.add()" /></a>

<span id="dv_fileinput_upbtn"><a href="javascript:DvFileInput.send()">上传附件</a></span><span id="dv_fileinput_msg"></span>

'+DvFileInput.readme+'</div></form></div><div id="dv_fileinput_show"></div>')

},

send:function(){

if (DvFileInput.realcount>0){

DvFileInput.$('dv_fileinput_'+DvFileInput.count).disabled=true

DvFileInput.$('dv_fileinput_upbtn').innerHTML='上传中,请稍等..'

DvFileInput.$('dv_fileinput_form').submit()

}else{

alert('请先添加附件再上传。')

}

},

clear:function(){

for (var i=DvFileInput.counti>0--i){

DvFileInput.remove(i)

}

DvFileInput.$('dv_fileinput_form').reset()

var o=DvFileInput.$('dv_fileinput_btn_0')

if(o){o.parentNode.parentNode.removeChild(o.parentNode)}

DvFileInput.$('dv_fileinput_0').disabled=false

DvFileInput.$('dv_fileinput_0').style.display=''

DvFileInput.count=0

DvFileInput.realcount=0

}

}

DvFileInput_OnResize=function(){

var o=parent.document.getElementById("ad")

(o.style||o).height=(parseInt(DvFileInput.realcount)*16+18)+'px'

}

DvFileInput_OnUpload=function(iFtype,sExt,sOldName,sShowName,iDownid){

try{

if ('1'==iFtype||'2'==iFtype){

parent.dvtextarea.insert('[upload='+sExt+','+sOldName+']'+sShowName+'[/upload]<br/>')

}else{

parent.dvtextarea.insert('[upload='+sExt+','+sOldName+']viewFile.asp?ID='+iDownid+'[/upload]<br/>')

}

parent.Dvform.upfilerename.value+=(iDownid+',')

}catch(e){}

}

DvFileInput_OnEcho=function(str){

DvFileInput.$('dv_fileinput_msg').innerHTML=str

}

DvFileInput_OnMsgSuc=function(str){

DvFileInput_OnEcho(str)

DvFileInput.clear()

}

DvFileInput_OnMsgFail=function(str){

DvFileInput_OnEcho(str)

DvFileInput.clear()

}

DvFileInput_OnUpdateRndCode=function(str){

DvFileInput.$('UploadCode').value=str

}

//-->

</script>

</head>

<body>

<script language="javascript">

<!--

DvFileInput.boardid='31'

DvFileInput.uploadcode='1442'

DvFileInput.uped=parseInt('0')

DvFileInput.max=parseInt('100')

DvFileInput.once=parseInt('50')

DvFileInput.readme='今天还可上传'+DvFileInput.max+'个<a style="CURSOR: help" title="论坛限制:一次'+DvFileInput.once+'个,一天100个,每个500K">(查看论坛限制)

</a>'

DvFileInput.init()

DvFileInput_OnResize()

//-->

</script>

</body>

</html>

【1、最基本的js弹出对话框窗口代码】

这是最基本的js弹出对话框,其实代码就几句非常简单:

复制代码代码如下:

<script LANGUAGE="javascript">

<!--

window.open ("page.html")

-->

</script>

因为这是一段javascripts代码,所以它们应该放在<script LANGUAGE="javascript">标签和</script>之间。<!-- 和-->是对一些版本低的浏览器起作用,在这些老浏览器中不会将标签中的代码作为文本显示出来。要养成这个好习惯啊。

window.open ("page.html") 用于控制弹出新的窗口page.html,如果page.html不与主窗口在同一路径下,前面应写明路径,绝对路径(http://)和相对路径(../)均可。用单引号和双引号都可以,只是不要混用。

这一段代码可以加入HTML的任意位置,<head>和</head>之间可以,<body>间</body>也可以,越前越早执行,尤其是页面代码长的页面,这种js弹出对话框想早点弹出就尽量往前放。

【2、增加属性设置的js弹出对话框代码】

下面再说一说js弹出对话框窗口属性的设置。只要再往上面的代码中加一点东西就可以了。

我们来定制这个js弹出对话框弹出的窗口的外观,尺寸大小,弹出的位置以适应该页面的具体情况。

复制代码代码如下:

View Code

<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、用函数控制js弹出对话框窗口】

下面是一个完整js弹出对话框的代码。

复制代码代码如下:

View Code

<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 onload="openwin()">

...任意的页面内容...

</body>

</html>

这里定义了一个函数openwin(),函数内容就是打开一个窗口。在调用它之前没有任何用途。

怎么调用呢?

方法一:浏览器读页面时弹出窗口;

复制代码代码如下:

<body onload="openwin()">

方法二:浏览器离开页面时弹出窗口;

复制代码代码如下:

<body onunload="openwin()">

方法三:用一个连接调用:

复制代码代码如下:

<a href="#" onclick="openwin()">打开一个窗口</a>

注意:使用的“#”是虚连接。

方法四:用一个按钮调用:

复制代码代码如下:

<input type="button" onclick="openwin()" value="打开窗口">

【4、同时弹出2个窗口的js弹出对话框】

对源代码稍微改动一下:

复制代码代码如下:

View Code

<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个js弹出对话框窗口的name(newwindows和newwindow2)不要相同,或者干脆全部为空。

【5、主窗口打开文件1.htm,同时弹出小窗口page.html】

如下代码加入主窗口<head>区:

复制代码代码如下:

View Code

<script language="javascript">

<!--

function openwin()

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

}

//-->

</script>

加入<body>区:

复制代码代码如下:

<a href="1.htm" onclick="openwin()">open</a>

即可。

【6、js弹出对话框弹出的窗口之定时关闭控制】

下面我们再对js弹出对话框的窗口进行一些控制,效果就更好了。如果我们再将一小段代码加入弹出的页面(注意是加入到page.html的HTML中,可不是主页面中,否则...),让它10秒后自动关闭是不是更酷了?

首先,将如下代码加入page.html文件的<head>区:

复制代码代码如下:

<script language="javascript">

function closeit()

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

</script>

然后,再用<body onload="closeit()">这一句话代替page.html中原有的<BODY>这一句就可以了。(这一句话千万不要忘记写啊!这一句的作用是调用关闭窗口的代码,10秒钟后就自行关闭该窗口。)

【7、在js弹出对话框窗口中加上一个关闭按钮】

复制代码代码如下:

<FORM>

<INPUT TYPE="BUTTON" value="关闭" onClick="window.close()">

</FORM>

呵呵,现在更加完美了!

【8、内包含的js弹出对话框窗口-一个页面两个窗口】

上面的例子都包含两个窗口,一个是主窗口,另一个是弹出的小窗口。

通过下面的例子,你可以在一个页面内完成上面的效果。

复制代码代码如下:

View Code

<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="#" onclick="openwin()">打开一个窗口</a>

<input type="button" onclick="openwin()" value="打开窗口">

</body>

</html>

看看 OpenWindow.document.write()里面的代码不就是标准的HTML吗?只要按照格式写更多的行即可。千万注意多一个标签或少一个标签就会出现错误。记得用OpenWindow.document.close()结束啊。

【9、终极应用--js弹出对话框的窗口之Cookie控制】

回想一下,上面的弹出窗口虽然酷,但是有一点小毛病(沉浸在喜悦之中,一定没有发现吧?)比如你将上面的脚本放在一个需要频繁经过的页面里(例如首页),那么每次刷新这个页面,窗口都会自动执行js弹出对话框代码一次,是不是非常烦人?:-(有解决的办法吗?Yes! -) Follow me.

我们使用cookie来控制一下就可以了。

首先,将如下代码加入主页面HTML的<HEAD>区:

复制代码代码如下:

View Code

<script>

function openwin()

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

function get_cookie(Name)

{var search = Name + "="

var returnvalue = ""

if (documents.cookie.length >0) {

offset = documents.cookie.indexOf(search)

if (offset != -1) {

offset += search.length

end = documents.cookie.indexOf("", offset)

if (end == -1)

end = documents.cookie.length

returnvalue=(documents.cookie.substring(offset,end))

}

}

return returnvalue

}

function loadpopup(){

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

openwin()

documents.cookie="popped=yes"

}

}

</script>

然后,用<bodyonload="loadpopop()">(注意不是openwin而是loadpop啊!)替换主页面中原有的<BODY>这一句即可。你可以试着刷新一下这个页面或重新进入该页面,窗口再也不会再有js弹出对话框了。真正的实现只弹一次。

需要注意的是,JS脚本中的的大小写最好前后保持一致。

以上就是九种js弹出对话框的代码,如果有其它方法也可以提供给我喔,当然自己写的弹窗也Ok的啦。

希望河南新华的回答可以帮助到你

JS打开选择本地文件的对话框:

方法一:

在定义的form中添加以下javascript代码:

{

              anchor : '95%',

              xtype: 'fileuploadfield',

              id: 'form-file',

              emptyText: '请选择',

              fieldLabel: '上传附件',

              name: 'meetingAffix',

              buttonText: 'Browse'

    }

单击Browse按钮后可以打开对话框,选中文件后,所选文件路径会出现在上传附件后面的文本框中。

方法二:

在定义的form中添加以下javascript代码:

{

      columnWidth :1,

      layout :'form',

      bodyStyle :'padding:10px',

      defaultType :'textfield',

      labelWidth :120,

      width :1000,

      buttonAlign :'center',

      items : [ {

       fieldLabel :'请选择要上传的文件',

       name :'fileName',

       id :'fileId',

       anchor :'95%',

      allowBlank :false,

       inputType :'file'

      } ]

}

方法三:

给按钮定义以下javascript函数:

var inputObj=document.createElement('input')

         inputObj.setAttribute('id','_ef')

         inputObj.setAttribute('type','file')

         inputObj.setAttribute("style",'visibility:hidden')

         document.body.appendChild(inputObj)

         inputObj.click()

         inputObj.value 

单击已经添加函数的按钮会弹出选择本地文件的对话框。