javascript html url加载问题

时间:2011-01-21 10:14:03

标签: javascript

如何在javascript中的同一视图中加载html网址的内容。

4 个答案:

答案 0 :(得分:1)

另一种选择是使用IFRAME标记。

答案 1 :(得分:0)

如果您正在谈论在Javascript中加载其他页面的内容,则需要使用AJAX。

<html>
<head>
<script type="text/javascript">
function loadXMLDoc()
{
var xmlhttp;
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for older IE
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","ajax_info.txt",true);
xmlhttp.send();
}
</script>
</head>
<body>

<div id="myDiv"><h2>Let AJAX change this text</h2></div>
<button type="button" onclick="loadXMLDoc()">Change Content</button>

</body>
</html>

W3Schools的更多信息和示例。 请注意,为了安全限制,AJAX在不同的域中不起作用。

答案 2 :(得分:0)

iframe将是你最好的选择neha

<html>
<head>
</head>

<body>
<font face="verdana,arial" size="3">
<div align="center">
<p>

<a href="http://www.altavista.com" target="iframe1">AltaVista.com</a> |
<a href="http://www.aol.com" target="iframe1">AOL.com</a> |
<a href="http://www.lycos.com" target="iframe1">Lycos.com</a> |
<a href="http://www.yahoo.com" target="iframe1">Yahoo.com</a>

<p>
<iframe
name="iframe1"
width="600"
height="400"
src="http://www.stackoverflow.com"
frameborder="yes"
scrolling="yes">
</iframe>
<p>

</font>
</div>

<!--
name="iframe1"
- Set all links to target the iframe1 name when you want the contents of the iframe to change from links on the originating page.

width="600" - Width of the iframe

height="400" - Height of the iframe * Width and height can be expressed in pixels or percent

src="http://www.yahoo.com" - Initial page that will fill in the iframe

frameborder="yes" - Border around the iframe is displayed by default

scrolling="yes" - Allow scrollbars around the iframe for navigation Vaild values are yes, no and auto

align="" - Valid entries are left and right left is set by default
-->

</body>
</html> 

答案 3 :(得分:0)

如果您想轻松完成并且没有iframe,您可能会发现jQuery's load method有用

$('#id-of-element-that-accepts-html-result').load('/url-that-returns-html-snippet.xyz', {
  //querystring parameters
  foo: 'bar',
  moo: 'par',
});
相关问题