Home » SharepointRSS

Javascript fails in chrome and firefox, works fine in IE

Hi,

I am customizing Newform.aspx i.e. hiding some fields, making other readonly etc. It works fine in IE but fails miserably in chrome and firefox i.e. readonly are no more readonly, hidden are no more hidden. Here is my code:

<script language="javascript" type="text/javascript">
    _spBodyOnLoadFunctionNames.push("autoFillFields");
    function autoFillFields()
    {
       var ctrl = findacontrol("Reference Customer");
    var param = getURLParam('RefFromCust');   
    var properParam = replaceCharacters(param);
    ctrl.value = properParam;
   
    var ctrl = findacontrol("RefReqOpptyId");
    var param = getURLParam('OpptyId');   
    var properParam = replaceCharacters(param);
    ctrl.value = properParam;

   
    param = getURLParam('RefFromAE');
    properParam = replaceCharacters(param);
    setPeoplePicker("People Picker", properParam);
   
    hideFldCtrl("Approval Status");
    hideFldCtrl("Email Status");
    hideFldCtrl("Request Created");
    hideFldCtrl("Sales Manager");
    hideFldCtrl("Opportunity Description");
    hideFldCtrl("Email Template");
    hideFldCtrl("AEForRefCustomer Comments");
    hideFldCtrl("Post Meeting Summary Link");
    hideFldCtrl("RefReqOpptyId");
    hideFldCtrl("Due Date");
       hideFldCtrl("Extended Due Date");
       hideFldCtrl("Check Names");
       hideFldCtrl("NotifyManager Date");
       hideFldCtrl("NoActionTaken Date");
       hideFldCtrl("WorkflowJobInterval");
    makeReadOnly("Reference Customer");
   
       
    $(document).ready(function(){$('a').each(function(){if($(this).attr('title') == "Browse")$(this).hide();});}); 
 }
   
    function getURLParam(name)
 {
   name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
   var regexS = "[\\?&]"+name+"=([^&#]*)";
   var regex = new RegExp( regexS );
   var results = regex.exec( window.location.href );
   if( results == null )
     return "";
   else
     return results[1];
 }
 
 function replaceCharacters(str)
 {
    str = str.replace(/\+/g, " ");
    str = str.replace(/%2c/g, ",");
    str = str.replace(/%20/g, " ");
    return str;  
 }
 
    function findacontrol(FieldName)
    {
        var arr = document.getElementsByTagName("input");
        for (var i = 0; i < arr.length; i++)
        {
            if (arr[i].title == FieldName)
            { return arr[i]; }
        }
    }
   
    function setPeoplePicker(fieldName, value)
    {
   if (value == undefined) return;
   var assignedToInput = getTagFromIdentifierAndTitle("div", "",fieldName);
   assignedToInput.innerHTML = value;
  
 }

    function getTagFromIdentifierAndTitle(tagName, identifier, title)
    {
    var len = identifier.length;
    var tags = document.getElementsByTagName(tagName);
   for (var i=0; i < tags.length; i++)
   {
      var tempString = tags[i].id;
      if (tags[i].title == title && (identifier == "" || tempString.indexOf(identifier) == tempString.length - len))
      {
         return tags[i];
        }
    }
    return null;
 }
 
 function hideFldCtrl(title) {
        var ctrl = findcontrolbycomment(title);
        if (ctrl) ctrl.parentNode.parentNode.style.display = "none";
    }
   
    function findcontrolbycomment(FieldName) {
        var arr = document.getElementsByTagName("!");
        for (var i = 0; i < arr.length; i++) {
            if (arr[i].innerHTML.indexOf(FieldName) > 0)
            { return arr[i]; }
        }
    }
   
    function makeReadOnly(title) {
        var ctrl = findacontrol(title);
        if (ctrl) ctrl.readOnly = true;
        ctrl.style.color = '736F6E';
              
    }   
   
    function disablePeoplePickers(tagName, identifier, title)
    {
    var len = identifier.length;
    var tags = document.getElementsByTagName(tagName);
   for (var i=0; i < tags.length; i++)
   {
      var tempString = tags[i].id;
      if (tags[i].title == title && (identifier == "" || tempString.indexOf(identifier) == tempString.length - len))
      {
         tags[i].disabled = true;
        }
    }
    return null;
  } 
</script>

Any suggestions would be highly appreciated.

Thanks,
Rehman.

 

5 Answers Found

 

Answer 1

I only tested this in FireFox, but I think your problem might be in your findControlByComment function. If you change it to

function findcontrolbycomment(FieldName) {

    alert("Finding control by comment: " + FieldName);

    var arr  = document.getElementsByTagName("!");

    alert("Comments Found: " + arr.length);
    /* arr.length is always 0 in FireFox*/for (var i = 0; i < arr.length; i++) {
      if (arr[i].innerHTML.indexOf(FieldName) > 0)
      { return arr[i]; }
    }
  }

 

You will see that document.getElementsByTagName("!") doesn't behave the same in FireFox as it does in IE.

HTH

 

 

Answer 2

Try this URL

http://sharepointsherpa.com/2008/08/26/sharepoint-2007-hiding-fields-on-newformaspx-and-editformaspx-the-easy-way/

Another option you have is Convert that list form to XSLT like i explained in this blog.http://www.c-sharpcorner.com/UploadFile/Roji.Joy/2713/

If you conver to XSLT you will get more control on each field

 

 

Answer 3

Thanks Paul, its exactly the cause, however I can find the following from the page source in Firefox:

<!-- FieldName="Reference Customer"
			 FieldInternalName="Title"
			 FieldType="SPFieldText"
		 -->

why is the comment not found...the only difference is the comment is all italic in firefox  and plane (non italic) in IE.

Any suggestions.

Thanks once again,
Rehman.

 

Answer 4

Hi Rehman,

I think this is probably to do with difference in the DOM parser in each browser. I see you had some jQuery in your original post.. you could try modifying the code  for hideFldCtrl and findcontrolbycomment as follows

function hideFldCtrl(title) {
  var ctrl  = findcontrolbycomment(title);
  //since findcontrolbycomment returns a td, only go up one nodeif (ctrl) ctrl.parentNode.style.display = "none";
 }
 
function findcontrolbycomment(FieldName) {

  var search="FieldName=\"" + FieldName + "\"";
  var fieldTD = null;
  $("TD.ms-formbody").each(function(){
   var tdHtml = $(this).html();
   var regex  = new RegExp(search, "gi");
   if(regex.test(tdHtml)){
    fieldTD = this;    
   }   
  });
  //returns a td, not a comment nodereturn fieldTD;
}

 

 

 

Answer 5

Thanks Paul, much appreciated...

Here is my modified code. It works for IE, Firefox and Chrome (shd work for Safari as well, as it behaves much like Chrome does).

Put this code  on line 70 of NewForm.aspx....

"Post Meeting Summary Link" is a Rich Text field, so needed one extra "parentNode" to be hidden.

can also make fields  readonly using this code.

<script language="javascript" type="text/javascript">
 _spBodyOnLoadFunctionNames.push("autoFillFields");
 function autoFillFields() 
 {
  
	 hideFldCtrl("Approval Status");
	 hideFldCtrl("Email Status");
	 hideFldCtrl("Request Created");
	 hideFldCtrl("Opportunity Description");
	 hideFldCtrl("Email Template");
	 hideFldCtrl("AEForRefCustomer Comments");
	 hideFldCtrl("Post Meeting Summary Link");
	 hideFldCtrl("RefReqOpptyId");
	 hideFldCtrl("Due Date");
  hideFldCtrl("Extended Due Date");
  hideFldCtrl("NotifyManager Date");
  hideFldCtrl("NoActionTaken Date");
  hideFldCtrl("WorkflowJobInterval");
  hidePeoplePicker("nobr", "Sales Manager");
  makeReadOnly("Reference Customer"); 
	 
	 	 
	}
 
 	
  
 function getTagFromIdentifierAndTitle(tagName, identifier, title) 
 {
 		var len  = identifier.length;
 		var tags  = document.getElementsByTagName(tagName);
 		for (var i=0; i < tags.length; i++) 
 		{
	 	var tempString = tags[i].id;
	 	if (tags[i].title == title  && (identifier == "" || tempString.indexOf(identifier) == tempString.length - len)) 
	 	{
  			return tags[i];
  		}
 		}
 		returnnull;
	}
	
	
function hideFldCtrl(title) 
	{
  var ctrl  = getTagFromIdentifierAndTitle("input", "TextField", title);
  if (ctrl) ctrl.parentNode.parentNode.parentNode.style.display = "none";
  ctrl = getTagFromIdentifierAndTitle("select", "DropDownChoice", title);
  if (ctrl) ctrl.parentNode.parentNode.parentNode.style.display = "none";
  ctrl = getTagFromIdentifierAndTitle("TextArea", "TextField", title);
  if (ctrl) 
  {
  	if (title == "Post Meeting Summary Link")
  		ctrl.parentNode.parentNode.parentNode.parentNode.style.display = "none";
			else
			 ctrl.parentNode.parentNode.parentNode.style.display = "none";
  }
  ctrl = getTagFromIdentifierAndTitle("input", "DateTimeField_DateTimeFieldDate", title);
  if (ctrl) ctrl.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.style.display = "none";  
  } 
 
 function  findacontrol(FieldName) 
    {
        var  arr = document.getElementsByTagName("input");
        for (var i = 0; i < arr.length; i++)
        {
            if (arr[i].title == FieldName)
            { return  arr[i]; }
        }
    }   
function makeReadOnly(title) { var ctrl = findacontrol(title); if (ctrl) ctrl.readOnly = true; ctrl.style.color = '736F6E'; } function hidePeoplePicker(tagName, title) { var tags = document.getElementsByTagName(tagName); for (var i=0; i < tags.length; i++) { if (tags[i].innerHTML == title) tags[i].parentNode.parentNode.parentNode.style.display = "none"; } } </script>

Rehman Gul
 
 
 

<< Previous      Next >>


Microsoft   |   Windows   |   Visual Studio   |   Follow us on Twitter