Hướng dẫn reload specific url javascript - tải lại javascript url cụ thể

Let's say that someone decides to press F5 or the browser's reload button. Is that possible to open a specific url in the same tab?

asked Jan 28, 2016 at 11:56

Hướng dẫn reload specific url javascript - tải lại javascript url cụ thể

8

Like posted in comment, if you display prompt, this is possible using following ugly hack:

var a,b;
window.onbeforeunload = function (e) {
    if (b) return;
    a = setTimeout(function () {
        b = true;
        window.location.href = "//test.com";
    }, 500);
    return "Now you will be redirected to new page if choosing to stay there...";
}
window.onunload = function () {
    clearTimeout(a);
}

answered Jan 28, 2016 at 12:27

A. WolffA. Wolff

73.6k9 gold badges91 silver badges152 bronze badges

3

Use This

// To check if someone pressed f5 or leaving the page

window.onbeforeunload = function () {

    //for changing the url

    var newUrl = "http://www.someurl.com";
    window.location.href = newUrl;
}; 

answered Jan 28, 2016 at 12:04

Hướng dẫn reload specific url javascript - tải lại javascript url cụ thể

7

As noted in this Stack Overflow answer, the ShortCut library is designed to handle keyboard button events, including F-keys and arrow-buttons etc.

However, I must say that the idea of redirecting people to another page or site when they think they are refreshing the page is not only bad practice, but open to abuse.

answered Jan 28, 2016 at 12:11

Hướng dẫn reload specific url javascript - tải lại javascript url cụ thể

8

I just wat to know how I can reload a specific element or div with javascript code.

Nội dung chính

  • location.reload trong JavaScript là gì
  • Tải lại trang bằng location.reload trong JavaScript phương thức location.reload với mã HTML sau đây:
  • Tổng kết

div:

Hướng dẫn reload specific url javascript - tải lại javascript url cụ thể

hope you will help . I really need it . Thank you

sam

1,55711 silver badges14 bronze badges11 silver badges14 bronze badges

asked Nov 5, 2021 at 13:15Nov 5, 2021 at 13:15

8

You can easily set the source of an image in JS:

document.getElementById("holla").src = "avatar.gif";

function refresh() {
document.getElementById("holla").src = "avatar.gif";
}
Hướng dẫn reload specific url javascript - tải lại javascript url cụ thể

answered Nov 5, 2021 at 13:20Nov 5, 2021 at 13:20

11

If you are trying to reload from server then you can do ajax call and set the response to that div. document.getElementById('holla').innerHTML = this.responseText;

answered Nov 5, 2021 at 13:24Nov 5, 2021 at 13:24

1

You must have some mechanism to load the image. Ajax call will load the image asynchronously.

// Create an XMLHttpRequest object
const xhttp = new XMLHttpRequest();
// Define a callback function
xhttp.onload = function() {
  //you get response back from server in responseText.
  document.getElementById('holla').innerHTML = this.responseText;
}
// Send a request
xhttp.open("GET", "ajax_info.txt");// path to file
xhttp.send();

answered Nov 5, 2021 at 13:44Nov 5, 2021 at 13:44

Hướng dẫn cách tải lại trang bằng JavaScript. Bạn sẽ biết cách sử dụng thuộc tính location.reload để load lại trang bằng JavaScript sau bài học này.tải lại trang bằng JavaScript. Bạn sẽ biết cách sử dụng thuộc tính location.reload để load lại trang bằng JavaScript sau bài học này.

location.reload trong JavaScript là gì

Tải lại trang bằng location.reload trong JavaScript phương thức location.reload với mã HTML sau đây: là một phương thức của window.location Object, có tác dụng tải (load) lại trang hiện tại.

Tổng kết

hope you will help . I really need it . Thank you

sam

Tải lại trang bằng location.reload trong JavaScript phương thức location.reload với mã HTML sau đây:

html>
<html lang="vi">
<head>
<meta charset="UTF-8">
<title>Sampletitle>
head>
<body>

<h2>Sampleh2>

<p id="msg">Ví dụ tải lại trangp>

<input type="button" value="reload" id="btnReload">

<script>
let d = new Date();
let element = document.getElementById('msg');
element.insertAdjacentHTML('afterend', '

' + d.toTimeString() + '

'
);


let btnReload = document.getElementById('btnReload');
btnReload.addEventListener('click', function(){
location.reload();
});
script>

body>
html>

Tổng kết

Tổng kết

hope you will help . I really need it . Thank youTải lại trang bằng JavaScript bằng location.reload rồi. Để nắm rõ nội dung bài học hơn, bạn hãy thực hành viết lại các ví dụ của ngày hôm nay nhé.

sam

1,55711 silver badges14 bronze badges