title: Prevent redirect-loop in Javascript-function
body: <p>I got one ressource which is available through two urls, for example: mygoodlookingurl.de/nice.jsf and mybadlookingurl.de/bad123454423523413413.jsf </p>
  
  <p>I have to call the bad-looking url for one time and afterwards I want to redirect. Currently I took JQuery's document-ready()-function and call cleanUrl() which redirects via top.location.href = "mygoodlookingurl.de/nice.jsf"; </p>
  
  <p>Unfortunately an infinite loop occurs, because after loading the url on onload the function get called again and again. What to do? </p>
  
  <p>Here is the JS-Code (I am using a trick to read the URL with a created anchor-Tag):</p>
  
  <pre><code>function cleanUrl(){
      var url = document.url;
      var a = "";
      a = document.createElement( 'a' );
      a.href = url;
      if ( a.pathname == "/xxx/undefined"){
          var cleanedUrl = "https://xxx.xxx.xx/xxx.jsf";
          top.location.href = cleanedUrl;
      }
  }
  </code></pre>
  
  <p>Thanks in advance.</p>
  
  <p>UPDATE: currentUrl is not updated after the first redirect and so the infinite loop still occurs.</p>
  
  <pre><code>function cleanUrl(){
      var cleanedUrl = "https://xxx.xxx.xx:xxx.jsf";
      var currentUrl = window.location.href;
      if ( currentUrl != cleanedUrl){
          top.location.href = cleanedUrl;
      } 
  }
  </code></pre>