This can happen for instance when JQuery has not been fully loaded before trying to execute JQuery code.
Using $(document).ready(function($) is one way to solve this, as this should wait until JQuery is fully loaded.
//some awsome jquery code
alert( this.value.substring(this.value.lastIndexOf("\\")+1) );
To redirect before headers are started to be sent (for instance from inside a controller) the following works:
This will redirect to the page called portal.
However if you try to do this from a view then you will get an error message about headers already having been sent.
echo 'Due to SOMEREASON you are being redirected <a herf="' . Mage::getUrl('portal') . '">Please click here if nothing happens</a>';
window.location = "' . Mage::getUrl('portal') . '"
However a tip is to reconsider if you really need to do the redirect from the view, doing it from the controller is a lot easier and quicker.