Browser
Browser
HTML anchor tag is used to open URLs in a new tab using the target=”_blank”
attribute. In JavaScript, we have an inbuilt method window.open() which is used
to open a new browser window or new tab depending on the browser settings.
<html>
<head>
<title>Open URL in New Tab </title>
</head>
<body>
<p> Click the button to open
<b> geeksforgeeks.org </b>
in new tab
</p>
<button onclick="NewTab()">
Open Geeksforgeeks
</button>
<script>
function NewTab() {
window.open(
"https://www.geeksforgeeks.org", "_blank");
}
</script>
</body>
</html>
Timers:
setTimeout(function, milliseconds)
Executes a function, after waiting a specified number of milliseconds.
setInterval(function, milliseconds)
Same as setTimeout(), but repeats the execution of the function continuously.
The setTimeout() and setInterval() are both methods of the HTML DOM Window object.
1.
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Timing</h2>
<p>Click "Try it". Wait 3 seconds, and the page will alert "Hello".</p>
<button onclick="setTimeout(myFunction, 3000);">Try it</button>
<script>
function myFunction() {
alert('Hello');
</script>
</body>
</html>
2.
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Timing</h2>
<p>Click "Try it". Wait 3 seconds. The page will alert "Hello".</p>
<script>
function myFunction() {
alert("Hello");
</script>
</body>
</html>
The setInterval() Method
The setInterval() method repeats a given function at every given time-interval.
window.setInterval(function, milliseconds);
The second parameter indicates the length of the time-interval between each execution.
This example executes a function called "myTimer" once every second (like a digital watch)
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Timing</h2>
<p id="demo"></p>
<script>
setInterval(myTimer, 1000);
function myTimer() {
document.getElementById("demo").innerHTML = d.toLocaleTimeString();
</script>
</body>
</html>
browser location and history in javascript
Window Location
The window.location object can be written without the window prefix.
Some examples:
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript</h2>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML =
</script>
</body> </html>
Window Location Hostname
The window.location.hostname property returns the name of the internet host
(of the current page).
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript</h2>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML =
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript</h2>
<h3>The window.location object</h3>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML =
</script>
</body>
</html>
Window History
The window.history object can be written without the window prefix.
Some methods:
History Back
The history.back() method loads the previous URL in the history list.
<html>
<head>
<script>
function goBack() {
window.history.back()
}
</script>
</head>
<body>
History Forward
The history.forward() method loads the next URL in the history list.
<html>
<head>
<script>
function goForward() {
window.history.forward()
}
</script>
</head>
<body>
</body>
</html>