0

I have my JavaScript code in SharePoint site which is working only if we keep break point in my code in developer tools in IE. In chrome and Firefox it is working.

$(document).ready(function(){



 var publishButton = $("input[id$=onetidSaveItem]");
    // change redirection behavior
   publishButton.removeAttr("onclick");

publishButton.click(function SubmitPageCustom(fAddMore, btn) {
    var surveyListName; 
    $.ajax({
        url:_spPageContextInfo.webAbsoluteUrl + "/_api/Web/Lists(guid'"+ _spPageContextInfo.pageListId.replace("{","").replace("}","") +"')",

    method: "GET",
    headers: { "Accept": "application/json; odata=verbose" },
    success: function (data) {
       // console.log("List Title :" + data.d.Title);
         surveyListName=data.d.Title;
          btn=publishButton;
fAddMore=false;
        var currentUrl = window.location.href;
        currentUrl = currentUrl.substring(0, currentUrl.lastIndexOf('/')-11);
    var formSubmit  = document.forms.frmSubmit;
    var pageCommand = "Cmd=NewField";

    var formAction  =currentUrl+"_layouts/15/mngfield.aspx?i=1" +
                                "&" + pageCommand + "&" +
                                "List="+_spPageContextInfo.pageListId.replace("{","%7B").replace("}","%7D").split('-').join('%2D');
    if (fAddMore)                      
    {
       // var surveyUrl = currentUrl+"_layouts/15/qstNew.aspx/List/"+_spPageContextInfo.pageListId.replace("{","%7B").replace("}","%7D").split('-').join('%2D');
        formSubmit.action = formAction + "&NextUsing=" +  currentUrl.split(':').join('%3A').split('/').join('%2F')+"%5Flayouts%2F15%2FqstNew%2Easpx%2FList%3D"+_spPageContextInfo.pageListId.replace("{","%7B").replace("}","%7D").split('-').join('%2D');

    }
    else
    {
        //setTimeout(function(){ formSubmit.action = formAction + "&NextUsing=" + currentUrl+"Lists/"+surveyListName+"/preview.aspx"; }, 10000);
        formSubmit.action = formAction + "&NextUsing=" + (currentUrl+"Lists/"+surveyListName+"/preview%2Easpx").split(':').join('%3A').split('/').join('%2F');


    }

    var frm         = document.forms[MSOWebPartPageFormName];
    var Name        = TrimSpaces( frm.Name.value        );
    var DisplayName = TrimSpaces( frm.DisplayName.value );

    if (DisplayName.length < 1)
    {
         alert(L_alert1_Text);
         frm.DisplayName.focus();
         return false;
    }
    var GroupName;

    {
        var FromBaseType= "";
        var DisplaySize = "";
        var Type        = GetFieldType();
        if (Type == "")
        {
            alert("Column type is not selected.");
            return;
        }
        if (-1 != DisplayName.indexOf("[") ||
            -1 != DisplayName.indexOf("]"))
        {
            alert(L_alert17_Text);
            frm.DisplayName.focus();
            return false;
        }
        if (Type == "PageSeparator")
        {
            count = 0;
            do
            {
                count++;
                tempDisplayName = DisplayName + " " + count;
            }
            while(doesFieldNameConflict(tempDisplayName))
            DisplayName = tempDisplayName;
        }
        else
        {
            if (doesFieldNameConflict(DisplayName))
            {
                alert(L_alert16_Text);
                frm.DisplayName.focus();
                return false;
            }
        }

        if (! helperFrm.FValidate(true))
            return false;
       if (Type=="Boolean" || Type=="Calculated" || Type=="PageSeparator")
       {
            var Required    = "";
        }
        else {
            var Required;
            if (frm.Required != null)
                Required = frm.Required[0].checked ? "TRUE" : "FALSE";
            else
                Required =  "FALSE" ;
        }
        var EnforceUniqueValues = "";
        if (frm.EnforceUniqueValues != null)
            EnforceUniqueValues = frm.EnforceUniqueValues[0].checked ? "TRUE" : "FALSE";
        else
            EnforceUniqueValues =  "FALSE" ;
        var Indexed =  "FALSE" ;

            if (EnforceUniqueValues == "TRUE")
            {
                if (!confirm('This column must be indexed to enforce unique values. Do you want to index this column?')) return false;
                Indexed = "TRUE";
            }

        var UnlimitedLengthInDocumentLibrary = "";

        if (Type=="Lookup" || Type=="User") {
            var LookupList  = GetLookupList();
            var LookupField = TrimSpaces( GetLookupField() );
            var ShowPresence= GetShowPresence();
            if (LookupList == "UserInfo")
            {
                Type = "User";
            }
            else
            {
                Type = "Lookup";
            }
        }
        var FillInChoice = "";
        if (Type=="Choice") {
            var Format      = frm.Format     ? GetRadioButtonValue(frm.Format) : "";
            FillInChoice    = frm.FillInChoice ? GetRadioButtonValue(frm.FillInChoice) : "";
        }
        else if (Type=="DateTime" || Type=="Calculated"){
            var Format    = frm.Format[0].checked ? "DateOnly" : "DateTime";
        }
        else {
            var Format      = frm.Format     ? GetSelectedValue(frm.Format)      : "";
        }
        if (Type=="Boolean") {
            var Default     = frm.Default    ? GetSelectedValue(frm.Default) : "";
        }
        else if (Type=="DateTime") {
            var Default = "";
            if (frm.Default) {

                if (frm.Default[3].checked)
                {
                    var DefaultFormula = frm.DefaultFormula    ? TrimSpaces(frm.DefaultFormula.value): "";
                }
                else if (frm.Default[2].checked)
                {
                    if (g_fld.date)
                        Default = DateOptions.StISODate(g_fld.date);
                    else {
                        frm.Default[0].checked = true;
                        Default =  frm.Default[0].value;
                    }
                } else
                    Default =  GetRadioButtonValue(frm.Default);

            }
            if (frm.FriendlyFormat)
            {
                if (frm.FriendlyFormat[0].checked)
                {
                    var FriendlyFormat = frm.FriendlyFormat[0].value;
                }
                else if (frm.FriendlyFormat[1].checked)
                {
                    var FriendlyFormat = frm.FriendlyFormat[1].value;
                }
            }
        }
        else {
        if (frm.Default && frm.Default[1])
        {
            if (frm.Default[1].checked)
            {
                var DefaultFormula = frm.DefaultText ? TrimSpaces(frm.DefaultText.value): "";
                var Default     = "";
            }
            else
            {
                var Default     = frm.DefaultText ? TrimSpaces(frm.DefaultText.value): "";
            }
        }
        else
                var Default     = frm.Default ? TrimSpaces(frm.Default.value): "";
        }
        var L_GridLow_Text = "Low";
        var L_GridAverage_Text = "Average";
        var L_GridHigh_Text = "High";
        var GridChoices = frm.GridChoices ? frm.GridChoices.value  : "";
        var GridStartNum= frm.GridNumRange ? 1 : "";
        var GridEndNum  = frm.GridNumRange ? frm.GridNumRange.selectedIndex + 3 : "";
        var GridTxtRng1 = frm.GridTxtRng1 ? frm.GridTxtRng1.value : "";
        var GridTxtRng2 = frm.GridTxtRng2 ? frm.GridTxtRng2.value : "";
        var GridTxtRng3 = frm.GridTxtRng3 ? frm.GridTxtRng3.value : "";
        var GridNATxt   = frm.GridNATxt ? frm.GridNATxt.value : "";
        var Description = frm.Description ? TrimSpaces( frm.Description.value ): "";
        var NumLines    = frm.NumLines    ? frm.NumLines.value : "";
        var MaxLength   = frm.MaxLength   ? frm.MaxLength.value : "";
        var Choices     = frm.Choices     ? frm.Choices.value  : "";
        var radioPushdown = document.getElementById("RadPushDownYes");
        var PushDown = (radioPushdown != null && radioPushdown.checked) ? "1" : "";
        var AddToDefVw  = "ON";
        var AddToAllContentTypes = 'NO';
        var Min         = frm.Min        ? frm.Min.value : "";
        var Max         = frm.Max        ? frm.Max.value : "";
        var Decimals    = frm.Decimals   ? TrimSpaces( GetSelectedValue(frm.Decimals) ): "";
        var Percentage  = frm.Percentage ? (frm.Percentage.checked==1 ? "TRUE" : "") : "";
        var RichText = "";
        var RichTextMode = "";
        if (frm.RichText)
        {
            if (frm.RichText[0].checked)
            {
                RichText = "FALSE";
                RichTextMode = "";
            }
            if (frm.RichText[1].checked)
            {
                RichText = "TRUE";
                if (frm.RichText.length > 2)
                    RichTextMode = "Compatible";
                else
                    RichTextMode = "FullHtml";
            }
            if (frm.RichText.length > 2 && frm.RichText[2].checked)
            {
                RichText = "TRUE";
                RichTextMode = "FullHtml";
            }
        }
        var AllowHyperlink = frm.AllowHyperlink ? "TRUE" : "";
        var AppendOnly  = frm.AppendOnly ? (frm.AppendOnly[0].checked==1 ? "TRUE" : "") : "";
        var CurrencyInput = frm.ctl00_PlaceHolderMain_idLCID2;
        var LCID        = CurrencyInput ? GetSelectedValue(CurrencyInput) : "";
        var CalType     = frm.RadioCalType ? (frm.RadioCalType[1].checked  ? (frm.CalType  ?    GetSelectedValue(frm.CalType) : "") : "") : "";
        var Direction   = frm.Direction  ?             GetSelectedValue(frm.Direction) : "";
        var DefaultURLDesc = frm.DefaultURLDesc ? TrimSpaces(frm.DefaultURLDesc.value): "";
        var ResultType  = frm.ResultType ? TrimSpaces( GetRadioButtonValue(frm.ResultType) ): "";
        var Formula     = frm.Formula    ? TrimSpaces(frm.Formula.value) : "";
        var VldFormula  = elemVldFormula ? TrimSpaces(elemVldFormula.value) : "";
        var VldMessage  = elemVldMessage ? TrimSpaces(elemVldMessage.value) : "";
        var ReadOnly    = frm.ReadOnly   ? "TRUE" : "";
        if (Type == "Calculated")
        {
            Decimals = TrimSpaces( GetSelectedValue(frm.Decimals) );
        }
        if (Type=="Number" || Type=="Currency")
        {
            if (Max != "" && Min != "" && (Max - 0) < (Min - 0))
            {
            AlertAndSetFocus(L_alert6_Text, frm[FrmLocalFieldName("Min")] );
            return false;
            }
            else if (Min != "" && Default != "" && (Default - 0) < (Min - 0))
            {
                AlertAndSetFocus(L_alert8_Text, frm[FrmLocalFieldName("DefaultText")]);
                return false;
            }
            if (Max != "" && Default != "" && (Default - 0) > (Max - 0))
            {
                AlertAndSetFocus(L_alert9_Text, frm[FrmLocalFieldName("DefaultText")]);
                return false;
            }

            if (Type=="Number" && Percentage == "TRUE")
            {
                if (Min != "") Min = StDivideByOneHundred(Min);
                if (Max != "") Max = StDivideByOneHundred(Max);
                if (Default != "") Default = StDivideByOneHundred(Default);
            }

        }
        else if (Type=="Text" && Default)
        {
            if (MaxLength != "" && Default.length > (MaxLength - 0))
            {
                AlertAndSetFocus(L_alert11_Text, frm.DefaultText);
                return false;
            }
        }

        if (Type == "Note" && AppendOnly == "TRUE")
        {
            AlertAndSetFocus(L_alert25_Text, frm.AppendOnly[1]);
            frm.AppendOnly[1].checked=1;
            return false;
        }

        if (Type == "Choice" && Format == "Checkboxes")
        {
            Type = "MultiChoice";
            Format = "";
        }
        if (Type == "GridChoice")
        {
            if (GridTxtRng1.length > 150)
            {
                alert(L_alert22_Text);
                frm.GridTxtRng1.focus();
                return false;
            }
            if (GridTxtRng2.length > 150)
            {
                alert(L_alert22_Text);
                frm.GridTxtRng2.focus();
                return false;
            }
            if (GridTxtRng3.length > 150)
            {
                alert(L_alert22_Text);
                frm.GridTxtRng3.focus();
                return false;
            }
              if (frm.GridShowNA.checked)
              {
                if (GridNATxt.length > 150)
                {
                    alert(L_alert23_Text);
                    frm.GridNATxt.focus();
                    return false;
                }
            }
            else
            {
                GridNATxt="";
            }
            if (GridStartNum != 1 || GridEndNum < 3 || GridEndNum > 20)
            {
                alert(L_alert20_Text);
                frm.GridNumRange.focus();
                return false;
            }
            var GridWidth   = GridEndNum - GridStartNum;
            if (GridWidth < 2)
            {
                alert(L_alert20_Text);
                frm.GridNumRange.focus();
                return false;
            }
        }
        var Schema = ('<Field ' +
            (Name         ? 'Name="'        + SimpleHTMLEncodeMultiLine(Name)+ '" ' : '') +
            (FromBaseType ? 'FromBaseType="'+ SimpleHTMLEncode(FromBaseType) + '" ' : '') +
            (DisplaySize  ? 'DisplaySize="' + DisplaySize                    + '" ' : '') +
            (Type         ? 'Type="'        + SimpleHTMLEncode(Type)         + '" ' : '') +
            (LookupList   ? 'List="'        + SimpleHTMLEncode(LookupList)   + '" ' : '') +
            (LookupField  ? 'ShowField="'   + SimpleHTMLEncode(LookupField)  + '" ' : '') +
            (DisplayName  ? 'DisplayName="' + SimpleHTMLEncodeMultiLine(DisplayName)+ '" ' : '') +
            (Description  ? 'Description="' + SimpleHTMLEncode(Description)  + '" ' : '') +
            (Required.length > 0 ? 'Required="'    + SimpleHTMLEncode(Required)     + '" ' : '') +
            (EnforceUniqueValues.length > 0 ? 'EnforceUniqueValues="' + SimpleHTMLEncode(EnforceUniqueValues) + '" ' : '') +
            (Indexed.length > 0 ? 'Indexed="'    + SimpleHTMLEncode(Indexed)     + '" ' : '') +
            (UnlimitedLengthInDocumentLibrary ? 'UnlimitedLengthInDocumentLibrary="'    + SimpleHTMLEncode(UnlimitedLengthInDocumentLibrary)     + '" ' : '') +
            (NumLines     ? 'NumLines="'    + SimpleHTMLEncode(NumLines)     + '" ' : '') +
            (Format       ? 'Format="'      + SimpleHTMLEncode(Format)       + '" ' : '') +
            (MaxLength    ? 'MaxLength="'   + MaxLength                      + '" ' : '') +
            (Min          ? 'Min="'         + Min                            + '" ' : '') +
            (Max          ? 'Max="'         + Max                            + '" ' : '') +
            (Percentage   ? 'Percentage="'  + Percentage                     + '" ' : '') +
            (RichText     ? 'RichText="'    + RichText                       + '" ' : '') +
            (RichTextMode ? 'RichTextMode="' + RichTextMode                  + '" ' : '') +
            (RichTextMode == 'FullHtml' ? 'IsolateStyles="TRUE" ' : '') +
            (AllowHyperlink ? 'AllowHyperlink="' + AllowHyperlink            + '" ' : '') +
            (AppendOnly   ? 'AppendOnly="'  + AppendOnly                     + '" ' : '') +
            (Decimals     ? 'Decimals="'    + Decimals                       + '" ' : '') +
            (LCID         ? 'LCID="'        + LCID                           + '" ' : '') +
            (CalType      ? 'CalType="'     + CalType                        + '" ' : '') +
            (Direction    ? 'Direction="'   + Direction                      + '" ' : '') +
            (FillInChoice ? 'FillInChoice="'+ FillInChoice                   + '" ' : '') +
            (ResultType   ? 'ResultType="'  + SimpleHTMLEncode(ResultType)   + '" ' : '') +
            (Type=="Note" ? 'Sortable="FALSE" '                                     : '') +
            (DefaultURLDesc ? 'DefaultURLDesc="' + SimpleHTMLEncode(DefaultURLDesc) + '" ' : '') +
            (ReadOnly     ? 'ReadOnly="'    + ReadOnly                       + '" ' : '') +
            (ShowPresence ? 'Presence="'    + ShowPresence                   + '" ' : '') +
            (GroupName    ? 'Group="'       + SimpleHTMLEncode(GroupName)    + '" ' : '') +
            (FriendlyFormat    ? 'FriendlyDisplayFormat="'       + SimpleHTMLEncode(FriendlyFormat)    + '" ' : '') +
            (GridStartNum ? 'GridStartNum="'+ GridStartNum                   + '" ' : '') +
            (GridEndNum   ? 'GridEndNum="'  + GridEndNum                     + '" ' : '') +
            (GridTxtRng1  ? 'GridTxtRng1="' + SimpleHTMLEncode(GridTxtRng1)  + '" ' : '') +
            (GridTxtRng2  ? 'GridTxtRng2="' + SimpleHTMLEncode(GridTxtRng2)  + '" ' : '') +
            (GridTxtRng3  ? 'GridTxtRng3="' + SimpleHTMLEncode(GridTxtRng3)  + '" ' : '') +
            (GridNATxt    ? 'GridNATxt="'   + SimpleHTMLEncode(GridNATxt)    + '" ' : '') +

            '>'
        );
        if(DefaultFormula) {
            if (DefaultFormula.charAt(0) != '=')
            {
                DefaultFormula = "="+DefaultFormula;
            }
            Schema += '<DefaultFormula>' + SimpleHTMLEncode(DefaultFormula) + '</DefaultFormula>';
        }else if(Default)
        {
            Schema += '<Default>' + SimpleHTMLEncode(Default) + '</Default>';
        }
        if (Type == "Calculated")
        {
            if (!CheckForIllegals(Formula, false))
                return false;
            if (Formula.charAt(0) != '=')
            {
                Formula = "="+Formula;
            }
            Schema += '<Formula>' + SimpleHTMLEncode(Formula) +'</Formula>';
        }
        if (Type != "MultiChoice")
        {
            if (VldFormula)
            {
                if (!CheckForIllegals(VldFormula, true))
                    return false;
                if (VldFormula.charAt(0) != '=')
                    VldFormula = "="+VldFormula
                if (VldFormula.length > 1024)
                {
                    alert(String.format(L_alert26_Text, 1024));
                    elemVldFormula.focus();
                    return false;
                }
                bHaveValidation = true;
            }
            if (VldFormula || VldMessage)
            {
                Schema += '<Validation';
                if (VldMessage)
                    Schema += ' Message="' + SimpleHTMLEncode(VldMessage) + '"';
                Schema += '>' + SimpleHTMLEncode(VldFormula) + '</Validation>';
            }
        }
        if(Type == "Choice" || Type == "MultiChoice" || Type == "OutcomeChoice")
        {
            re = /\\r\\n+/g;
            Choices = Choices.replace(re, "\\r\\n");
            if (TrimWhiteSpaces(Choices) == "")
            {
                alert(L_alert13_Text);
                frm.Choices.focus();
                return false;
            }
            if (-1 != Choices.indexOf(";#"))
            {
                alert(L_alert19_Text);
                frm.Choices.focus();
                return false;
            }
            var ChoicesArr = Choices.split( /[\r\n]+/ig );
            var bFoundMatchWithDefault = false;
            Schema += '<CHOICES>';
            for( i = 0; i < ChoicesArr.length; i ++ )
            {
                var Choice = TrimWhiteSpaces( ChoicesArr[i] );
                if(Choice) {
                    if(Choice.length > 255)
                    {
                        alert(L_alert14_Text);
                        frm.Choices.focus();
                        return false;
                    }
                    if (Default == Choice)
                        bFoundMatchWithDefault = true;
                    Schema += '<CHOICE>' + SimpleHTMLEncode(Choice) + '</CHOICE>';
                }
            }
            Schema += '</CHOICES>';
            if (Default && !bFoundMatchWithDefault)
            {
                alert(L_alert15_Text);
                frm.DefaultText.focus();
                return false;
            }
        }
        if(Type == "GridChoice")
        {
            if (TrimSpaces(GridChoices) == "")
            {
                alert(L_alert13_Text);
                frm.GridChoices.focus();
                return false;
            }
            if (-1 != GridChoices.indexOf(";#"))
            {
                alert(L_alert19_Text);
                frm.GridChoices.focus();
                return false;
            }
            var GridChoicesArr = GridChoices.split( /[\r\n]+/ig );
            if (GridChoicesArr.length > 50)
            {
                alert(L_alert19A_Text);
                frm.GridChoices.focus();
                return false;
            }
            Schema += '<CHOICES>';
            for( i = 0; i < GridChoicesArr.length; i ++ )
            {
                var Choice = TrimSpaces( GridChoicesArr[i] );
                if(Choice)
                {
                    if(Choice.length > 255)
                    {
                        alert(L_alert14_Text);
                        frm.GridChoices.focus();
                        return false;
                    }
                    Schema += '<CHOICE>' + SimpleHTMLEncode(Choice) + '</CHOICE>';
                }
            }
            Schema += '</CHOICES>';
        }
        Schema += '</Field>';

        document.frmSubmit.FieldName.value        = DisplayName;
        document.frmSubmit.FieldXML.value         = Schema;
    }
    document.frmSubmit.AddToDefaultView.value = AddToDefVw;
    document.frmSubmit.AddToAllContentTypes.value = AddToAllContentTypes;
    document.frmSubmit.PushDown.value = PushDown;
    SubmitButtonOnClick(btn);
    document.frmSubmit.submit();
    return true;

    },
    error: function (data) {

    }
  });


    }

);

});

I could find no tricks to solve it. Anyone can suggest the ways to resolve it.!

Thanks.

  • is there a console.log in your code? – dsouzajoel22 Jan 17 '17 at 7:51
  • can you post your code ? – Gautam Sheth Jan 17 '17 at 7:59
  • There is no console.log @ dsouzajoel22 – NivethaSri Jan 17 '17 at 8:02
  • Sure I'll post It @ Gautam – NivethaSri Jan 17 '17 at 8:02
4

I copied your code in my IDE (and could not spot an obvious single error)

You have to learn/do:

  • Use a decent Javascript IDE that has a syntax checker.
    Your code has so many flaws it is impossible to list them all.
    Visual Studio Code is free: https://code.visualstudio.com/

  • split your code,
    Your .success function has 338 lines of code,
    that makes it impossible for you to keep on overview of what it does.
    Add functions with long descriptive function-names, preferably no more than 7 to 10 lines of code,
    It will make it easier for you to read what your code does and allows for easy disabling of code.

  • Learn to use the F12 Debugger, and step through your code line by line

  • Or add console.debug('read data') like statements at strategic lines in your code to isolate where your code goes wrong.

Note

The console Object is ONLY available in Internet Explorer when the F12 Console is OPEN
That means any console statement left in your code will cause an error

Sort of a Catch-22 situation; you won't see that error because the console is closed,
and the error will not happen when the console is open...

You can 'PolyFill` the console by adding this to the head of your code:

(function (o) {
    if (o.__ts__) return;//if already executed
    var slice = Array.prototype.slice;//pointer for faster access
    ['log', 'debug', 'info', 'warn', 'error'].map(function (f) {
        var _ = o[f];
        o[f] = function () {
            try {
                var args = slice.call(arguments);
                args.unshift(new Date().toISOString().split('T')[1]);
                return _.apply(o, args);
            } catch (e) {
                console.warn(e, f);
            }
        };
    });
    o.__ts__ = true;
})(console);

more: https://www.google.nl/search?q=console+polyfill

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.