Cross browser AJAX function to dynamically load HTML

后端 未结 6 1680
清酒与你
清酒与你 2021-02-06 12:25

I\'m looking for an AJAX function to dynamically request an HTML page. I already found the following:

function ajaxinclude(url) 
{
   var page_request = false

         


        
相关标签:
6条回答
  • 2021-02-06 12:32

    I recommend jQuery, but there is also a very lightweight solution: XHConn

    0 讨论(0)
  • 2021-02-06 12:39

    Or you can try this if you don't need an entire framework: http://www.hunlock.com/blogs/The_Ultimate_Ajax_Object

    0 讨论(0)
  • 2021-02-06 12:39

    I would suggest using any of a number of different javascript frameworks for this functionality instead of reinventing it. There's jQuery, Prototype/Scriptaculous, MooTools, Dojo, and many others. All of these offer cross-browser support for what you are doing.

    0 讨论(0)
  • 2021-02-06 12:45

    If I were you, I'd use a toolkit like JQuery in order to make sure that it is as compatible as possible. No matter what, however, you're going to have to deal with the cases where it doesn't work. Don't forget that there are plenty of people who browse without javascript support.

    0 讨论(0)
  • 2021-02-06 12:49

    You might use an IE version which your script does not support. Try it again with this code snippet added before your function. ajaxinclude() can then be shortened to

    function ajaxinclude(url)  {
        var req = new XMLHttpRequest;
        if(!req)
            return null;
    
        req.open('GET', url, false); // get page synchronously 
        req.send();
        return req.responseText;
    }
    

    As an aside: I generally dislike using frameworks - there's too much magic happening behind the scenes for me to feel comfortable...

    0 讨论(0)
  • 2021-02-06 12:53

    I would have to agree, don't go reinventing the wheel, or in this case, AJAX.

    JQuery and Prototype do an excellent job of letting you NOT have to deal with cross-browser support, and greatly simplifying Javascript type programming. I fell into JQuery first so I'm biased towards it, and from what I've seen the library is a little smaller (read: faster in the browser), but Prototype I believe has been around longer and has TONS of plugins and examples out there. Ruby on Rails also by default uses Prototype. Funny enough, the code in both looks very similar and takes little rewriting to change libraries.

    JQuery Tutorials <-- Just head on down to the AJAX section

    0 讨论(0)
提交回复
热议问题