如何用JAVASCRIPT取得IFRAME显示内容

yang6130 2008-06-06 11:10:31
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
</head>

<body>
<iframe name="userIP" id="userIP" src="http://www.ip138.com/ip2city.asp" frameborder="0" scrolling="no" width="100%" height="10%"></iframe>
</body>
</html>

以上HTML文件中用框架来显示用户IP归属地,结果内容为:“您的IP地址是:[125.76.232.90] 来自:陕西省西安市 电信”,请问在同一页面中如何用JAVASCRIPT取得结果并赋值给变量VAR IP("陕西省西安市 电信");
...全文
531 10 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
<style type="text/css">
<!--
body{background: #fff;margin:0;}
table,td,img,form{border: none;margin: 0;padding: 0;}
td,p,li,select,input,textarea{font-size:12px;}
a{text-decoration:underline;}
a:link{color:#00007f;}
a:visited{color:#00007f;}
a:active,a:hover{color:#f00;}
-->
</style>
<%
On Error Resume Next
Server.ScriptTimeOut=9999999
Function getHTTPPage(Path)
t = GetBody(Path)
getHTTPPage=BytesToBstr(t,"GB2312")
End function
Function GetBody(url)
on error resume next
Set Retrieval = CreateObject("Microsoft.XMLHTTP")
With Retrieval
.Open "Get", url, False, "", ""
.Send
GetBody = .ResponseBody
End With
Set Retrieval = Nothing
End Function

Function BytesToBstr(body,Cset)
dim objstream
set objstream = Server.CreateObject("adodb.stream")
objstream.Type = 1
objstream.Mode =3
objstream.Open
objstream.Write body
objstream.Position = 0
objstream.Type = 2
objstream.Charset = Cset
BytesToBstr = objstream.ReadText
objstream.Close
set objstream = nothing
End Function
Function Newstring(wstr,strng)
Newstring=Instr(lcase(wstr),lcase(strng))
if Newstring<=0 then Newstring=Len(wstr)
End Function


Dim wstr,str,url,start,over,dtime

url="http://www.ip138.com/ip2city.asp"
wstr=getHTTPPage(url)
start=Newstring(wstr,"[")
over=Newstring(wstr,"]")
body=mid(wstr,start+1,over-start-1)
start1=Newstring(wstr,"来自:")
over1=Newstring(wstr,"</center>")
body1=mid(wstr,start1+3,over1-start1-3)
response.write body
response.write body1
%>
  • 打赏
  • 举报
回复
<style type="text/css">
<!--
body{background: #fff;margin:0;}
table,td,img,form{border: none;margin: 0;padding: 0;}
td,p,li,select,input,textarea{font-size:12px;}
a{text-decoration:underline;}
a:link{color:#00007f;}
a:visited{color:#00007f;}
a:active,a:hover{color:#f00;}
-->
</style>
<%
On Error Resume Next
Server.ScriptTimeOut=9999999
Function getHTTPPage(Path)
t = GetBody(Path)
getHTTPPage=BytesToBstr(t,"GB2312")
End function
Function GetBody(url)
on error resume next
Set Retrieval = CreateObject("Microsoft.XMLHTTP")
With Retrieval
.Open "Get", url, False, "", ""
.Send
GetBody = .ResponseBody
End With
Set Retrieval = Nothing
End Function

Function BytesToBstr(body,Cset)
dim objstream
set objstream = Server.CreateObject("adodb.stream")
objstream.Type = 1
objstream.Mode =3
objstream.Open
objstream.Write body
objstream.Position = 0
objstream.Type = 2
objstream.Charset = Cset
BytesToBstr = objstream.ReadText
objstream.Close
set objstream = nothing
End Function
Function Newstring(wstr,strng)
Newstring=Instr(lcase(wstr),lcase(strng))
if Newstring<=0 then Newstring=Len(wstr)
End Function


Dim wstr,str,url,start,over,dtime

url="http://www.ip138.com/ip2city.asp"
wstr=getHTTPPage(url)
start=Newstring(wstr,"[")
over=Newstring(wstr,"]")
body=mid(wstr,start,over-start)
start1=Newstring(wstr,"来自:")
over1=Newstring(wstr,"</center>")
body1=mid(wstr,start1,over1)
response.write body
response.write body1
%>
  • 打赏
  • 举报
回复
你这个调用的是别人网站的,使用这种方法是不行的
要使用xmlhttp方法
yang6130 2008-06-07
  • 打赏
  • 举报
回复
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
</head>

<body>
<iframe name="userIP" id="userIP" src="http://www.ip138.com/ip2city.asp" frameborder="0" scrolling="no" width="100%" height="10%"> </iframe>
<form id="form1" name="form1" method="post" action="">
<label>
<input type="button" name="test" value="测试" onclick="javascript:alert(window.frames["userIP"].document.body.innerHTML)" />
</label>
</form>
</body>
</html>

好像不行呀,各位再帮助看看,最好能帮助测试OK!
懒牛科技 2008-06-07
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 btbtd 的回复:]
function fIframeContent(XIframe){return XIframe.contentWindow.document.body.innerHTML;}
[/Quote]
lfywy 2008-06-07
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 btbtd 的回复:]
function fIframeContent(XIframe){return XIframe.contentWindow.document.body.innerHTML;}
[/Quote]
guilin_gavin 2008-06-06
  • 打赏
  • 举报
回复
如果想从外面的页面获取iframe页面中的内容,可以使用如下的调用:
document.userIP.IP 即可获得iframe页面中的变量IP的值
当然也可以是写成window.frames[0].IP,都是一个意思
如果想从irame页面访问父页面的变量内容,可以写成window.top.document.IP即可,想必可以解决楼主的问题~
太乙 2008-06-06
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 tantaiyizu 的回复:]
alert(window.frames["userIP"].document.body.innerHTML)
[/Quote]
btbtd 2008-06-06
  • 打赏
  • 举报
回复
function fIframeContent(XIframe){return XIframe.contentWindow.document.body.innerHTML;}
tantaiyizu 2008-06-06
  • 打赏
  • 举报
回复
alert(window.frames["userIP"].document.body.innerHTML)

87,996

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧