Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Your post title shows that you want to change the current page programmatically using JavaScript but still having the HTTP referrer provided (from what I understood, using a <code>&lt;a&gt;</code> tag is just for a test case).</p> <p>You need to be aware of cross-browser issues:</p> <ul> <li>The HTTP referrer header (HTTP-Referer) is set when changing <code>window.location.href</code> under the following browsers: <ul> <li><strong>MSIE 9 (but probably any version above 9)</li> <li>Firefox (at least 3.0, 3.5, 4.0, 5.0, but most probably all versions)</li> <li>Chrome (at least 9, but most probably all versions)</li> <li>Safari (at least 5, but most probably all versions)</li> <li>Opera (at least 11, but most probably all versions)</strong></li> </ul></li> <li><strong>MSIE (at least 6, 7, 8)</strong>: the referrer is <strong>not</strong> set when changing <code>window.location.href</code> (this is why some pseudo-solutions are based on <code>myLink.click()</code>)</li> <li><strong>Firefox (at least 3.0, 3.5, 4.0)</strong>: the <code>click</code> function does not exist (this is why pseudo-solutions based on <code>myLink.click()</code> do not work)</li> <li><strong>Firefox 5</strong> : the <code>click</code> function exists under Firefox 5 but does not change the window location, so all the methods relying on the existence of the <code>myLink.click()</code> method will not work. Calling <code>myLink.onclick()</code> or <code>myLink.onClick()</code> raise an error ("onclick is not a function"), so <a href="https://stackoverflow.com/questions/143747/is-it-possible-to-trigger-a-links-or-any-elements-click-event-through-javasc">solutions based on these calls</a> will not work.</li> </ul> <p>In order to manage these cross-browser issues, I'm using the following method:</p> <pre><code>function navigateToUrl(url) { var f = document.createElement("FORM"); f.action = url; var indexQM = url.indexOf("?"); if (indexQM&gt;=0) { // the URL has parameters =&gt; convert them to hidden form inputs var params = url.substring(indexQM+1).split("&amp;"); for (var i=0; i&lt;params.length; i++) { var keyValuePair = params[i].split("="); var input = document.createElement("INPUT"); input.type="hidden"; input.name = keyValuePair[0]; input.value = keyValuePair[1]; f.appendChild(input); } } document.body.appendChild(f); f.submit(); } navigateToUrl("http://foo.com/bar"); </code></pre> <p>This solution works on all the browser flavors and version listed above. It has the advantage to be simple, multi-browser and easy to understand. Note that this has not been tested under HTTP<strong>S</strong>.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload