2017-12-27 9 views
0

인사말 메시지와 URL에서 가져온 이름 값을 표시하려고합니다. 그러나 변수 이름이 없으면 인사말 메시지도 표시되지 않습니다. 이 문제의 원인은 무엇입니까? 어떤 변수 이름이없는 경우 작동URL 변수에서 값 가져 오기 및 표시

<h1 class="welcomeGreeting" style="color: white !important; text-align: center; padding-bottom:1px !important; margin-bottom:1px !important;"> 
</h1><script> 
    var url = window.location.href; 
    var captured = /name=([^&]+)/.exec(url)[1]; // Value is in [1] ('384' in our case) 
    var result = captured ? captured : 'myDefaultValue'; 
//var result = window.location.href.split('&name=')[1]; 


    window.alert(result); 
    var thehours = new Date().getHours(); 
var themessage; 
var morning = ('Good morning'); 
var afternoon = ('Good afternoon'); 
var evening = ('Good evening'); 

if (thehours >= 0 && thehours < 12) { 
     themessage = afternoon + ' ' + result; 

} else if (thehours >= 12 && thehours < 17) { 
     themessage = afternoon + ' ' + result; 

} else if (thehours >= 17 && thehours < 24) { 
     themessage = afternoon + ' ' + result; 
} 

$('.welcomeGreeting').append(themessage); 

</script> 
+0

제공 할 수

에서 다음 값을 추출 할 필요가

Jay

답변

1

그러나, 아무것도, 심지어 인사 메시지가 표시되지 않습니다. 이 문제의 원인은 무엇입니까?

exec 일치하는 항목이없는 경우 null을 반환합니다. 변수가 존재하지 않으면 null[1]url에 넣으려고합니다.

먼저 변수의 값을 가지고 당신이 당신이 값을 얻을 수있는 URL을
var captured = /name=([^&]+)/.exec(url); 
var result = captured ? captured[1] : 'myDefaultValue';