1
0
-1

Hi,

I need to add some script to the 'head' element of a Composer form.

I've read this question (Composer - include script references in the HTML Form Header) and also have other scripts added to the 'Page Tracking' tab that function as expected.

I need to add some dynamic script, and capture a variable; the variable then needs to be persisted in the form.

I am able to add the dynamic script, but cannot store the variable in the form. I have tried various techniques to 'getElementById' and others, but they are challenging because of the way Composer handles id's.

I have tried using 'document.evaluate' and XPath, but with limited success. I can get the result of the evaluate, but can't seem to get the actual node to be able to add content to it.

<script>
var r=Math.floor(Math.random() * 899999 + 100000),
sid='beyond'+r+new Date().getTime();

(function(w,d,s,m,o,i) {
var h=d.getElementsByTagName(s)[0],
j=d.createElement(s);
j.src='https://'+m+'/fp/tags.js?org_id='+o+'&session_id='+i;
h.parentNode.insertBefore(j,h);
}
)(window,document,'script','sub_domain','org_id',sid);

function load(id) {
console.log("load: id= " + id);
var t=document.createTextNode(sid)
var n = document.evaluate("//input[@data-sfc-name='TMxSID']", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null);
console.log("n.singleNodeValue: " + n.singleNodeValue);
n.singleNodeValue.textContent = id;
// console.log("n= " + n.singleNodeValue.textContent);
// alert("n= " + n.singleNodeValue.textContent);
// n.appendChild(t);
}

document.onload = load(sid);
</script>

The dynamic function works (it follows the same pattern as the Google Tag Manager script); the script tag is added to the document.

However, the load function does not work. The 'load' function is called, but the rest of the function does not add any text to the element. It seems that "n.singleNodeValue" is null within the function, but using dev tools an object exists.


Any ideas would be appreciated.

Thanks

Mark

    CommentAdd your comment...