4

I have the following Javascript code embedded in my side row

window.onload = new function()
{
hideBtns();
}
function hideBtns()
{
if(document.getElementsByName('edit')[0]!=null){
document.getElementsByName('edit')[0].style.display = 'none';}

if(document.getElementsByName('edit')[1]!=null){
document.getElementsByName('edit')[1].style.display = 'none';}
}

Inspecting the edit button in chrome o/p :

<input value=" Edit " class="btn" title="Edit" name="edit" onclick="navigateToUrl('/001d000000JXhAX/e?retURL=%2F001d000000JXhAX','DETAIL','edit');" type="button">

The script is being called , though I see that the name of the "Edit" button is "edit" when I inspect it is not entering the if(document.getElementsByName('edit')[0]!=null){ block !!!

Any pointers will be a great help!!!

Ryan Guest
  • 3,368
  • 1
  • 22
  • 35
Rao
  • 16,691
  • 13
  • 68
  • 108
  • Is there a reason you're not just removing the edit button from the page layout? – Matt Lacey Dec 20 '12 at 01:10
  • yes i want to hide the button based on a field value!!! so this is the first part where i want to just hide the button and start putting conditions later – Rao Dec 20 '12 at 01:20
  • I can execute that JavaScript directly from the Chrome Conosle w/o error on the Account detail page that has an edit button, so not sure exactly what's going on. I'ts probably not a good idea to override the window.onload function. You should be able to use JS to append to it, e.g., multiple onload handlers or use jQuery to abstract that level of detail out. Without your code in place, you can execute window.onload from the Chrome Console and see that SF has something defined already. – Peter Knolle Dec 20 '12 at 01:58
  • 3
    If you want don't want to have users editing a record when a field is a specific value, you could look at locking the record. Here's one way to do that: http://verticalcode.wordpress.com/2012/07/26/locking-a-record-from-editing/. – Daniel Hoechst Dec 20 '12 at 02:52
  • Peter i am getting no errors too, but the problem is te code is not entering document.getelementbyname('edit') !!! am i doing something wrng r is there a better way to hide button. – Rao Dec 20 '12 at 03:12
  • 1
    daniel let me find out if its ok to lock record. sounds nice to do it tht simple, – Rao Dec 20 '12 at 03:13
  • Try replacing single quotes with double quotes for ids. Something like this - var eles = document.getElementsByName("edit"); for(var i=0;i<eles.length;i++){ eles[i].style.display = "none"; } – Tanuj Kumar Sharma Dec 20 '12 at 08:09
  • 3
    This should sort you out , probably beat using a Jquery selector with document.ready(), which I tend to rely more on than window.onload http://www.tehnrd.com/show-and-hide-buttons-on-page-layouts/ – techtrekker Dec 20 '12 at 09:59
  • @techtrekker can you please post it as an answer !!! It was a freaking magical solution that could help many people !!! – Rao Dec 20 '12 at 17:36

1 Answers1

1

I am afraid you are likely out of luck. If you are using a Visualforce Area to place the javascript in your sidebar, Salesforce accomplishes this by placing your code in an iframe. This sandboxes the component and prevents it from accessing any other parts of the page. There is another post here detailing the changes Salesforce made to javascript in the sidebar: End of javascript sidebar workarounds?.

dsharrison
  • 3,992
  • 20
  • 55