JavaScript CMS tricks: Search user database from anywhere on site

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 &lt;iframe&gt; element.
var ifrm = document.createElement(&quot;IFRAME&quot;);
// Choose the page you want to load into the &lt;iframe&gt;
ifrm.setAttribute(&quot;src&quot;, &quot;http://www.monkeyseatkoalas.com&quot;);
// 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+&quot;%&quot;;
ifrm.style.height = 600+&quot;px&quot;;
// Attach the new &lt;iframe&gt; to the body of the page we&#x27;re on. Caveats: &lt;iframe&gt; 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&#x27;s now do something odd -- let&#x27;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(&#x27;script&#x27;)).src=&#x27;http://www.bigstoragethingy.com/yourOtherJSTrickeryFile.js&#x27;;
/* ######
#########
########## JS File 2 ###
#### */
// We don&#x27;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&#x27;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&#x27;s ID. If it doesn&#x27;t have one, go ahead and give it one, and make your life easier. How? Hmm. I think document.forms[numberOfFormElementOnThePage].setAttribute(&#x27;id&#x27;, &#x27;mySpecialCupcakeFormCheeseName&#x27;);
var getDatForm = document.forms[&quot;IDofYourForm&quot;];
// 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 &lt;input&gt; tag. So the first &lt;input&gt; tag in your form will be yourForm.elements[0].
var getDatSearchBox = getDatForm.elements[1];
// Gettin&#x27; 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(&#x27;Word to hunt for? Name? Ice cream? Doggy?&#x27;);
// Update your search box with that text.
getDatSearchBox.value=nameToFind;
// BOOM! Submit your form.
document.forms[&quot;users_search&quot;].submit();
};