2013-08-01 2 views
7

페이지를 새로 고친 후 경고 상자를 만들려고하지만 작동하지 않습니다.
내 코드를 수정하고 이유를 알려주십시오.JavaScript로 페이지를 다시로드 한 후 알림을 표시하는 방법은 무엇입니까?

$("button").click(function(){ 
    window.location.reload(); 
    alert("Hello world"); 
}); 
+4

당신이 페이지를 다시로드가되면 자동으로 이전 페이지의 상태를 기억 않을거야 것을 고려하십시오. 페이지를 새로 고침 할 때 클릭 이벤트를 바인딩하고 누군가가 버튼을 클릭 할 때까지 아무 작업도 수행하지 않을 것입니다. 당신이 다시로드하면 전체 페이지를 새로 고침하는 –

+0

, 그 콜백 –

+0

내가 허용 대답이 잘못되었습니다 더 올바른 방법 – suhailvs

답변

9

당신은 sessionStorage 사용할 수 있습니다

$("button").click(function() { 
    sessionStorage.reloadAfterPageLoad = true; 
    window.location.reload(); 
}); 
$(function() { 
    if (sessionStorage.reloadAfterPageLoad) { 
     alert("Hello world"); 
     sessionStorage.reloadAfterPageLoad = false; 
    } 
}) 
+1

안녕하세요, 몇 가지 서식을 수정했습니다. 미래에 'sms speak'에 대한 완전한 단어를 선호 할 수 있다면 좋을 것입니다. 예를 들어 "you"over "u". 독자 여러분 께 감사드립니다. 감사합니다. –

+0

@BenjaminGruenbaum. 감사. 이를 명심하십시오. :) –

+2

이 코드가 작동하지 않습니다. 'if (sessionStorage.reloadAfterPageLoad)'가 항상 실행됩니다. 자세한 내용은 http://stackoverflow.com/questions/39127730/jquery-if-condition-is-false-but-still-executed/39127785를 참조하십시오. – Adam

1

이것이 onload라고합니다. DOM 준비가 끝나기 전에 waaaaay가 왔고 DOM 준비는 실제로 onload가 이미지를 기다리는 정확한 이유 때문에 만들어졌습니다.

window.onload = function() { 
    alert("It's loaded!"); 
    //dom not only ready, but everything is loaded 
} 

jQuery Javascript 솔루션은 document.ready()에서 window.onload 이벤트를 바인딩하는 것입니다.

$(window).bind("load", function() { 
    // code here 
}); 
+0

페이지 후 그 일이 새로 고쳐집니다 : == 는 항목을 참조하십시오? – Kobi

+0

나는 한혜진 예 –

+0

@MichaelUnterthurner는 초기 이동 작동하지 않습니다 생각? 사용자가 처음 페이지를 입력 할 때 _ 메시지가 표시되지 않습니까? –

0

<a class="button" href="?reload/">reload and alert</a> 
<script type="text/javascript">   
    args = location.search.substr(1); 
    if (args=='reload/')alert('page reloaded'); 
</script> 
+1

문서가로드 된 것이 아니라 문서가 다시로드되었는지 확인하고 있습니까? –

+0

@BenjaminGruenbaum 이제 작동합니다. – suhailvs

0

1 단계에서 PHP request.GET 같은 링크 HREF에서 ?reload 문자열로 그 일을 어떤 더러운 방법에 대한 자신의 함수를 작성 확인 버튼이있는 경고 팝업 (메시지, 경고 유형, 메소드 이름을 허용하는 매개 변수화 된 함수를 만들었습니다.)

기능 AlertMessageOk (캐릭터,을 alertType, 방법)
{

 $('#AlertMessage .divDialogElements').empty(); 

    $('#AlertMessage .divDialogElements').append(msg); 

    if (alertType == "success") { 
     $('#AlertMessage #modalAlertHeaderTitle').html("Success"); 
     $('#AlertMessage #modalAlertHeaderTypeClass').attr("class", "modal-header alert-success"); 
    } 
    else if (alertType == "error") { 
     $('#AlertMessage #modalAlertHeaderTitle').html("Error"); 
     $('#AlertMessage #modalAlertHeaderTypeClass').attr("class", "modal-header alert-danger"); 
    } 
    else if (alertType == "info") { 
     $('#AlertMessage #modalAlertHeaderTitle').html("Status"); 
     $('#AlertMessage #modalAlertHeaderTypeClass').attr("class", "modal-header alert-info"); 
    } 
    else if (alertType == "warning") { 
     $('#AlertMessage #modalAlertHeaderTitle').html("Warning"); 
     $('#AlertMessage #modalAlertHeaderTypeClass').attr("class", "modal-header alert-warning"); 
    } 

    $('#AlertMessage #btnAlertOk').attr("onclick", method); 

    $('#AlertMessage').modal('show'); 
} 

2 단계 : 아약스 response.result에 == AlertMessageOk 기능에 충실 호출. 메소드 이름을 전달하여 페이지를 다시로드합니다.

기능 buttonActivate_onClick (때 storeID) {

 $.ajax({ 
     type: "POST", 
     url: "/configuration/activateStore", 
     timeout: 180000, 
     data: { StoreID: storeID }, 
     success: function (response) { 
      if (response.result == true) { 
       AlertMessageOk("Store configuration for Store ID " + storeID + " is successfully activated.", "success", "reloadPage();"); 
      } 
     }, 
     error: function (xhr, textstatus) { 
      AlertMessage("Error: " + xhr.statusText + " [" + xhr.status + "]", "error"); 

     } 
    }); 
    $('#wait_load').css("display", "none"); 
} 


function reloadPage() { 
    location.reload();   
} 
관련 문제