Why is my saved variable from window.location changing?

Marwelln

I save window.location to a variable but when window.location is changed (History.js) my variable is also changed, why? I am not setting my variable anywhere except on first page request.

(function(){
    var myvar = window.location;

    History.Adapter.bind(window, 'statechange', function(){
        console.log(window.location.pathname);
        console.log(myvar.pathname); // same as window.location.pathname but should be saved data from line 2

        var state = History.getState(),
            options = {
                url: state.url,
                type: 'get',
                success: function(resp, status, xhr) { 
                    $('#wrapper').html(resp); 
                }
            };

        $.extend(options, callbackOptions);
        $.ajax(options);
    });

    $(document).on('click', 'a[data-pjax]', function(e){
        e.preventDefault();

        var self = $(this),
            callback = self.attr('data-pjax');

        History.pushState({ "callback" : callback }, 'Loading page...', self.attr('href'));
    });
})();

I load the page and my current location.pathname is /foo, I click on a pjax link and my new url is /bar. Why is myvar changed when I do it like this? I have no code changing the variable.

Quentin
typeof window.location
"object"

location is a reference to an object. When you assign its value to something, you are assigning the reference. Changes to the object will be visible from all references to it.

If you want to preserve it's previous state, then you need to copy immutable values. You appear to care only about pathname, which is a string…

typeof window.location.pathname
"string"

… so store that instead.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Why is cin changing the address of my reference variable?

From Dev

Why is cin changing the address of my reference variable?

From Dev

Why isn't my variable changing?

From Dev

Why does changing a variable from a lambda not work?

From Dev

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

From Dev

Why doesn't changing the value of my global variable take effect?

From Dev

Why isn't my for loop in Javascript changing the variable amount?

From Dev

Why is my button not changing the location of my browser when it's inside a form?

From Dev

Why are my settings saved in this situation?

From Dev

WebSQL:table not creating on changing window.location

From Dev

Why is my window not being updated from my thread?

From Dev

Why my View is not changing?

From Dev

Why my View is not changing?

From Dev

Why Location client gives very far location from my current location.?

From Dev

mysql Why variable is not changing in if

From Dev

I'm changing the value of a const variable by accessing the memory location. Why doesn't it work?

From Dev

Xcode changing the Location Services of my application

From Dev

Prevent this variable from changing

From Dev

Prevent this variable from changing

From Dev

AngularJs - why is $emit not working from my popup window

From Dev

AngularJs - why is $emit not working from my popup window

From Dev

Finding saved locations that are within 100 meters of my current location

From Dev

Why is position of my object not changing?

From Dev

Why is my brushstroke thickness changing?

From Dev

Why are my objects changing unexpectedly?

From Dev

Why is Javascript Changing My Date?

From Dev

Subjects in my Gmail are Changing - Why?

From Dev

Why is my string changing in this pipeline?

From Dev

Why are my strings changing unintentionally?

Related Related

  1. 1

    Why is cin changing the address of my reference variable?

  2. 2

    Why is cin changing the address of my reference variable?

  3. 3

    Why isn't my variable changing?

  4. 4

    Why does changing a variable from a lambda not work?

  5. 5

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

  6. 6

    Why doesn't changing the value of my global variable take effect?

  7. 7

    Why isn't my for loop in Javascript changing the variable amount?

  8. 8

    Why is my button not changing the location of my browser when it's inside a form?

  9. 9

    Why are my settings saved in this situation?

  10. 10

    WebSQL:table not creating on changing window.location

  11. 11

    Why is my window not being updated from my thread?

  12. 12

    Why my View is not changing?

  13. 13

    Why my View is not changing?

  14. 14

    Why Location client gives very far location from my current location.?

  15. 15

    mysql Why variable is not changing in if

  16. 16

    I'm changing the value of a const variable by accessing the memory location. Why doesn't it work?

  17. 17

    Xcode changing the Location Services of my application

  18. 18

    Prevent this variable from changing

  19. 19

    Prevent this variable from changing

  20. 20

    AngularJs - why is $emit not working from my popup window

  21. 21

    AngularJs - why is $emit not working from my popup window

  22. 22

    Finding saved locations that are within 100 meters of my current location

  23. 23

    Why is position of my object not changing?

  24. 24

    Why is my brushstroke thickness changing?

  25. 25

    Why are my objects changing unexpectedly?

  26. 26

    Why is Javascript Changing My Date?

  27. 27

    Subjects in my Gmail are Changing - Why?

  28. 28

    Why is my string changing in this pipeline?

  29. 29

    Why are my strings changing unintentionally?

HotTag

Archive