Variable from function into window.location.href 'not defined'

user3249657

Hello again people of Stack

I have a small problem with my code. I am trying to send my variable with the window.location.href, but I keep getting the error not defined.

This is the function wich chooses my image (src) and the id (chosenid) The chosenid does work as I've tested that.

function HeadShow() {
  var HeadItems = document.getElementById('HeadItems');
  HeadItems.style.display = 'block';
}

function headHide(src, chosenid) {

  var head = document.getElementById("head");
  var HeadItems = document.getElementById('HeadItems');
  HeadItems.style.display = 'none';
  head.innerHTML = "<img src='" + src + "' width=80; height=80;>";
  var chosenhead = chosenid;

}

Then, I also have the function which should send the variable to the next page:

function send() {
    window.location.href = "test3.php?head=" + chosenhead;
}

All I get is the error: chosenhead is not defined

Hope you guys can help me out with this.

Yami

You defined your chosenhead variable inside a function.. so it's only accessible in the scope of this function. Move it outside the function:

var chosenhead = '';

        function HeadShow() {
                    var HeadItems = document.getElementById('HeadItems');
                    HeadItems.style.display = 'block';
    }

    function headHide(src, chosenid) {

                    var head = document.getElementById("head");
                    var HeadItems = document.getElementById('HeadItems');
                    HeadItems.style.display = 'none';
                    head.innerHTML = "<img src='" +src+ "' width=80; height=80;>";
                    chosenhead = chosenid;

    }

    function send()
    {
        window.location.href = "test3.php?head=" + chosenhead;
    }

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

String is not a function on window location href

From Dev

Using a variable for if window.location.href.indexOf()

From Dev

preventDefault stops window.location.href from working

From Dev

Any other ways apart from Window.location.href?

From Dev

window.location.href not working

From Dev

window.location.href and location.reload() does not load updated page from Server

From Dev

Accessing a variable from a defined function in a while loop

From Dev

Using a defined variable from outside the current function

From Dev

Accessing a variable from a defined function in a while loop

From Dev

Why is my saved variable from window.location changing?

From Dev

Make window.location.href load page from Server instead from browser cache

From Dev

Make window.location.href load page from Server instead from browser cache

From Dev

When I use window.location.href ,then my another function not calling .Following is my javascript code

From Dev

callback function for location.href

From Dev

Enable and disable window.location.href

From Dev

onclick window.location.href with input value

From Dev

Javascript window.location.href onClick not working

From Dev

Passing parameter through "window.location.href"

From Dev

onchange event on window.location.href

From Dev

Window.location.href infinite loop

From Dev

Stubbing window.location.href with Sinon

From Dev

window.location.href not working in IE 11

From Dev

$window.location.href NOT WORKING in AngularJS

From Dev

window.location.href appending instead of replacing

From Dev

JavaScript window.location.href not working in localhost

From Dev

window.location.href is not redirecting html page

From Dev

window.location.href not working on IE

From Dev

How to Pass QueryString in window.location.href?

From Dev

onclick window.location.href with input value

Related Related

  1. 1

    String is not a function on window location href

  2. 2

    Using a variable for if window.location.href.indexOf()

  3. 3

    preventDefault stops window.location.href from working

  4. 4

    Any other ways apart from Window.location.href?

  5. 5

    window.location.href not working

  6. 6

    window.location.href and location.reload() does not load updated page from Server

  7. 7

    Accessing a variable from a defined function in a while loop

  8. 8

    Using a defined variable from outside the current function

  9. 9

    Accessing a variable from a defined function in a while loop

  10. 10

    Why is my saved variable from window.location changing?

  11. 11

    Make window.location.href load page from Server instead from browser cache

  12. 12

    Make window.location.href load page from Server instead from browser cache

  13. 13

    When I use window.location.href ,then my another function not calling .Following is my javascript code

  14. 14

    callback function for location.href

  15. 15

    Enable and disable window.location.href

  16. 16

    onclick window.location.href with input value

  17. 17

    Javascript window.location.href onClick not working

  18. 18

    Passing parameter through "window.location.href"

  19. 19

    onchange event on window.location.href

  20. 20

    Window.location.href infinite loop

  21. 21

    Stubbing window.location.href with Sinon

  22. 22

    window.location.href not working in IE 11

  23. 23

    $window.location.href NOT WORKING in AngularJS

  24. 24

    window.location.href appending instead of replacing

  25. 25

    JavaScript window.location.href not working in localhost

  26. 26

    window.location.href is not redirecting html page

  27. 27

    window.location.href not working on IE

  28. 28

    How to Pass QueryString in window.location.href?

  29. 29

    onclick window.location.href with input value

HotTag

Archive