简体   繁体   中英

Load external Web Page with jQuery

I was wondering what a good way to load an external web page (same server) would be. I have tried .load() and .get() however, The external page has a php script that spits out information every few seconds, the .load() and .get() only load it after the php is done. I have tried iFrame with does load it displaying the information being outputted by the PHP script. However, I don't really like to use iFrames. Thanks!

If your goal is for the PHP information (that is spit out every few seconds) to be updated on your site, then what you want to do is use AJAX, inside a setInterval routine.

See this post for the basics of AJAX -- it really is simpler than you might think. (You might first want to look at the simple examples linked at bottom).

Once you've got a simple ajax exchange happening, put that into a function called, for example, doAjax() -- and then create a setInterval, like this:

setInterval('doAjax();',60000);

Here is an important note when considering setInterval


Following is a simple copy/paste(able) example that will let you see exactly what I mean:

HTML/javascript: index.php

<html>
    <head>
        <script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
        
        <style>
            #timeDiv{width:40%;height:200px;background:wheat;padding:10px;}
        </style>
        
        <script type="text/javascript">
            $(document).ready(function() {

                doAjax();
                
                window.setInterval(function(){
                    doAjax();
                },2000);
                
            }); //END document.ready

            function doAjax() {
                $.ajax({
                    type: "POST",
                    url: "your_php_processor.php",
                    success: function(myData) {
                        $('#thetime').html(myData);
                    }
                });
            }

        </script>
    </head>

<body>

    <div id="timeDiv">
        The time is: <span id="thetime"></span>
    </div>

</body>

</html>

Now, the PHP side... your_php_processor.php

<?php

    $d = date("h:i:s");
    echo $d;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM