我正在使用Google跟踪代码管理器在我的其他分析脚本中触发Facebook像素。没有深入细节,我想做的是以下几点:
我的自定义HTML标记代码:
<script>
var element = {{Click Element}};;
var linkclickurl = '{{Click URL}}';
alert('Click URL is ' + linkclickurl + '. Hostname is {{Page Hostname}}');
// Check if hostname is contained in linkclickurl
if(linkclickurl.indexOf("{{Page Hostname}}") > -1) {
alert('Not outbound link');
}
else {
// Check if query string already exists
if(linkclickurl.indexOf("?") > -1) {
window.location.href = linkclickurl + '&fb=1';
alert('Found a query');
}
else {
window.location.href = linkclickurl + '?fb=1';
alert('Found no query');
}
}
</script>
所以我当前的问题是我不知道在链接点击后如何阻止链接传播。我正在改变href,因为页面将重定向到原始链接,因此无效。
有没有办法阻止链接在传出时传播,然后将其切换到更新版本?
答案 0 :(得分:0)
采用@sdhaus建议,我能够使用以下脚本执行此操作:
function updateLinks(parameter, value)
{
var links = document.getElementsByTagName('a');
var includeDomains = self.location.host;
for (var i=0;i<links.length;i++)
{
if(links[i].href != "#" && links[i].href != "/" && links[i].href != "" && links[i].href != window.location) //Ignore links with empty src attribute, linking to site root, or anchor tags (#)
{
var updateLink = true;
if(links[i].href.toLowerCase().indexOf(includeDomains.toLowerCase()) != -1) //Domain of current link is included i the includeDomains array. Update Required...
{
updateLink = false;
}
if(!updateLink)
{
//Do nothing - link is internal
}
else
{
var queryStringComplete = "";
var paramCount = 0;
var linkParts = links[i].href.split("?");
if(linkParts.length > 1) // Has Query String Params
{
queryStringComplete = "?";
var fullQString = linkParts[1];
var paramArray = fullQString.split("&");
var found = false;
for (j=0;j<paramArray.length;j++)
{
var currentParameter = paramArray[j].split("=");
if(paramCount > 0)
queryStringComplete = queryStringComplete + "&";
if(currentParameter[0] == parameter) //Parameter exists in url, refresh value
{
queryStringComplete = queryStringComplete + parameter + "=" + value;
found = true;
}
else
{
queryStringComplete = queryStringComplete + paramArray[j]; //Not related parameter - re-include in url
}
paramCount++;
}
if(!found) //Add new param to end of query string
queryStringComplete = queryStringComplete + "&" + parameter + "=" + value;
}
else
{
queryStringComplete = "?" + parameter + "=" + value;
}
links[i].href = links[i].href.split("?")[0] + queryStringComplete;
}
}
else
{
//Do nothing
}
}
}
&#13;