how to load the content of a html url in the same view in javascript.
-
1Please be (much) more specific.polarblau– polarblau2011-01-21 10:18:16 +00:00Commented Jan 21, 2011 at 10:18
-
I am parsing a xml file and the link which i am talking about, is the one the content which i get after the parsing is done. Now i want to get the content of that html link in order to display that content on some view.neha– neha2011-01-21 11:01:06 +00:00Commented Jan 21, 2011 at 11:01
-
Where do all the newbies come from recently?BastiBen– BastiBen2011-01-21 12:12:27 +00:00Commented Jan 21, 2011 at 12:12
Add a comment
|
4 Answers
Another option is to use an IFRAME tag.
2 Comments
Wasim Karani
+1 because I had also given same answer and I think this is the right way to do it...
neha
After adding iframe tag in html of dashcode(web application IDE for iphone), i am getting an error which says "For Security reasons framing is not allowed".
If you're talking about loading another page's content in Javascript, you need to use 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>
More info and examples at W3Schools. Note that AJAX doesn't work across different domains for security restrictions.
2 Comments
neha
I am working in Dashcode On Mac so getting error while using ActiveXObject, any solution to this.....
mrbellek
The exception for using ActiveXObject is for older Internet Explorer versions. I can't imagine you are developing for IE5/6 on a Mac, so if the line gives you errors, just remove it.
Iframe is going to be the best option for you 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>
Comments
if you want to do it easily and without an iframe you might find jQuery's load method helpful
$('#id-of-element-that-accepts-html-result').load('/url-that-returns-html-snippet.xyz', {
//querystring parameters
foo: 'bar',
moo: 'par',
});