Injecting Elements After Load

Posted by John
on Tuesday, 12 February 2008

Here's a javascript technique which you might need down the line and which helped me out a lot when handling a ppc event. Basically the code below allows you to execute a script that would normally be run on load, but here we run it when we want to; because say in this case you don't want to reload the page just for an extra script.

Injecting Scripts After Load

So,

var oScript = document.createElement("script");
oScript.src = "signed_up_user.js";
document.body.appendChild(oScript);

What this does is create a new script element and append it to the current page.

The createElement() bit defines what kind of object you're going to create,

  • img => image
  • a => hyperlink
  • script => javascript

...giving you all the properties related to that object, so to set the script for a script object we do...

oScript.src = "signed_up_user.js";

The appendChild injects the element into the area you specify within the DOM model, in this case the body.

!! Document.Write !!

Important note, you can't fire a Document.Write after the page has loaded, just won't work.

To add elements to your page use document.body.appendChild or Prototype / MooTools, which add some really efficient handlers for you to do this even faster and cross-browser.

Injecting Images After Load

We can also add an image generated off of some ppc script we've been given (big help if you've got googleppc to handle), like...

var oScript = document.createElement("img");
oScript.height = "1";
oScript.width = "1";
oScript.src = "some_ppc_script_function()";
document.body.appendChild(oScript);

In this case our image element is generated with the appropriate width & height, and then the source is put in from our ppc function that returns back an image element and injects it into the page.

All without refreshing the page, but more importantly when you add that script to the page after-load it IS processed!.

Hardcore AJAX ;-)

Have fun!

Beginning NGINX

Posted by John
on Wednesday, 06 February 2008

In this series of articles I'll explain how to install and setup the super light and fast NGINX webserver on your Linux box and get it to host rails apps and maybe a little extra.

First off let's install NGINX

To install the latest copy of NGINX you're gonna need to build from source so make sure you install the build-essentials (gcc), to do this run...

sudo aptitude install build-essential

Now you've got the GCC compiler installed you can build from source, so let's download the latest copy of NGINX...

First some dependencies,

sudo aptitude install libpcre3 libpcre3-dev libpcrecpp0 libssl-dev zlib1g-dev

Now the bad boy himself,

cd ~/sources/
wget http://sysoev.ru/nginx/nginx-0.5.35.tar.gz
tar -zxvf nginx-0.5.35.tar.gz
cd nginx-0.5.35/

now configure the source,

./configure --sbin-path=/usr/local/sbin --with-http_ssl_module

it'll finish with a summary of locations like,

nginx path prefix: "/usr/local/nginx"
nginx binary file: "/usr/local/sbin"
nginx configuration file: "/usr/local/nginx/conf/nginx.conf"

write these down before you continue, very important!

now build,

make

and install

sudo make install

Running NGINX

As those last summary lines told us, nginx lives in -> /usr/local/sbin/nginx, so let's go start it,

sudo /usr/local/sbin/nginx

now if you navigate to your boxes ip address you should see a fancy 'welcome to nginx' message, wahey! you have it installed.

Final part, startup scripts

Now the next script I'm very thankful for PickledOnion over at Slicehost.com for providing.

First off let's create an init script so we can start it more nicely and NGINX will start on reboot, so...

sudo nano /etc/init.d/nginx

And now copy & paste this init script into nano...

#! /bin/sh

### BEGIN INIT INFO
# Provides:          nginx
# Required-Start:    $all
# Required-Stop:     $all
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: starts the nginx web server
# Description:       starts nginx using start-stop-daemon
### END INIT INFO

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/local/sbin/nginx
NAME=nginx
DESC=nginx

test -x $DAEMON || exit 0

# Include nginx defaults if available
if [ -f /etc/default/nginx ] ; then
        . /etc/default/nginx
fi

set -e

case "$1" in
  start)
        echo -n "Starting $DESC: "
        start-stop-daemon --start --quiet --pidfile /usr/local/nginx/logs/$NAME.pid \
                --exec $DAEMON -- $DAEMON_OPTS
        echo "$NAME."
        ;;
  stop)
        echo -n "Stopping $DESC: "
        start-stop-daemon --stop --quiet --pidfile /usr/local/nginx/logs/$NAME.pid \
                --exec $DAEMON
        echo "$NAME."
        ;;
  restart|force-reload)
        echo -n "Restarting $DESC: "
        start-stop-daemon --stop --quiet --pidfile \
                /usr/local/nginx/logs/$NAME.pid --exec $DAEMON
        sleep 1
        start-stop-daemon --start --quiet --pidfile \
                /usr/local/nginx/logs/$NAME.pid --exec $DAEMON -- $DAEMON_OPTS
        echo "$NAME."
        ;;
  reload)
          echo -n "Reloading $DESC configuration: "
          start-stop-daemon --stop --signal HUP --quiet --pidfile     /usr/local/nginx/logs/$NAME.pid \
              --exec $DAEMON 
          echo "$NAME."
          ;;
      *)
            N=/etc/init.d/$NAME
            echo "Usage: $N {start|stop|restart|reload|force-reload}" >&2
            exit 1   
            ;;
    esac

    exit 0

Yes it's a monster, I've copied it over to my server so you can grab it at..

Now lets use it with...

sudo chmod +x /etc/init.d/nginx
sudo /usr/sbin/update-rc.d -f nginx defaults

You should now see...

Adding system startup for /etc/init.d/nginx ...
   /etc/rc0.d/K20nginx -> ../init.d/nginx
   /etc/rc1.d/K20nginx -> ../init.d/nginx
   /etc/rc6.d/K20nginx -> ../init.d/nginx
   /etc/rc2.d/S20nginx -> ../init.d/nginx
   /etc/rc3.d/S20nginx -> ../init.d/nginx
   /etc/rc4.d/S20nginx -> ../init.d/nginx
   /etc/rc5.d/S20nginx -> ../init.d/nginx

Now NGINX will startup on reboot and you can run these commands to control it better.

Start

sudo /etc/init.d/nginx start

Stop

sudo /etc/init.d/nginx stop

Restart

sudo /etc/init.d/nginx restart

Next up i'll put together the nginx scripts I use myself which should help you out a lot when hosting your site with this great tool.

Final Note

On a later note you may get times when you change your NGINX .conf scripts restart NGINX and it doesn't seem to have taken your latest config changes, I get this myself sometimes.

What you can do is brute-force kill the NGINX process and restart it with...

ps aux | grep nginx

which will return the process id NGINX is running at, then kill it with...

kill [processid]

and now start NGINX from fresh,

sudo /etc/init.d/nginx start

.Conf Templates

For your info and more so the guys who have working NGINX setups, I've put example config files in my downloads area, with direct links here....

Keeping things backed up

Posted by John
on Tuesday, 11 September 2007

One thing you can never plan too well is the eventual loss of data from your primary machine, aka your laptop.

Via Terminal

So realising events from the past I cobbled together some RSYNC commands to backup my laptop’s most important files over to Strongspace.

To use them, you’ll need to create a .ssh directory in your strongspace account (use an SFTP program to do this) and create a .pub public key file and put it on your remote server (strongspace) to upload to it.

Create a key by running,

ssh-keygen -t dsa

This will create a id_dsa.pub file, upload this to your strongspace .ssh/ directory and rename it as authorized_keys without the extension.

Once done you’ll be able to run these thru to your remote storage center in the terminal window seamlessly (more so if you don’t set a password for your key files).

For now though, here’s the code, replace ‘joe’ with your strongspace username

rsync -azvCL —exclude=.DS_Store —progress ~/strongspace/ joe@joe.strongspace.com:/home/joe/strongspace/

rsync -azvCL —exclude=.DS_Store —progress ~/ruby/ joe@joe.strongspace.com:/home/joe/ruby/

(note the — is really two minus signs together so don’t let the css fool you into thinking its one -)

How it works is that in my user root dir I have two directories, one called strongspace containing all my documents and one called ruby containing all my ruby projects.

Be aware that any files you delete on your laptop won’t be deleted on the remote site so things might get messy on the remote after a while, but at least you won’t lose anything.

Via AppleScript

You can even automate this with AppleScript,

tell application “Terminal”

do script “…commands…”

do script “exit”

end tell

save your text file with the .script extension to tell osx to treat it as an AppleScript file.

Enjoy!

Generators

Posted by John
on Saturday, 28 July 2007

with ruby on rails, it’s so easy to build stuff fast; heck it’s the nature of being Agile.

script/generate controller [name] [model]
p. …create a controller

script/generate model [name]
p. …create data model

script/generate scaffold [model] [controller]
p. … create ruby scaffold /add/edit/view/list/ using a specific model

script/generate migration add_stuff
p. …create new migration script

rake db:migrate
p. …apply any pending migrations to your database