1

I'm trying this rough script idea. But it is not working.

<script>
    function storeurl() {
        var varurl = document.URL;
    }

    document.onclick = storeurl;
    document.write(varurl);
</script>

varurl is set as the actual URL using document.URL function.

with <a href="#2">broogle</a> then on click i would like varurl to be set to #2 and then echo.

In a perfect world this script would echo

http://url/#2

when clicking on the link

Any help? Thx

4
  • 2
    Well, what does it do? Does it do nothing? Make an error? Crash your computer? Make flying monkeys fall from the sky? Give you free waffles? Please clarify. Commented Jun 17, 2013 at 13:33
  • 1
    varurl is private, you can't access it from outside its local scope which is inside the storeurl function. Commented Jun 17, 2013 at 13:34
  • Isn't also document.onclick = storeurl; an issue since it should be: document.onclick = storeurl(); ? Commented Jun 17, 2013 at 13:59
  • 1
    doorknob why being mean? Commented Jun 17, 2013 at 14:07

6 Answers 6

3

Your varurl variable is scoped at method (function) level. This means it is not visible to code which runs outside of the function.

Also, the document.write code will execute when the script first runs i.e. before the click (should the click ever happen).

If you don't need to use varurl other than to write it to the document you can move the document.write code into the function and retain the narrow scope of varurl:

<script>
function storeurl() {
var varurl = document.URL;
document.write(varurl);
}

document.onclick = storeurl;

</script>

Otherwise move the variable definition out of the function so that it (the variable) becomes a global:

<script>
var varurl;

function storeurl() {
varurl = document.URL;
document.write(varurl);
}

document.onclick = storeurl;

</script>
Sign up to request clarification or add additional context in comments.

Comments

1

The var makes it a local variable to the function's scope. Plus you are trying to read it before it is even set.

1 Comment

true i took out the var before declaring
1

For simply storing a URL in a variable, be it an external URL or the URL of the current page, and then either display it or do something else with it, you can follow what is shown in the code below:

<html>
    <body>
        <button onclick="go()">GO TO GOOGLE</button><br/>
        <button onclick="show()">CLICK TO SHOW CURRENT URL</button><br/>
        <p id="showhere"></p>

        <script>
            function go(){
                var u = "http://www.google.com" ;
                window.location.href = u; //takes you to google.com
            }

            function show(){
                var x = window.location.href;
                document.getElementById("showhere").innerHTML = x;
                  //shows URL of current page below the buttons
            }
        </script>
    </body>
</html>

Comments

0

You have made varurl location to the function it is declared in with var so it isn't visible from outside that function.

var varurl;
function storeurl() {
    varurl = document.URL;
}

You are also writing its value immediately instead of doing so in the click event, so it won't have been set by the time you write().

function storeurl() {
    var varurl = document.URL;
    document.write(varurl);
}

document.onclick = storeurl;

Comments

0

It should work,

<script>
function storeurl() {
varurl = document.URL; // it should be Global variable, so remove var
document.write(varurl);//when you're declaring this outside of the function
}

document.onclick = storeurl;


</script>

2 Comments

yes this is good i added document.write(varurl); it sends back url/thenameofthescript.php but not what is containing the href, in this case #2.
document.URL will bring you the url of the current doc. do you want to redirect to #2 on clicking something? please be detailed.
0

change your code to

var varurl;

function storeurl() {
    varurl = window.location.href;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.