if(document.URL!="location.php?img_url="+img_url){
window.location.href = "location.php?img_url="+img_url;
}
이렇게하면 페이지가 다시로드됩니다. 나는 그것이 바뀌는지를 확인하기 위해 URL을 확인한다.문서를 비교할 때 예기치 않은 결과가 발생합니다.
if(document.URL!="location.php?img_url="+img_url){
window.location.href = "location.php?img_url="+img_url;
}
이렇게하면 페이지가 다시로드됩니다. 나는 그것이 바뀌는지를 확인하기 위해 URL을 확인한다.문서를 비교할 때 예기치 않은 결과가 발생합니다.
!=
은 항상 올바르게 작동합니다. 잘못한 사람이 당신입니다!
이 경우 document.URL
이 window.location.href
으로 설정되는 문자열이 될 것으로 예상됩니다. 그렇지 않을 수도 있습니다.
실제로는 "location.php?img_url="+img_url
이 아닙니다. 항상 URL이므로 http://www.example.com/location.php?img_url="+img_url
과 같은 URL이됩니다.
항상! 올바르게 작동 – raam86
보기 때문에 무엇 document.URL
입니다!
console.log(document.URL);
전체 URL을 반환합니다. http://example.com/location.php?img_url=1234
일치하는 부분이 아닌 일치하는 부분을 찾고 있습니다.
하나의 해결책은 indexOf()
if(document.URL.toLowerCase().indexOf("location.php?img_url="+img_url)===-1){
을 시도해보십시오
window.location.pathname
를 사용하는 대신
document.URL
쿼리 문자열은 포함되지 않습니다. –
사용해보십시오 사용하는 것입니다
if(window.location != "http://www.urlhere.com/location.php?img_url="+img_url){
window.location.href = "http://www.urlhere.com/location.php?img_url="+img_url;
}
방금 답변을 편집했습니다. 나는 전체 URL 경로도 포함하려고했다. 나는'window.location'이'document.URL'보다 쉽다는 것을 알고 있습니다. – copilot0910
어쩌면 도메인 이름을 잊었을 수도 있습니다. 이 같은
시도 뭔가 :
if(document.URL!="http://localhost/location.php?img_url="+img_url){
window.location.href = "http://localhost/location.php?img_url="+img_url;
}
사용 document.location.pathname + document.location.search
대신 내가 당신을 확신 document.URL
의 ='연산자가 제대로 작동 자바 스크립트'!. – naomik
우스꽝스러운 질문 제목과 관련없는 태그를 삭제했습니다. –