title: How to get the current URL with JavaScript?
body: <p>I would like to get my current URL address, save it to a variable and then pass it to an HTML element:</p>
  
  <pre><code> var url = document.URL;
   document.getElementById("url").innerHTML = url;
  </code></pre>
  
  <p>I have tried using <code>document.URL</code>, <code>window.location</code> and <code>window.location.href</code> but none of them works for me. It displays nothing.</p>
  
  <p>My HTML is:</p>
  
  <pre><code>&lt;p id="url"&gt;&lt;/p&gt;
  </code></pre>
  
  <p>Thanks in advance!</p>
  
  <p>Here is my source code:</p>
  
  <pre><code>&lt;!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"&gt;
  &lt;html&gt;
  
  &lt;head&gt;
  
    &lt;style type="text/css"&gt;
  
      input:hover
      {
          background: black;
          color: white;   
          border: 0;      
      }
  
    &lt;/style&gt;
  
    &lt;script&gt;   
  
    var url = location.href;
    document.getElementById("url").innerHTML = url;
  
    function first()
    {
      var string = "It works!";
      write(string);
    }
  
    function write(szoveg)
    {
        alert(szoveg);
    }
  
    function submit()
    {
        var result;
        result = confirm("Would you like to confirm your change?");
        if(result==true) 
            window.location="okay.html";
        else if(result==false)
            alert("You have clicked the \"cancel\" button.");
  
    }  
  
    &lt;/script&gt;
  
  &lt;/head&gt;
  
  &lt;body&gt;
  
    &lt;input type="button" onclick="first()" value="Try Me" /&gt;
  
    &lt;p onmouseover="submit()"&gt;Hover over this text.&lt;/p&gt;
  
    &lt;p id="url"&gt;error&lt;/p&gt;
  
  &lt;/body&gt;
  
  &lt;/html&gt;
  </code></pre>