将URL参数拉取到WordPress "Raw HTML"内容元素中



我正在使用URL Params插件使用短代码将参数拉入常规内容。但是我必须使用原始HTML块将Typeform代码插入页面,并且我希望能够将URL参数传递到Typeform代码中以跟踪表单提交的来源。

我不知道该怎么做。该表单在以下位置工作正常: https://HelloExit.com/instant-valuation

但我希望能够派人去 https://HelloExit.com/instant-valuation/?source=XXXX 并将 XXXX 拉入 Typeform 代码中作为"data-url"中的"源"值

这是我尝试过的:

<script>
function getUrlVars() {
var vars = {};
var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) {
vars[key] = value;
});
return vars;
}
var source = getUrlVars()["source"];
</script>

<div
class="typeform-widget"
data-url="https://xgenius.typeform.com/to/zZHPPk?source=<script>document.write(source)</script>"
data-transparency="100"
data-hide-headers=true
data-hide-footer=true
style="width: 100%; height: 500px;">
</div>

<!-- Typeform embed code -->
<script>(function() { var qs,js,q,s,d=document, gi=d.getElementById, 
ce=d.createElement, gt=d.getElementsByTagName, id="typef_orm", 
b="https://embed.typeform.com/"; if(!gi.call(d,id)) { js=ce.call(d,"script"); js.id=id; 
js.src=b+"embed.js"; q=gt.call(d,"script")[0]; q.parentNode.insertBefore(js,q) } })() 
</script><div style="font-family: Sans-Serif;font-size: 12px;color: #999;opacity: 0.5;padding-top: 5px;"> powered by <a href="https://admin.typeform.com/signup?utm_campaign=zZHPPk&utm_source=typeform.com-01D8JVB4T91A26RA6WEZRZVF05- pro&utm_medium=typeform&utm_content=typeform-embedded-poweredbytypeform&utm_term=EN" style="color: #999" target="_blank">Typeform</a></div>

任何帮助将不胜感激!

你很接近了,但你需要使用 Javascript 来改变你的div 的data-url属性。

// ...
var source = getUrlVars()["source"];
// concatenate the url with your source variable
var newUrl = `https://xgenius.typeform.com/to/zZHPPk?source=${source}`;
// get the element whose attributes you want to dynamically set
// https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelector
var widgetElement = document.querySelector('.typeform-widget');
// set the source attribute
// https://developer.mozilla.org/en-US/docs/Web/API/Element/setAttribute
widgetElement.setAttribute('data-url', newUrl);

请仔细测试,因为它可能仍会以争用条件结束(也就是说,Typeform 嵌入代码可能会在您更新它引用的data-url属性之前开始运行(。

最新更新