JQuery Ajax $ .getScript () Method

$. getScript from jQuery ajax methods can be used to load and execute javascript from a remote place ... Can you also use javascript to load and execute it later when some event occurs on elements that were loaded using ajax based on some actions user ...?

Thank you and respectfully ....

+3
source share
3 answers

Is the goal to bind the script to elements that are not yet loaded, or to reduce the number of calls to the remote script?

, script , $.getScript() , :

function executeScript() {
     $.getScript("somescript.js", 
          blah blah blah
          );
 }

$(".post_loaded_elements").hover(executeScript);

, script - , , , , , - , script ajax - . , , script , - . , , jquery.

+2

$.getScript Javascript. Javascript, Javascript , , .

, :

$.getScript('http://example.com/hello.js');

:

function Hello() {
  alert('hello');
}

Javascript , , . , Hello(). , , , Hello(). Javascript. :

$.getScript('http://example.com/hello.js', function() { Hello(); });

script, , Hello.

+8

Yes it is possible. I made the following script, it consists of three files, an interface, an html text field loaded from an external file when users press button 2 and an external script that loads when users click on the first and notifies the value of the text field that was downloaded before the script but after loading the document.

Click button 2, enter something in the text box and click button 1, here is the code.

html page (interface)

<button id="b1">button 1</button>
<button id="b2">button 2</button>

JQuery

$(document).ready(function(){
    $("#b1").click(function(){
        $.getScript('externalscript.js');
    });
    $("#b2").click(function(){
        $("#load").load('test.html');
    });


});

Loaded item

<input type="text" id="text" />

external script

alert($("#text").val());
0
source

Source: https://habr.com/ru/post/1723309/


All Articles