【发布时间】:2017-12-27 05:59:39
【问题描述】:
我正在尝试显示一条问候消息,后跟从 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>
【问题讨论】:
-
您能否提供您从中获取值的网址
标签: javascript jquery dom domparser