2013-06-26 3 views
0
if(document.URL!="location.php?img_url="+img_url){ 
     window.location.href = "location.php?img_url="+img_url; 
    } 

이렇게하면 페이지가 다시로드됩니다. 나는 그것이 바뀌는지를 확인하기 위해 URL을 확인한다.문서를 비교할 때 예기치 않은 결과가 발생합니다.

+2

의 ='연산자가 제대로 작동 자바 스크립트'!. – naomik

+2

우스꽝스러운 질문 제목과 관련없는 태그를 삭제했습니다. –

답변

3

!=은 항상 올바르게 작동합니다. 잘못한 사람이 당신입니다!

이 경우 document.URLwindow.location.href으로 설정되는 문자열이 될 것으로 예상됩니다. 그렇지 않을 수도 있습니다.

실제로는 "location.php?img_url="+img_url이 아닙니다. 항상 URL이므로 http://www.example.com/location.php?img_url="+img_url과 같은 URL이됩니다.

+0

항상! 올바르게 작동 – raam86

7

보기 때문에 무엇 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){ 
0

을 시도해보십시오

window.location.pathname 

를 사용하는 대신

document.URL 
+1

쿼리 문자열은 포함되지 않습니다. –

2

사용해보십시오 사용하는 것입니다

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; 
} 
+0

방금 ​​답변을 편집했습니다. 나는 전체 URL 경로도 포함하려고했다. 나는'window.location'이'document.URL'보다 쉽다는 것을 알고 있습니다. – copilot0910

0

어쩌면 도메인 이름을 잊었을 수도 있습니다. 이 같은

시도 뭔가 :

if(document.URL!="http://localhost/location.php?img_url="+img_url){ 
    window.location.href = "http://localhost/location.php?img_url="+img_url; 
} 
1

사용 document.location.pathname + document.location.search 대신 내가 당신을 확신 document.URL

+0

쿼리 문자열이 포함되지 않습니다. –

+0

@CrazyTrain 고마워요 – raam86

+0

+1 그게 효과가있다. –

관련 문제