Trigger edit bar event

Permalink
Is it possible to let a trigger event put a page in edit when a url parameter (like ?edit is detected?

I've put together the following script but for some reason that I don't know about, it won't work.
$(document).ready(function() {
var myElem = $('#ccm-nav-edit');
   if (myElem != null) { 
      if (window.location.search.indexOf('edit') > -1) {
         myElem.trigger('click');
      }
   } 
});

GreyhorseDesign
 
JohntheFish replied on at Permalink Reply
JohntheFish
I don't know your absolute answer.

Assuming it is actually possible, your script looks OK.

At a guess, maybe the edit button is not ready to receive the event at the time you are triggering it because your ready handler is earlier in the page. If that is the case, a simple 100ms delay within your handler would allow any other ready handlers to run before your delayed handler completes.
GreyhorseDesign replied on at Permalink Reply
GreyhorseDesign
Thank you for the input! I've put the handler inside a setTimeout method set to be delayed up to 2000ms but without any result.
Also placed this script all the way down the source file to be sure it's loaded last.
JohntheFish replied on at Permalink Reply
JohntheFish
Looking at the edit button in the developer console, the main parts are an href and an onClick handler.

You could check with console.log(myElem) just to make sure you are actually catching the right element.

If triggering a click won't work, perhaps you could scrape and duplicate the action by running equivalent script to the onClick() handler then directly navigating to the href.

The overall mechanism looks like it could be useful for developers wanting to get directly into editing a page they (we => me ;)) have just broken as an alternative to reverting the page version.
GreyhorseDesign replied on at Permalink Reply
GreyhorseDesign
This works, but I don't know if it's the proper way to do it.

$(document).ready(function() {
   var myElem = $('#ccm-nav-edit');
   if (window.location.search.indexOf('edit') > -1) {
      if (myElem != null) { 
         var editURL = myElem.attr("href");
         window.location = editURL;
      }
   }
});


Edit: This should be placed at the bottom of the page beneath
<?php Loader::element('footer_required'); ?>