0

As we know that AJAX is used to request a web-part in HTML format from the server. Is it possible to request a script containing functions using AJAX?

Parveez Ahmed
  • 1,325
  • 4
  • 17
  • 28
  • Yes. Some Libraries like `prototypejs` offer automatic `eval`uation of responses. – RienNeVaPlu͢s Sep 06 '13 at 08:22
  • 1
    if I understand it correctly, you need the server to return a JS function that you can then use on the front-end side? if that's so, there's not a problem with that by a simple use of [eval()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/eval) – Zathrus Writer Sep 06 '13 at 08:24
  • oh thnks @ZathrusWriter ,,that is what I really want,,,can you give me some more details ? – Parveez Ahmed Sep 06 '13 at 08:27

3 Answers3

1

Yes you can load a javascript via ajax

http://api.jquery.com/jQuery.getScript/

$.getScript("ajax/test.js")
.done(function(script, textStatus) {
  console.log( textStatus );
})
.fail(function(jqxhr, settings, exception) {
  $( "div.log" ).text( "Triggered ajaxError handler." );
});

Also you could try something like this as mentioned in(how to run javascript in html loaded via ajax):

require("extra.js", function () {
    functionDefinedInExtraJS();
});

//Sample require function:

function require(file, callback) {

    var script = document.getElementsByTagName('script')[0],
        newjs = document.createElement('script');

    // IE
    newjs.onreadystatechange = function () {
        if (newjs.readyState === 'loaded' || newjs.readyState === 'complete') {
            callback();
        }
    };

    // others
    newjs.onload = function () {
        callback();
    };

    newjs.src = file;
    script.parentNode.insertBefore(newjs, script);
}

One other way would be to use eval() function and convert a string reply into working javascript code.

Community
  • 1
  • 1
0x_Anakin
  • 3,229
  • 5
  • 47
  • 86
  • Thanks,, is there any javascript solution doing the same ? – Parveez Ahmed Sep 06 '13 at 08:34
  • Well if you know the path of the file you could use the second proposal. Otherwise you could write an pure javascript ajax wrapper of your own which will collaborate with a server api and use eval() to make a string reply into working code. – 0x_Anakin Sep 06 '13 at 08:37
1

Here's an example how to use eval() to accomplish what you need:

var xmlhttp;
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }

xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
      eval(xmlhttp.responseText);
      // you can use whatever functionw as returned from the server from this line on :)
    }
  }

xmlhttp.open("GET","your-server-page-url",true);
xmlhttp.send();
Zathrus Writer
  • 4,311
  • 5
  • 27
  • 50
  • Suppose, I have a function named "test" which simply alerts a message.The test function is sent from the server.Also I want some text message to appear on a div in the current page from where AJAX request is made. That is: document.getElementById("id").innerHTML=xmlhttp.responseText Where should I place my code? @Zathrus Writer – Parveez Ahmed Sep 06 '13 at 09:13
  • 1
    that wouldn't work - you'd need to move the `innerHTML` code inside that *test* function and use element it of that DIV as a parameter of *test*, e.g.: `function test(divID) {alert('your text'); document.getElementById(divID).innerHTML = 'some new text';}` – Zathrus Writer Sep 06 '13 at 09:16
  • ooo it seems to be a different story.Can you give me some tutorial link over this? @Zathrus Writer – Parveez Ahmed Sep 06 '13 at 09:27
  • sorry, I don't have any tutorial link handy, but I'm sure you can find what you're looking for via a simple Google search now that you know what you're looking after :) – Zathrus Writer Sep 06 '13 at 09:32
0

Is it possible to request a script containing functions using AJAX?

Yes, it is possible. And those functions could be executed on the client. For example with jQuery you even have a function that allows you to perform such request: $.getScript.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928