1

This is a webservice having script loaded on CheckJs webmethod.

    [WebMethod(EnableSession = true)]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public string CheckJs()
    {
        string script = "<script> (function() { alert('script loaded'); })(); </script>";

        return script;
    }

Checkscript.js

Here we call the AJAX method to consume webservice result

function Loadarticlecomments() {
inputdata = {};        
var outputhtml = "";
var newurl = "/WebServices/CheckJsScript/WSScript.asmx/CheckJs";
var dataStrVal = "";

jQuery.ajax({
    type: "POST",
    url: newurl,
    data: dataStrVal,
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function(result) {            
        if (result.d != null && result.d.length > 0) {                
            $(".commentsid").html((result.d));
        }
    },
    error: function(result) {
        console.log('JS Script-  system failure');
        return false;
    }

});

}

script is not firing.Could you please someone fix this?

Genet
  • 11
  • 1

1 Answers1

1

The script is not firing when you insert it in page because there is no one to execute it, is just standing there.

You can make use of eval() to execute an javascrip code represented as string. Don't forget to remove <script> tags from service.

// code
success: function(result) {            
    if (result.d != null && result.d.length > 0) {                
        eval(result.d); // execute script returned by service
    }
},
// other code

Note: Consider reading this, before use it, Why is using the JavaScript eval function a bad idea?

var data = "(function() { alert('script loaded'); })()"
eval(data);
Community
  • 1
  • 1
adricadar
  • 9,971
  • 5
  • 33
  • 46