Hello!

Such a general problem: I make a gadget into which the content is loaded when the button is pressed, when the gadget is turned on, the load() function is activated in the body , which loads the information, but when the button is pressed, the same function doesn’t happen, here’s the code:

javascript

 function load() { $.get("http://mirella8.ru/gadget/work.php?callback=", function (data) { $("#frame").html(data); }); } 

HTML

 <body onload="load();"> <div id="frame"></div> <button onclick="load();" >Обновить</button> </body> 

The code is working, but does not work in IE, when the load() page loads, it works, but when you press the button, it does not. :(

  • Well, help, plizzzz)) - Mikhail Nikolaev
  • And why did you decide that it does not work? add an alert('lol'); to the callback alert('lol'); And also check that conflicting scripts are not loaded into #frame . The probability of the presence of the " load " function in them is quite high) - Sh4dow

4 answers 4

KiTE is right, if you really use jQuery, then to the end!

And then, the code is working. Simply, due to the fact that the resulting object does not change is not visible and changes when you click on the button. If you add a parameter to the load () function, which thread when you click a button, you can see that everything works:

  <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <script type="text/javascript" src="jquery.js"></script> <script type="text/javascript"> function load(s){ $.get("http://localhost/stam/get.html", function(data){ $("#frame").html(data + s); }); } </script> </head> <body onload="load('');"> <div id="frame"></div> <button onclick="load('sss');" >Обновить</button> </body> </html> 

    Does it make sense to do that?

    Is this better? (not perfect, but still easier)

     var Startup = { __url: "", __data: {}, __target: "", // Инициализация constructor: function (url, data, target) { this.setUrl(url); this.setData(data); this.setTarget(target); this.regListeners(); this.remote(); }, // Сюда заносим все листенеры данного объекта regListeners: function () { jQuery("button_id").click( function () { Startup.remote(); }); }, /** Функция запроса * @param url - URL * @data - параметры запроса (object) * @target - Куда вставить */ remote: function (url, data, target) { if(!url) var url = this.getUrl(); if(!data) var data = this.getData(); if(!target) var target = this.getTarget(); var str = "?"; if(typeof(data) == "object") { for (i in data) { if(str.length > 1) str += "&"; str += i + "=" + data[i]; } } var url = this.getUrl + str; jQuery.get(url, str, function (e) { jQuery("#" + target).html(e); }); }, setUrl: function (e) { this.__url = e; }, getUrl: function () { return this.__url; }, getData: function () { return this.__data; }, setData: function (e) { if(typeof(e) != "object") return false; this.__data = e; } 

    }

    Using <script>

     var url = "http://mirella8.ru/gadget/work.php"; var params = { callback: ""}; var target = "frame"; jQuery(document).ready( function () { Startup.constructor(url, params, target) }); </script> 
    • 3
      "Simpler"?!! - KiTE
    • Of course it's easier. In short, the code is not a fact that it is easier. - Stanislav Komar
    • In this case, obviously, not easier. Why should a readable code of 10 lines be spread over several listings? Especially when, in any case, jQuery is used. - KiTE
    • :) That's when you will write large projects - you will understand :)) You need to write the code in such a way that it does not handle a specific situation, but it can also be suitable for use in other cases. PS And where is my code unreadable? PPS Code is more versatile. PPPS I said this is far from ideal. More ideal would be more code. - Stanislav Komar
    • At the expense of code reuse. There are a lot of options: library functions, plugin development, using an MVC-like architecture. Not a word about this. A simple question implies a simple answer. A man needs a scapula to dig a hole, and you need him to dig. ZY: I have been involved in the development of large projects for a long time ... - KiTE

    Try to use the standard framework constructs:

     jQuery(function($){}); 

    or

     jQuery(document).ready(function($){}); 

    For example:

     jQuery(function($){ var load = function(){ $.get("http://mirella8.ru/gadget/work.php?callback=", function(data){ $("#frame").html(data); }); }; $('#btn').click(load); load(); }); 

    HTML:

     <div id="frame"></div> <button id="btn">Обновить</button> 
    • Oshiiiiiiiibka :) If you need to use a structure like that of Miha Nikolaev (function call from another element, for example, by clicking), load will not work again :)) KiTE, think why? :) - Stanislav Komar
    • Because it is declared as local. But you can write it down in window.tools.load and everything will be fine. - Sh4dow
    • Not a mistake, but a typo :) The <button> needs to assign an id and hang up the processing of the click on it. Corrected. [Here] [1] a simplified version. [1]: jsfiddle.net/4dmVZ - KiTE
    1. If the appeal goes to another domain, then it is impossible. ajax works within the same domain. Yes, there are reservations, but in the simplest case, this is the case: you cannot go for other people's content. It describes in detail how to cope with it, if you really need.
    2. Using jQuery, it’s really better to write in jQuery style $(document).ready(function(){ ... }); and $("btn").click( ... ); The code in this case will be separated from the markup and it will be easier to search for it. 2.1. Just write $("#frame").load("content.php"); instead of $.get(...) , although if you need a callback in which you do something other than loading data into a frame, get will come down, though it’s $.ajax use $.ajax , imho.
    3. Yes, everything works in IE too))
    • Conceptually, on the 1st point, you are absolutely right. But since jQuery 1.4.4, .ajax() domain queries have been added to .ajax() . In the standard case, if the URL has a callback parameter, then this request is formed and processed according to the JSONP principle. Honor and praise to developers for standardization, although it may be misleading. - KiTE
    • @Yura Ivanov you are certainly right, but as far as my callback is here, this already means that I am making a request to another domain, and the request is successfully passed. - Mikhail Nikolaev
    • Yes, and I have already coped with the problem - Mikhail Nikolaev
    • Well, the example was not jsonp, but plain text, so I doubted the correctness of the idea. jsonp from other domains works on a different principle than the usual ajax, that is, not through XmlHttpRequest, it must be borne in mind and the error of possible non-work should already be looked for in the answer, not in the code ... - Yura Ivanov
    • I also watched through the browser - the text issue. Can the server side check some http header and return a json in a callback wrapper to the ajax request from jQuery? .. - KiTE