All About Code

4 days ago

Photo by Frida Bredesen on Unsplash

How do you redirect to another page in Javascript?

There are many ways to redirect to another page. But which is the best?

// 1. window.location
window.location.replace('http://www.example.com')
window.location.assign('http://www.example.com')
window.location.href = 'http://www.example.com'
document.location.href = '/path'
// 2. window.history
window.history.back()
window.history.go(-1)
// 3. window.navigate; ONLY for old versions of Internet Explorer
window.navigate('top.jsp')
// 4. More ways... but probably not a good…

--

--

5 days ago

Here is the JavaScript.

function playAudio(url) {
new Audio(url).play();
}

Here is the HTML.

<img src="image.png" onclick="playAudio('mysound.mp3')">

If you experience a delay, use var audio = new Audio(url); once on page load. Then trigger it by audio.currentTime = 0; audio.play();. Resetting current time is only needed if the sound is…

--

--