Skip to content

Commit

Permalink
placholder replacements in heref
Browse files Browse the repository at this point in the history
  • Loading branch information
kameshsampath committed Oct 14, 2020
1 parent e0a761e commit 81820e5
Showing 1 changed file with 26 additions and 3 deletions.
29 changes: 26 additions & 3 deletions src/js/07-userparams-behaviour.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ document.addEventListener('DOMContentLoaded', function () {
}

function replaceParamsInNodes (node, pattern, value) {
var re = new RegExp(pattern, 'gi')
if (node.nodeType === 3) {
var re = new RegExp(pattern, 'g')
var text = node.data
if (text.match(re)) {
if (value) {
Expand All @@ -52,16 +52,39 @@ document.addEventListener('DOMContentLoaded', function () {
allQueryPramLinks.forEach(appendQueryStringToHref)
}

var pramLinks = document.querySelectorAll('.params-link')
if (pramLinks) {
pramLinks.forEach(appendQueryStringToHref)
}

var allNavLinks = document.querySelectorAll('.nav-link')
if (allNavLinks) {
allNavLinks.forEach(appendQueryStringToHref)
}

function appendQueryStringToHref (el) {
var queryString = window.location.search
var appendQueryString = el.classList.contains('query-params-link') ||
el.classList.contains('nav-link')
if (!hasQueryString(el.href) && queryString) {
// console.log('No Query String in %s, adding.', el.href)
el.href += queryString
var href = el.href
//console.log('Orginal href %s', href)
for (var i = 0; i < keys.length; i++) {
//console.log('href %s', href)
//(%25key%25|%key%) %25 is urlencode value of %
var paramKeyPattern = '(' + '%25' + keys[i] + '%25' +
'|' + '%' + keys[i] + '%' + ')'
//console.log('Replacing %s', paramKeyPattern)
var re = new RegExp(paramKeyPattern, 'gi')
href = href.replace(re, allParams[keys[i]])
//console.log('after replace href %s', href)
}
if (appendQueryString) {
el.href = href + queryString
} else {
el.href = href
}

}
}
})

0 comments on commit 81820e5

Please sign in to comment.