0

I have an HTML page which I want to load on a click of a class using JavaScript. I am thinking of fetching that HTML using an iframe. This page contains content in the form of an overlay.

I have used .load() function of jQuery but it does not do anything. The pages I am working are both in same domain so I was hoping that the page would have loaded.

How can I achieve this?

$(".popup-layout").click(function() {

// I want to load an iframe here. Where should that iframe sit on the current page with diplay:none.




    });
3
  • can you post your markup? or a fiddle would be nicer
    – Yaje
    Jun 26, 2014 at 1:23
  • I was hoping that on click of the class .popup-layout, i fetch the page localhost:8080/abc.html using javascript. I have got just this javascript. Jun 26, 2014 at 1:28
  • possible duplicate of How do I load an url in iframe with Jquery
    – Yaje
    Jun 26, 2014 at 1:34

3 Answers 3

0

I would use ajax loaded content over an iframe, just my opion though.

$(".popup-layout").click(function() {
       $.get("yourFile.html", function(data){
        $("#Content").html(data); // the div to load content in
       });
    });
0

$('#result') is an <iframe id='result'>? You can't populate an iframe via jquery.load(). In fact, you don't need to. You can use a simple <div id='result'>

2
  • actually you can. using .attr()
    – Yaje
    Jun 26, 2014 at 1:31
  • You mean setting the src attribute of the iframe? Of course you can do that, but that has nothing to do with jquery.load() Jun 26, 2014 at 1:49
0

Here are 3 methods that can help you achieve that:

  1. Load the page on an <iframe> by changing its src. Example fiddle:

    YourFrameId.src = 'http://example.com';
    
  2. Load the page via an HTTP request and change the innerHTML of an existent element. Example fiddle:

    var req = new XMLHttpRequest();
    req.open('GET', 'http://www.mozilla.org/', true);
    req.onreadystatechange = function (aEvt) {
        if (req.readyState == 4 && req.status == 200) {
            YourElementId.innerHTML = req.responseText;
        }
    };
    req.send(null); 
    
  3. Use the location attribute of the document object:

    document.location = 'http://example.com'
    

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.