我现在正在努力学习JavaScript。 有人写了这段代码,我试图理解它。 什么是根据用户位置重定向用户的最佳方式?
目前,我们通过在手机中使用他们的areadcode来确定他们的区域#,如您在javaScript中看到的那样。
这是表格。
<div class="row">
<!-- Homework for Jason Begin -->
<div class="col-md-6 col-md-offset-3">
<!-- Homework for Jason End -->
<form id="sfDemoForm" action="https://webto.salesforce.com/servlet/servlet.WebToLead?encoding=UTF-8" method="POST" class="sky-form">
<input name="captcha_settings" value="{"keyname":"IC_ClearDent_Main_Demo","fallback":"true","orgId":"00D1I0000002QyG","ts":""}" type="hidden">
<input name="oid" value="00D1I0000002QyG" type="hidden">
<input name="retURL" value="https://test.cleardent.com/demo-thankyou-bc.html" type="hidden">
<!-- ---------------------------------------------------------------------- -->
<!-- NOTE: These fields are optional debugging elements. Please uncomment -->
<!-- these lines if you wish to test in debug mode. -->
<!-- <input type="hidden" name="debug" value=1> -->
<!-- <input type="hidden" name="debugEmail" value="ppli@cleardent.com"> -->
<!-- ---------------------------------------------------------------------- -->
<fieldset>
<!--<label class="label" for="first_name">First Name</label>-->
<label class="input margin-bottom-15"><i class="icon-prepend fa fa-user"></i>
<input id="sffirst_name" maxlength="40" name="first_name" size="20" type="text" required placeholder="First name">
</label>
<!--<label class="label" for="last_name">Last Name</label>-->
<label class="input margin-bottom-15"><i class="icon-prepend fa fa-user"></i>
<input id="sflast_name" maxlength="80" name="last_name" size="20" type="text" required placeholder="Last name">
</label>
<!--<label class="label" for="email">Email</label>-->
<label class="input margin-bottom-15"><i class="icon-prepend fa fa-envelope"></i>
<input id="sfemail" maxlength="80" name="email" size="20" type="email" required placeholder="Email address">
</label>
<!-- <label class="label" for="phone">Phone</label>-->
<label class="input margin-bottom-25"><i class="icon-prepend fa fa-phone"></i>
<input id="sfphone" maxlength="40" name="phone" size="20" type="text" required placeholder="Phone">
</label>
<!--<label class="label" for="description">Notes</label>-->
<label class="textarea textarea-resizable margin-bottom-25">
<textarea id="sfdescription" name="description" placeholder="Is there something specific you want to see from ClearDent?"></textarea>
</label>
<input id="sfstate" name="state" type="hidden">
<input id="sflead_source" name="lead_source" type="hidden" value="Website">
<input id="sfcompany" name="company" type="hidden">
<input id="sfCampaign_ID" name="Campaign_ID" type="hidden" value="7011I000000d5auQAA">
</fieldset>
<div id="recaptcha" class="g-recaptcha" data-sitekey="6LeXmEAUAAAAAG7VJd6Z8YCVkP44AgAlqCUmpRAi" data-callback="submitDemoToLead" data-size="invisible"> </div>
<footer>
<button id="sfdemoPreSubmit" class="btn-u"><i class="fa fa-paper-plane fa-fw"></i> Get Your Free Demo</button>
<button class="btn-u btn-brd" onclick="window.history.back();"><i class="fa fa-arrow-left fa-fw"></i> Back</button>
</footer>
</form>
</div>
</div>
这是JavaScript
<script src="https://www.google.com/recaptcha/api.js"></script>
<script>
//Required Salesforce functions
function timestamp() {
var response = document.getElementById("g-recaptcha-response");
if (response == null || response.value.trim() == "") {
var elems = JSON.parse(document.getElementsByName("captcha_settings")[0].value);
elems["ts"] = JSON.stringify(new Date().getTime());
document.getElementsByName("captcha_settings")[0].value = JSON.stringify(elems);
}
}
window.setInterval(timestamp, 500);
//Masking
$("#sfphone").mask('(999) 999-9999', {placeholder:'X'});
//Form helper functions
function getProvince(pStrPhone) {
var areacode = pStrPhone.substring(0, 3);
switch (areacode) {
case "403":
case "780":
case "587":
case "825":
return "AB";
case "604":
case "778":
case "250":
case "236":
return "BC";
case "204":
case "431":
return "MB";
case "506":
return "NB";
case "709":
return "NL";
case "867":
return "YT";
case "902":
case "782":
return "NS";
case "416":
case "647":
case "437":
case "519":
case "226":
case "548":
case "613":
case "343":
case "705":
case "249":
case "807":
case "905":
case "289":
case "365":
return "ON";
case "418":
case "581":
case "450":
case "579":
case "514":
case "438":
case "819":
case "873":
return "QC";
default:
return "";
}
}
function cleanPhNum(pStrPhone) {
return pStrPhone.replace("-", "").replace("(", "").replace(")","").replace(" ", "");
}
//Form validation and reCAPTCHA
//Jason Home Work - jQuery Validate and figure out how it validates
$("#sfdemoPreSubmit").click(function(e) {
e.preventDefault();
$("#sfcompany").val($("#sflast_name").val() + ", " + $("#sffirst_name").val());
$("#sfphone").val(cleanPhNum($("#sfphone").val()));
$("#sfstate").val(getProvince($("#sfphone").val()));
$("#sfDemoForm").validate();
if ($("#sfDemoForm").valid()) {
grecaptcha.execute();
}
});
function submitDemoToLead(token) {
$("#sfDemoForm").submit();
}
这是我的不良尝试。正好在上面的JavaScript代码之下。
function redirect() {
var province = document.getElementById('#sfstate').value;
if(province == 'BC')
{
location.href = "www.xxx.com/bc";
}
if(province == 'AB')
{
location.href = "www.xxx.com/ab";
}
if(province == 'ON')
{
location.href = "www.xxx.com/on";
}
else
{
alert("Invalid Input");
}
}
我实际上是在脱掉头发。
请帮帮我!!
谢谢!
答案 0 :(得分:0)
问题在于重定向功能。你编码了
var province = document.getElementById('#sfstate').value;
如果您希望将其保留为纯javascript,则应删除主题标签。
var province = document.getElementById('sfstate').value;
或者以jQuery方式执行
var province = $('#sfstate').val();