I'm trying to get the class below to work, however I continue to get an error message stating
alert!parsererrorSyntaxError: Unexpected token < in JSON at position 0/Home/onPremTest{"msg":"onPremTest message"}
It appears the opening symbol in the script closing tag is causing this message. Does anyone know how to format it so that the browser accepts the javascript code and executes it?
public class Javascript
{
static string scriptTag = "<script type=\"\" language=\"\">{0}</script>";
public static void ConsoleLog(string message)
{
string function = "console.log('{0}');";
string log = string.Format(GenerateCodeFromFunction(function), message);
//System.Web.HttpContext.Current.Response.Write("<script>alert('Test message !'); </script>");
System.Web.HttpContext.Current.Response.Write(log);
}
public static void ConsoleError(string message)
{
string function = "console.error('{0}');";
string log = string.Format(GenerateCodeFromFunction(function), message);
Page page = HttpContext.Current.Handler as Page;
if (ScriptManager.GetCurrent(page).IsInAsyncPostBack)
{
ScriptManager.RegisterClientScriptBlock(page, page.GetType(), "error", "console.error('" + message + "')", true);
}
else
{
HttpContext.Current.Response.Write(log);
}
}
public static void Alert(string message)
{
string function = "alert('{0}');";
string log = string.Format(GenerateCodeFromFunction(function), message);
Page page = HttpContext.Current.Handler as Page;
if (ScriptManager.GetCurrent(page).IsInAsyncPostBack)
{
ScriptManager.RegisterClientScriptBlock(page, page.GetType(), "alert", "alert('" + message + "')", true);
}
else
{
HttpContext.Current.Response.Write(log);
}
}
static string GenerateCodeFromFunction(string function)
{
return string.Format(scriptTag, function);
}
}
from How can I write to the browser console in MVC?
No comments:
Post a Comment