[英]Can not run Javascript from static method
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? 如果我调试
ClientScript
确实在运行,有人可以帮忙吗?
EDIT 编辑
This is an example script I want to run after the success event of ajax webmethod
: 这是我想在
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: 已经尝试将上面的脚本作为字符串返回,并且在ajax方法中尝试了以下操作,但是在Firebug中收到了
400 bad request
:
var script = document.createElement( 'script' );
script.type = 'text/javascript';
script.src = result;
document.body.appendChild(script);
EDIT 2: 编辑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 : 在成功的aspx页面中,我做了:
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"
但是运行它时,我在萤火虫中收到此错误
"NetworkError: 404 Not Found - http://localhost:5822/undefined"
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. 如评论中所述,您不能直接与已经使用AJAX调用呈现的页面进行交互,因此您必须在客户端进行一些工作。
Here an example, based on the code you showed us: 这是一个示例,基于您向我们展示的代码:
c#: 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: 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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.