Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing functions in the window object

Tags:

javascript

If I type: window['alert'] in the console it will find the alert() function.

However if I type: window['location.replace']

It will be undefined. Why can't I access the location.replace function?

like image 927
Cameron Avatar asked Dec 10 '22 21:12

Cameron


2 Answers

replace() is a function found in the object window.location, or window['location'], so you will have to write:

window.location.replace

or

window['location']['replace']
like image 161
Johan Karlsson Avatar answered Dec 28 '22 18:12

Johan Karlsson


What you want is window['location']['replace'] (or window['location'].replace).

"location.replace" is not inside of "window". Rather, "replace" is inside of "location", which is inside of "window". Thus, it must be accessed in that order.

like image 28
Hayden Schiff Avatar answered Dec 28 '22 20:12

Hayden Schiff