views:

91

answers:

2

Hi actually i am working on ajax in which i want to convert the iframe into div means that what is the functionality of iframe i want the same in div

Thanks.

+2  A: 

You can't have the exact same functionality of an iframe in a simple div. That's why they have two different names. Iframe is a bit of sandboxing, and can open urls on any domain.

With Ajax on nowadays browsers you have to stick with the same-origin policy which means you can only load contents from the same domain your site operates on.

So, aside from this restriction and the fact that iframe-like sandboxing cannot be emulated in a div here, is a simple ajax solution with javascript, which you can use to achive a site navigation for instance:

HTML

<ul id="nav">
  <li><a href="page1.html">page1</a></li>
  <li><a href="page2.html">page2</a></li>
  <li><a href="page3.html">page3</a></li>
</ul>

Javascript

// select the menu element
var nav = document.getElementById("nav");

// watch for clicks on the menu
nav.onclick = function(e) {

  // get the element that was clicked
  e = e || window.event;
  var el = e.target || e.srcElement;

  // only act if it was a link
  if (el.nodeName == "A") {

    // making a call is as simple as this
    ajax(el.href, function(data) {

        // do something with the server's response
        // e.g.: put it to the #content element
        document.getElementById("content").innerHTML = data;
    });

    // prevent default action
    return false;
  }
};

///////////////////////////////////////////////////////////////////////////////

function getXmlHttpObject() {
    var xmlHttp;
    try {
        // Firefox, Opera 8.0+, Safari
        xmlHttp = new XMLHttpRequest();
    } catch (e) {
        // Internet Explorer
        try {
            xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
    }
    if (!xmlHttp) {
        alert("Your browser does not support AJAX!");
    }
    return xmlHttp;
}


function ajax(url, onSuccess, onError) {

    var xmlHttp = getXmlHttpObject();

    xmlHttp.onreadystatechange = function() {
        if (this.readyState === 4) {

            // onSuccess
            if (this.status === 200 && typeof onSuccess == 'function') {
                onSuccess(this.responseText);
            }

            // onError
            else if(typeof onError == 'function') {
                onError();
            }
        }
    };
    xmlHttp.open("GET", url, true);
    xmlHttp.send(null);
    return xmlHttp;
}

galambalazs
@galambalazs, forgot to put the simple solution in .. :)
Gaby
@Gaby because I was hacking it.. :)
galambalazs
@galambalaz, thought that would be the case.. just making sure
Gaby
@Gaby you couldn't belive anyone will actually post him some code, right? :))
galambalazs
There is nothing worse for beginner than to get one sentence answer hidden in 100 lines of non relevant to his post information.
eugeneK
@eugeneK You're wrong. I gave him a **reusable** solution, which can be applied for **many situations** very **easily**. Replacing iframes with Ajax is a common pattern btw, so it really can't mean too many things (especially for a beginner, it's 99% site nav). Finally, he accepted it which means he found it useful. You however gave him nothing just empty words of a critical comment.
galambalazs
A: 

@rajesh, You cannot load external webpages via Ajax unless you place a JavaScript file with Ajax calls on website where resides the content you would like to place on your website in Div. So the only way for you is to use Iframes.

eugeneK
`"So the only way for you in to use Iframes."` - how do you know what is he using iframe for? For same-domain things ajax is great.
galambalazs
@galambalazs, you seem to be playful today. I could guess it is for getting something from external site.
eugeneK
I'm always playful. :)
galambalazs