JavaScript CMS tricks: Search user database from anywhere on site

velociraptor
They will eat you if you don’t give them what they want. Credit: Mike/flickr

Let’s say you’re a cowboy, and you have a herd of wild velociraptors that also happen to be registered members of a website you manage. On occasion, you’ve got to edit their user profiles, but you hate going to the user profile landing page, typing in a name, clicking search and waiting.

What you need is a bookmark that, when clicked, asks you for a name and just does the rest.

Now you’ve got one.

Remember it only works if you click on the bookmark while you’re on the site you manage; otherwise a cross-domain security conflict arises.

You’ll need to:

1) Make a bookmark that reads like this: javascript:(function(){document.body.appendChild(document.createElement(‘script’)).src=’http://www.xyz.com/yourJavaScriptFile.js’;})();

2) Make your JS file that gets appended to the page upon clicking the bookmark. That’s JS File 1 in the JS CodePen tab below.

3) Make the JS file that gets appended to the iframe page that gets created.. Yep, JS File 2 below.

Details on how to do all that below:

/* ###

There are two JS files. 

*/

/* ### JS File 1 ### */

// Make a variable that holds a new <iframe> element. 
var ifrm = document.createElement("IFRAME"); 

// Choose the page you want to load into the <iframe>
ifrm.setAttribute("src", "http://www.monkeyseatkoalas.com"); 

// A bit of styling of the iframe. Lay it out as you wish. I choose width of 100% because I want it to operate like a second window.
ifrm.style.width = 100+"%"; 
ifrm.style.height = 600+"px"; 

// Attach the new <iframe> to the body of the page we're on. Caveats: <iframe> and goofing about with it only works when you keep everything in one domain. 
document.body.appendChild(ifrm);

// Now we have an iframe on our document. It has a position of [0]. So let's now do something odd -- let's attach ANOTHER JavaScript file to this new iframe page. Why? Because that makes it easy to manipulate that page  with a fresh JS file. Here you go: 
frames[0].document.body.appendChild(frames[0].document.createElement('script')).src='http://www.bigstoragethingy.com/yourOtherJSTrickeryFile.js';


/* ######

#########
########## JS File 2 ### 

#### */ 

// We don't want to do anything until the whole beast is loaded. Otherwise, we might not be able to access somthing we want, or calculations we do based upon elements in the page might not work. Still, it's your call. You might be able to run this at an earlier stage in the load. 
window.onload = function () {

  // Looky there at that .forms. w00t! Handy! In the brackets, we have the form's ID. If it doesn't have one, go ahead and give it one, and make your life easier. How? Hmm. I think document.forms[numberOfFormElementOnThePage].setAttribute('id', 'mySpecialCupcakeFormCheeseName');
  var getDatForm = document.forms["IDofYourForm"];

  // Well, we need to find and store the box we want to populate with something. So we just use our form, nicely stuck in a variable, and do that super-handy elements[x] to it. In a form, this refers to the <input> tag. So the first <input> tag in your form will be yourForm.elements[0].
  var getDatSearchBox = getDatForm.elements[1];

  // Gettin' all interactivey here. Yep, ask your friendly Web user to provide something he/she wants to find. Stick it in a variable.
  var nameToFind = prompt('Word to hunt for? Name? Ice cream? Doggy?');

  // Update your search box with that text. 
  getDatSearchBox.value=nameToFind;

  // BOOM! Submit your form. 
  document.forms["users_search"].submit();
};

See the Pen JavaScript CMS tricks: Search user database from anywhere on site by tumolillo (@hellotumo) on CodePen.

1 thought on “JavaScript CMS tricks: Search user database from anywhere on site”

  1. I found this page doing a search for JavaScript based CMSs. I’m trying to get the word out about an open source project, ConnextCMS. It’s a native JavaScript CMS.

    Reply

Leave a Reply to Chris Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.