JS 函数 URL 参数 - 空值
JS Function URL Parameters - Null Value
我正在尝试提取 URL 参数以传递给预建的 HTML 表单。
例子URL是:
?state=CA&**period**=PERIOD_FIXED_30YEARS&**loan**=200000<v=80&**transaction**=54&property_type=34&fico=740&occupancy=49&cashout=0&rate=4.125&fees=510&points=0.204&trackingID=1445830871741638005
粗体文字是我需要提取的内容。
我试图通过以下方式做到这一点:
function getParameterByName(name) {
name = name.replace(/[\[]/, "\[").replace(/[\]]/, "\]");
var regex = new RegExp("[\?&]" + name + "=([^&#]*)"),
results = regex.exec(location.search);
return results === null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}
//Gathering the URL Parameters
var period = getParameterByName('period');
var loan = getParameterByName('loan');
var ltv = getParameterByName('ltv');
var transaction = getParameterByName('transaction');
var property_type = getParameterByName('property_type');
var fico = getParameterByName('fico');
var occupancy = getParameterByName('occupancy');
var cashout = getParameterByName('cashout');
var rate = getParameterByName('rate');
var fees = getParameterByName('fees');
var points = getParameterByName('points');
var trackingID = getParameterByName('trackingID');
然后使用
分配给表格
document.getElementById("tfa_49").value = period;
document.getElementById("tfa_50").value = loan;
我一直收到空错误,我认为这是正则表达式。
如有任何帮助,我们将不胜感激。我在网站上搜索了类似的问题,none 的答案适合我的情况。
更新工作代码:
<script type="text/javascript">
//Used to pull URL
const link = window.location.href;
const url = new URL(link);
//Function to get the URL parameters
function getParameterByName(name) {
return url.searchParams.get(name);
}
//Gathering the URL Parameters and writing to console to check if parse is accurate -- Note the console.log can be removed if needed
var period = getParameterByName('period');
console.log(period);
var loan = getParameterByName('loan');
console.log(loan);
var ltv = getParameterByName('ltv');
console.log(ltv);
var transaction = getParameterByName('transaction');
console.log(transaction);
var property_type = getParameterByName('property_type');
console.log(property_type);
var fico = getParameterByName('fico');
console.log(fico);
var occupancy = getParameterByName('occupancy');
console.log(occupancy);
var cashout = getParameterByName('cashout');
console.log(cashout);
var rate = getParameterByName('rate');
console.log(rate);
var fees = getParameterByName('fees');
console.log(fees);
var points = getParameterByName('points');
console.log(points);
var trackingID = getParameterByName('trackingID');
console.log(trackingID);
//On load function to prefill the forms hidden fields
$( document ).ready(function() {
document.getElementById("tfa_49").value = period;
document.getElementById("tfa_50").value = loan;
document.getElementById("tfa_51").value = ltv;
document.getElementById("tfa_53").value = transaction;
document.getElementById("tfa_54").value = property_type;
document.getElementById("tfa_55").value = fico;
document.getElementById("tfa_56").value = occupancy;
document.getElementById("tfa_57").value = cashout;
document.getElementById("tfa_58").value = rate;
document.getElementById("tfa_59").value = fees;
document.getElementById("tfa_60").value = points;
document.getElementById("tfa_61").value = trackingID;
});
</script>
您可以创建一个 URL
对象,然后使用 url.searchParams.get(param)
方法获取所需的参数。 More information.
工作示例:
const link = "http://www.example.com/test.html?state=CA&period=PERIOD_FIXED_30YEARS&loan=200000<v=80&transaction=54&property_type=34&fico=740&occupancy=49&cashout=0&rate=4.125&fees=510&points=0.204&trackingID=1445830871741638005";
const url = new URL(link);
function getParameterByName(name) {
return url.searchParams.get(name);
}
//Gathering the URL Parameters
var period = getParameterByName('period');
console.log(period);
硬编码 url 仅用于您的 URL 示例,在您的代码中使用:
const url = window.location.href
我正在尝试提取 URL 参数以传递给预建的 HTML 表单。
例子URL是:
?state=CA&**period**=PERIOD_FIXED_30YEARS&**loan**=200000<v=80&**transaction**=54&property_type=34&fico=740&occupancy=49&cashout=0&rate=4.125&fees=510&points=0.204&trackingID=1445830871741638005
粗体文字是我需要提取的内容。
我试图通过以下方式做到这一点:
function getParameterByName(name) {
name = name.replace(/[\[]/, "\[").replace(/[\]]/, "\]");
var regex = new RegExp("[\?&]" + name + "=([^&#]*)"),
results = regex.exec(location.search);
return results === null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}
//Gathering the URL Parameters
var period = getParameterByName('period');
var loan = getParameterByName('loan');
var ltv = getParameterByName('ltv');
var transaction = getParameterByName('transaction');
var property_type = getParameterByName('property_type');
var fico = getParameterByName('fico');
var occupancy = getParameterByName('occupancy');
var cashout = getParameterByName('cashout');
var rate = getParameterByName('rate');
var fees = getParameterByName('fees');
var points = getParameterByName('points');
var trackingID = getParameterByName('trackingID');
然后使用
分配给表格document.getElementById("tfa_49").value = period;
document.getElementById("tfa_50").value = loan;
我一直收到空错误,我认为这是正则表达式。
如有任何帮助,我们将不胜感激。我在网站上搜索了类似的问题,none 的答案适合我的情况。
更新工作代码:
<script type="text/javascript">
//Used to pull URL
const link = window.location.href;
const url = new URL(link);
//Function to get the URL parameters
function getParameterByName(name) {
return url.searchParams.get(name);
}
//Gathering the URL Parameters and writing to console to check if parse is accurate -- Note the console.log can be removed if needed
var period = getParameterByName('period');
console.log(period);
var loan = getParameterByName('loan');
console.log(loan);
var ltv = getParameterByName('ltv');
console.log(ltv);
var transaction = getParameterByName('transaction');
console.log(transaction);
var property_type = getParameterByName('property_type');
console.log(property_type);
var fico = getParameterByName('fico');
console.log(fico);
var occupancy = getParameterByName('occupancy');
console.log(occupancy);
var cashout = getParameterByName('cashout');
console.log(cashout);
var rate = getParameterByName('rate');
console.log(rate);
var fees = getParameterByName('fees');
console.log(fees);
var points = getParameterByName('points');
console.log(points);
var trackingID = getParameterByName('trackingID');
console.log(trackingID);
//On load function to prefill the forms hidden fields
$( document ).ready(function() {
document.getElementById("tfa_49").value = period;
document.getElementById("tfa_50").value = loan;
document.getElementById("tfa_51").value = ltv;
document.getElementById("tfa_53").value = transaction;
document.getElementById("tfa_54").value = property_type;
document.getElementById("tfa_55").value = fico;
document.getElementById("tfa_56").value = occupancy;
document.getElementById("tfa_57").value = cashout;
document.getElementById("tfa_58").value = rate;
document.getElementById("tfa_59").value = fees;
document.getElementById("tfa_60").value = points;
document.getElementById("tfa_61").value = trackingID;
});
</script>
您可以创建一个 URL
对象,然后使用 url.searchParams.get(param)
方法获取所需的参数。 More information.
工作示例:
const link = "http://www.example.com/test.html?state=CA&period=PERIOD_FIXED_30YEARS&loan=200000<v=80&transaction=54&property_type=34&fico=740&occupancy=49&cashout=0&rate=4.125&fees=510&points=0.204&trackingID=1445830871741638005";
const url = new URL(link);
function getParameterByName(name) {
return url.searchParams.get(name);
}
//Gathering the URL Parameters
var period = getParameterByName('period');
console.log(period);
硬编码 url 仅用于您的 URL 示例,在您的代码中使用:
const url = window.location.href