JavaScript Questions & Answers – History Management in JavaScript

This set of Javascript Multiple Choice Questions & Answers (MCQs) focuses on “History Management in JavaScript”.

1. What does a History object contain?
a) URL
b) Parameters
c) Attribute values
d) Property
View Answer

Answer: a
Explanation: The history object contains the URLs visited by the user. By using history object, you can load previous, forward or any particular page using various methods.

2. The history object is a part of which object?
a) Property
b) Window
c) Location
d) Screen
View Answer

Answer: b
Explanation: The window object represents an open window in a browser. The history object belongs to the Window object.

3. How many methods are there in the History object?
a) 3
b) 4
c) 5
d) 6
View Answer

Answer: a
Explanation: There are three methods belonging to the History object namely :

  1. back()
  2. forward()
  3. go()
advertisement
advertisement

back() loads the previous URL, forward loads the next URL and go() loads a specific URL in the history list.

4. What is the purpose of the method forward()?
a) Loads any random URL in the history list
b) Loads the previous URL in the history list
c) Loads a specific URL from the history list
d) Loads the next URL in the history list
View Answer

Answer: d
Explanation: The forward() method is found in the history object. The forward() method loads the next URL in the history list.
Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

5. How will you update the URL displayed in the location bar?
a) location
b) location.URL
c) location.hash
d) url
View Answer

Answer: c
Explanation: The hash property sets or returns the anchor part of a URL, including the hash sign (#). The property location.hash needs to be updated to display the updated URL in the location bar.

6. How do you add a particular state to the browsing history?
a) pushState()
b) replaceState()
c) state()
d) addstate()
View Answer

Answer: a
Explanation: The pushState() method adds a particular state to the browsing history. It pushes the given data onto the session history stack with the specified title and, if provided, URL.
advertisement

7. What does the pushState() method do?
a) Removes the state
b) Adds new state
c) Replaces the state
d) Change the state
View Answer

Answer: b
Explanation: When a web app enters a new state, it calls history.pushState() to add that state to the browsing history.

8. Which of the following method is used to replace the current history state instead of adding a new state to the browsing history?
a) replaceState()
b) replace(state)
c) replace()
d) change()
View Answer

Answer: a
Explanation: The replaceState() method is used to replace the current history state instead of adding a new state to the browsing history. It updates the most recent entry on the history stack to have the specified data, title, and, if provided, URL.
advertisement

9. How many parameters does the replaceState() method take?
a) 2
b) 3
c) 4
d) 5
View Answer

Answer: b
Explanation: The replaceState() updates the most recent entry on the history stack to have the specified data, title, and, if provided, URL. window.history.replaceState(stateObj, title, url) : This is just like window.history.pushState, except that the current browser state is removed from the history, so you cannot hit “back” to return to it.

10. What is the purpose of the event window.onpopstate?
a) When a state object is replaced
b) When a state object is added
c) When a state object is removed
d) When a state object is changed
View Answer

Answer: c
Explanation: The window.onpopstate event is fired whenever a state object is removed from the browser history, which occurs on browser “back” or “forward”. The object passed into a call to pushState or replaceState is provided as the state property on the event object in the “popstate” event.

Sanfoundry Global Education & Learning Series – Javascript Programming.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.