특정 페이지에 특정 메시지를 표시하고 싶습니다. URL에 자바 스크립트가 포함되어있는 경우
내가 "foo_page.html"라고 뭔가를 표시 할 페이지의 이름, 나는이 자바 스크립트를 사용을 할 수있는 방법을 가정?
특정 페이지에 특정 메시지를 표시하고 싶습니다. URL에 자바 스크립트가 포함되어있는 경우
내가 "foo_page.html"라고 뭔가를 표시 할 페이지의 이름, 나는이 자바 스크립트를 사용을 할 수있는 방법을 가정?
당신은 이런 식으로 작업을 수행 할 수 있습니다
if(document.URL.indexOf("foo_page.html") >= 0){
...show your message
}
아마도'> = 0'을 원할 것입니다. – Syntactic
좋은 장소, 고마워. 편집 됨. – GShenanigan
URL이 http://example.com/foo_page.html 같은 경우 경고 상자가 표시됩니다 다음 :
if(location.pathname=="/foo_page.html") alert('hey!');
var index = document.location.lastIndexOf("/");
var filename = document.location.substr(index);
if(filename.indexOf("foo_page.html")>-1){
alert("OK");
}
당신은 알아낼 document.location
를 사용할 수는 방문자가있는 URL
<script type="text/javascript">
var currentPage = document.location.href.substring(document.location.href.lastIndexOf("/")+1, document.location.href.length);
</script>
귀하의 "currentPage"변수는 이제중인 페이지의 이름이 포함되어야합니다. 이를 사용하여 작업을 선택할 수 있습니다.
var loc = window.location.pathname.split("/"),
size = loc.length
alert(loc[size])
마지막 부분은 "/"로 대부분의 시간을 HTML, PHP 또는 기타 파일로 제공합니다. 그러나 나는 당신 몸의 수업을 사용하여 당신이 어디에 있는지 인식합니다. 또는 무언가를하고 싶은 요소가 페이지에 있는지 확인하십시오. 이와 같은 함수를 실행하기 전에
function example(element) {
if(getElementById(element).length) {
// now you are sure that a element exists on the page
}else{
return false; //if not just do nothing
}
}
example("myId")
자세한 내용을 입력하십시오. – SLaks