从javascript函数添加js文件引用 [英] add js file ref from javascript function

查看:169
本文介绍了从javascript函数添加js文件引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在执行一项任务,在该任务中,我正在动态创建一些输入控件,并且正在使用c#代码中的streamwriter创建一个js文件来验证这些输入控件.现在,我想在我的aspx页面中添加当前创建的js文件的引用.

我的板条箱输入控制方法是一种Web方法,我从客户端使用ajax-json调用它.
网络方法:



I am working on a task where I am creating some input control dynamically and i am creating a js file using streamwriter in c# code to validate these input control. Now I want to add ref of the currently created js file in my aspx page.

my crate input control method is a webmethod and I call it using ajax-json from client side.
web method:

[WebMethod]
    public static string LoadDatanGenerateControls(int PostTypeID, int CategoryID)
    {
        DataTable dtControls = new DataTable();
        StringBuilder sb = new StringBuilder();
        dtControls = PostingExtendedFields.GetExtendedDFields(PostTypeID, CategoryID);
        sb.Append("<table>");
        foreach (DataRow dr in dtControls.Rows)
        {
            sb.Append("<tr><td>");
            sb.Append("<input value='" + dr["RequiredError"].ToString() + "' type='text' size='20' id='txt_" + dr["FieldName"].ToString() + "' class='riTextBox riEmpty' style='width:450px;' onfocus ='Focus(this);' onblur=\"Blur(this,'" + dr["RequiredError"].ToString() + "');\"/>");
            sb.Append("</td></tr>");
        }
        sb.Append("</table>");
        //create the js file
        string path = HttpContext.Current.Server.MapPath("") + @"\";
        if (File.Exists(path + "sale.js")) File.Delete(path + "sale.js");//delete if exist
        using (StreamWriter sw = new StreamWriter(path + "sale.js"))
        {
            sw.WriteLine("function Focus(obj) {");
            sw.WriteLine("var id = obj.id;");
            sw.WriteLine("$('#' + id).val('');");
            sw.WriteLine(" }");
            sw.WriteLine("function Blur(obj, title) {");
            sw.WriteLine("var id = obj.id;");
            sw.WriteLine("if ($('#' + id).val() == '') {");
            sw.WriteLine("$('#' + id).val(title);");
            sw.WriteLine("}");
            sw.WriteLine(" }");
        }
        return sb.ToString();
    }



在aspx页面上:



on aspx page:

function LoadControls() {
            try {
                var types = $find("<%= ddlPost.ClientID %>");
                var Category = $find("<%= ddlCategory.ClientID %>");
                var typeID = types.get_value();
                var catID = Category.get_value();
                ///web method
                $('#TDDynamicContents').html('');
                $.ajax({
                    type: "POST",
                    url: "Default.aspx/LoadDatanGenerateControls",
                    data: "{'PostTypeID':'" + typeID + "','CategoryID':'" + catID + "'}",
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function(msg) {
                        if (msg.d.length > 0) {
                            $('#TDDynamicContents').html(msg.d);
                            //add js file path
                            addjsfile("sale.js", "js")
                        }
                    },
                    async: false,
                    error: function(xhr, status, error) {
                        alert(xhr.statusText);
                    }
                });
            } catch (e) { }
        }

        function addjsfile(filename, filetype) {
            alert(filetype);
            if (filetype == "js") {
                var fileref = document.createElement('script');
                fileref.setAttribute("type", "text/javascript");
                fileref.setAttribute("src", filename);
            }
            if (typeof fileref != "undefined") {
                document.getElementsByTagName("head")[0].appendChild(fileref);
                alert(fileref);
            }
        }




但是我的addjsfile函数根本不起作用.
它没有在Head部分添加参考,并且我无法在js文件中使用函数.




but my addjsfile function is not working at all.
its not adding the refrence in the Head section and I am unable to use functions in the js file.

any help would be greatly appreciated.

推荐答案

('#'+ id).val('');); sw.WriteLine(}"); sw.WriteLine("function Blur(obj,title){"); sw.WriteLine("var id = obj.id;"); sw.WriteLine("if(
('#' + id).val('');"); sw.WriteLine(" }"); sw.WriteLine("function Blur(obj, title) {"); sw.WriteLine("var id = obj.id;"); sw.WriteLine("if (


('#'+ id).val()==''){"); sw.WriteLine("
('#' + id).val() == '') {"); sw.WriteLine("


('#'+ id).val(title);"); sw.WriteLine(}"); sw.WriteLine(}"); } 返回sb.ToString(); }
('#' + id).val(title);"); sw.WriteLine("}"); sw.WriteLine(" }"); } return sb.ToString(); }



在aspx页面上:



on aspx page:

function LoadControls() {
            try {
                var types =


这篇关于从javascript函数添加js文件引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆