Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Now that you have disclosed your entire code, we can see that you are running <code>document.getElementById("url")</code> too soon before the DOM is loaded. Move your script to the end of the body tag (see fixed code at the end of this answer).</p> <p>Scripts that reference DOM elements cannot be run until those DOM elements are successfully loaded. There are three ways to ensure that. </p> <ol> <li><p>The simplest is to just place the script right before the <code>&lt;/body&gt;</code> tag. Since things in the <code>&lt;body&gt;</code> tag are loaded sequentially, this ensures that all of your page has been loaded before the script runs and thus the script can reference it.</p></li> <li><p>You can code your script to wait for everything in the page to finish loading (including images) by running your code only when the <code>window.onload</code> event fires. This waits longer than necessary because it also waits for all images to finish loading, but it is safe and easy.</p></li> <li><p>You can code your script to wait until just the DOM is loaded. This is a little tricky to do cross browser since older browsers require different mechanisms. In newer browsers, this time is signified with a <code>DOMContentLoaded</code> event on the document.</p></li> </ol> <p><code>window.location.href</code> contains the current page URL. This always works and is the correct way to obtain the URL of the current page.</p> <p>Working demo: <a href="http://jsfiddle.net/jfriend00/yGrxU/" rel="nofollow">http://jsfiddle.net/jfriend00/yGrxU/</a></p> <p>If this doesn't work in your page, then you have something else wrong in your code. It could be because you are running your code too early before the DOM is loaded and thus <code>document.getElementById()</code> isn't working. You can fix that by moving your javascript code to the end of the body tag.</p> <p>Another possibility is that you have a script error in your page that is stopping execution of your javscript. You should check the browser error console or the debug console to see if there are any script errors being reported.</p> <hr> <p>Now that you've posted your entire code, we can see that you need to move the script to right before the <code>&lt;/body&gt;</code> tag like this:</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;/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;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;/body&gt; &lt;/html&gt; </code></pre>
 

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