开发者

jQuery show ax external PHP page in a DIV [duplicate]

This question already has answers here: Closed 11 years ago.

Possible Duplicate:

How to show a page where a DIV is with jQuery

Hello I have a DIV in the page index.html:

<div id="form"></div>

Now I need a way with jQuery to show another page inside that DIV. The page I need to call and load there is contact.php It is a simple HTML + PHP contact 开发者_高级运维form. Is there a way to load with jQuery the contents of contact.php inside index.html page where the DIV is?

Please remember that the contact.php page contains some javascript codes that must be fully working. So propably the jQuery.load function will not work in this case.

Thanks for your help!


Try this:

$.get("contact.php", function(data){ $("#form").html(data); });

If your contact.php is a full page (including <html>), you should consider loading it in an <iframe/>.

EDIT If $.get does not work as above, you could try this:

$.get('example.html', function(data) {
        var $page = $(data);
        $page.filter('script').add($page.find('script')).each(function(){
            $.globalEval(this.text || this.textContent || this.innerHTML || '');
        });
        $('#form').html(data);
    }
});

If $.get works as .load, script are ignored. With this code we're forcing them to be executed.

Hope this helps. Cheers


why won't jQuery.load work with javascript? It works great, you just have to use

$(function(){
     //your things
});

in ajax called page

you can fill your div with

$(function(){
    $.ajax({
        url: 'conteact.php', 
        success: function(data){ $("#form").html(data); }

    });
});


You could use the AJAX and the .load() method:

$(function() {
    $('#form').load('/contact.php');
});

If contact.php contains javascript code, this code will also work and be executed.

For example if contact.php looks like this:

<div id="foo"></div>
<script type="text/javascript">
    $('#foo').html('some dynamic content');
</script>

the resulting DOM will look like this after the AJAX call:

<div id="form"><div id="foo">some dynamic content</div></div>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜