Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loop over "history" in javascript?

Is there a way to loop over the "history" object in javascript to find a specific page in the history?

like image 553
Brian David Berman Avatar asked Dec 22 '09 15:12

Brian David Berman


People also ask

What is difference between Forin and for OF in JavaScript?

Difference between for...of and for...inThe for...in statement iterates over the enumerable string properties of an object, while the for...of statement iterates over values that the iterable object defines to be iterated over.

How do you iterate over an object in JavaScript?

There are two methods to iterate over an object which are discussed below: Method 1: Using for…in loop: The properties of the object can be iterated over using a for..in loop. This loop is used to iterate over all non-Symbol iterable properties of an object.

Can you iterate over a string in JavaScript?

[@@iterator]() returns iterator object which iterate over all code point of String. String[@@iterator] is Built – in Property of String. We can use this method by making a string iterator. We can make an iterator by calling the @@iterator property of String.

How for in loop works in JavaScript?

A for loop repeats until a specified condition evaluates to false. The JavaScript for loop is similar to the Java and C for loop. When a for loop executes, the following occurs: The initializing expression initialExpression , if any, is executed.


2 Answers

Security reasons - no, you can not dump the history of the browser (with javascript)

I mean you can not do

// This does not work
for (i=0; i<window.history.length; i++) {
      alert(window.history[i]);
}

However I don't have good explanation why history.go(n); is ok

like image 78
Svetlozar Angelov Avatar answered Nov 15 '22 19:11

Svetlozar Angelov


No, because it would you the ability to basically spy on the web history of anyone visiting your site (you could send this information to your server using Javascript).

You can redirect the client to particular pages in their history using the Javascript history object's back(), forward(), and go() methods. You just can't know where exactly you're redirecting them (except in the case of using go() to redirect to a URL instead of a history number).

like image 39
Kaleb Brasee Avatar answered Nov 15 '22 17:11

Kaleb Brasee