Question

I am trying to run a simple alert script from static method(with intend of running functions in future), but it does not show me the alert(if I do it in page load event it works fine):

 Page page = HttpContext.Current.CurrentHandler as Page;

  if (page != null)
  {
       string myScript = "<script type=\"text/javascript\" language=\"Javascript\">";
       myScript += "alert('hi');";
       myScript += "</script>";
       page.ClientScript.RegisterClientScriptBlock(page.GetType(), "alert", myScript);
   }

If I debug the ClientScript does run, Can someone help please?

EDIT

This is an example script I want to run after the success event of ajax webmethod:

<script type="text/javascript">
    var OrderReference = '<%= Id %>';
    var EID = <%= EId %>;
    var Comment = '';
    var SubDomain = 'track';

    if (location.protocol.toLowerCase() == 'https:') 
        wgProtocol = 'https';
    else
        wgProtocol = 'http';

    Uri = wgProtocol + '://' + SubDomain + '.test.com/tr.html' + '?&eid=' + EID 
        + '&orderreference=' + OrderReference ;

    document.write('<sc' + 'ript language="JavaScript"  type="text/javascript" src="' + Uri + '"></sc' + 'ript>');
</script>

<noscript>
    <img src='http://test.com/transaction.html?ver=1&eventid=<%= EId %>&wgorderreference=<%= Id %>&' alt='' />
</noscript>

have already tried to return the above script as string and in ajax method tried following but get 400 bad request in Firebug:

var script = document.createElement( 'script' );
script.type = 'text/javascript';
script.src = result;
document.body.appendChild(script);

EDIT 2:

I created this class :

[Serializable]
    public class PixelGenericResults
    {
        public int Id{ get; set; }
        public string Script { get; set; }
    }

In my static method I do:

 var g = new GenericResults();
 g.Id = 2;
 g.Script = AffiliateTracking.getScript(g.Id); //This gets me the above script

 HttpContext.Current.Response.ContentType = "application/json";
 strReturn = new JavaScriptSerializer().Serialize(g);

and in aspx page on success I did :

 success: function (result) {
                        if (result.hasOwnProperty("d")) { result = result.d; }
    var script = document.createElement('script');
    script.type = 'text/javascript';
    script.src = result.Script;
    document.getElementsByTagName('body')[0].appendChild(script);
}

but when running it I get this error in firebug "NetworkError: 404 Not Found - http://localhost:5822/undefined"

Was it helpful?

Solution

As stated in comments, you can't directly interact with a page already rendered using an AJAX call, so you have to do some work on client side.

Here an example, based on the code you showed us:

c#:

//import this namespace:
using System.Web.Script.Serialization;

// declare a class to contain all the info you will need client-side
[Serializable]
public class GenericResult
{
    public int OrderReference  { get; set; }
    public string URL {get; set;}
    public string EID  { get; set; }
    public string Comment  { get; set; }
    // etc. etc.
}

//In your static method, instance and populate such class:
var r = new GenericResult();
r.OrderReference = 34;
r.URL = "http://www.google.com";
r.EID = "someString";
r.Comment = "this will work like a charm";
this.Response.ContentType = "application/json";
this.Response.Write(new JavaScriptSerializer().Serialize(r));

JS:

//In your AJAX 'onsuccess' callback, you can use the object very easily, eg:
function callback(r){
    var script = document.createElement('script');  
    script.type = 'text/javascript';
    script.src = r.URL;
    document.getElementsByTagName('body')[0].appendChild(script);
}

I have not tested this, so it may contains any typos, but that should be enough to let you to adapt your code.

EDIT:

 success: function (result) {
    //what is the next line for?!?!?!
    //if (result.hasOwnProperty("d")) { result = result.d; } 

    var script = document.createElement('script');
    script.type = 'text/javascript';
    script.src = result.Script;
    document.getElementsByTagName('body')[0].appendChild(script);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top