Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8516655
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T05:29:21+00:00 2026-06-11T05:29:21+00:00

I would like to get my current URL address, save it to a variable

  • 0

I would like to get my current URL address, save it to a variable and then pass it to an HTML element:

 var url = document.URL;
 document.getElementById("url").innerHTML = url;

I have tried using document.URL, window.location and window.location.href but none of them works for me. It displays nothing.

My HTML is:

<p id="url"></p>

Thanks in advance!

Here is my source code:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>

<head>

  <style type="text/css">

    input:hover
    {
        background: black;
        color: white;   
        border: 0;      
    }

  </style>

  <script>   

  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.");

  }  

  </script>

</head>

<body>

  <input type="button" onclick="first()" value="Try Me" />

  <p onmouseover="submit()">Hover over this text.</p>

  <p id="url">error</p>

</body>

</html>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T05:29:23+00:00Added an answer on June 11, 2026 at 5:29 am

    Now that you have disclosed your entire code, we can see that you are running document.getElementById("url") 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).

    Scripts that reference DOM elements cannot be run until those DOM elements are successfully loaded. There are three ways to ensure that.

    1. The simplest is to just place the script right before the </body> tag. Since things in the <body> 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.

    2. You can code your script to wait for everything in the page to finish loading (including images) by running your code only when the window.onload event fires. This waits longer than necessary because it also waits for all images to finish loading, but it is safe and easy.

    3. 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 DOMContentLoaded event on the document.

    window.location.href contains the current page URL. This always works and is the correct way to obtain the URL of the current page.

    Working demo: http://jsfiddle.net/jfriend00/yGrxU/

    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 document.getElementById() isn’t working. You can fix that by moving your javascript code to the end of the body tag.

    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.


    Now that you’ve posted your entire code, we can see that you need to move the script to right before the </body> tag like this:

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    
    <head>
    
      <style type="text/css">
    
        input:hover
        {
            background: black;
            color: white;   
            border: 0;      
        }
    
      </style>
    
    
    </head>
    
    <body>
    
      <input type="button" onclick="first()" value="Try Me" />
    
      <p onmouseover="submit()">Hover over this text.</p>
    
      <p id="url">error</p>
    
      <script>   
    
      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.");
    
      }  
    
      </script>
    </body>
    
    </html>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to get the address string of the current user location. Is
I would like to get the weighted_median of an unsorted, variable length, eigen c++
I would like to get the dimensions (coordinates) for all the HTML elements of
I would like a way to get individual anchor vars from a url and
To get the url of the current page, I usually do something like this:
I am submitting a normal <form method=get> element to the current url... It's part
I have a VB console application. I would like to get the absolute URL
is it possible to make a jquery/javascript call like <span onclick=$('#dialog-2').html($.get('http://url.com');>Info</span> direct from html
I would like to get numbers of comments for each posts using mysql. I
I would like to get to know how to redirect output of some program

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.