The Drupal way of adding HTML syntax within hook_redirect_alter I'm trying to setup client-side redirects instead of server-side redirects.  Since Varnish is being used for caching, I would like that to remain; so the request should not get to Drupal, Varnish should handle the redirect.  Ultimately, using session cookies, I'm trying to have the value for source/referrer path available at the landing page (because redirects clear the original referrer headers).

In Drupal 7, within a custom module, I'm using hook_redirect_alter() to stop the redirect process, then print out javascript to the page to do the redirect.  My question is, what is the Drupal way of adding the below print statement to the page within this hook?

/**
 * Implements hook_redirect_alter().
 */
function mymodule_redirect_alter($redirect) {
  print('
    &lt;html&gt;
    &lt;head&gt;
    &lt;script src="sites/all/modules/contrib/jquery_update/replace/jquery/1.10/jquery.min.js?v=1.10.2"&gt;&lt;/script&gt;
    &lt;script src="sites/all/modules/contrib/jquery_update/replace/ui/external/jquery.cookie.js"&gt;&lt;/script&gt;
    &lt;script&gt;
      $(function() {
        var source = $(location).attr("pathname");
        var source = source.slice(1);
        $.cookie("source", source);
        window.location.replace("' . $redirect-&gt;redirect .'");
      });
    &lt;/script&gt;
    &lt;noscript&gt;
      &lt;a href="/' . $redirect-&gt;redirect . '"&gt;Javascript is disabled. Click here to continue&lt;/a&gt;
    &lt;/noscript&gt;
    &lt;/head&gt;
    &lt;/html&gt;
  ');

  // This kills sever-side redirect
  exit();
}
Since you cannot specify a 301 redirect in javascript, here is what I did all within hook_redirect_alter() :

function mymodule_redirect_alter($redirect) {
  // If checkbox is set then use URL parameter for redirect, otherwise ignore everything
  if (variable_get('my_checkbox', '') == '1') {
    $params = drupal_get_query_parameters();
    $my_parameter = variable_get('my_parameter', '');

    // Append redirect values seperated by commas if the specified parameter exists    
    if (!empty($params[$my_parameter])) {
      $refs = $params[$my_parameter] . ',' . $redirect-&gt;source;
    }
    // Otherwise add souce as the value of the parameter
    else {
      $refs = $redirect-&gt;source;
    }

    // Create parameters to append to URL
    $params = array('query' =&gt; array($my_parameter =&gt; $refs));

    // Do the actual redirect with this new parameter
    drupal_goto($redirect-&gt;redirect, $params);

    // This kills sever-side redirect
    exit();
  }
}


So the URL mysite.com/original_page will be redirected to a page with URL mysite.com/mypage?redirect=original_page