Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

replace text with hyperlink

function handleTextNode(textNode) {
    if(textNode.nodeName !== '#text'
        || textNode.parentNode.nodeName === 'SCRIPT' 
        || textNode.parentNode.nodeName === 'STYLE'
    ) {
        //Don't do anything except on text nodes, which are not children 
        //  of <script> or <style>.
        return;
    }
    let origText = textNode.textContent;
    let newHtml=origText.replace(/river/g,'<a href="http://www.cnn.com">asdf</a>');
    //Only change the DOM if we actually made a replacement in the text.
    //Compare the strings, as it should be faster than a second RegExp operation and
    //  lets us use the RegExp in only one place for maintainability.
    if( newHtml !== origText) {
        let newSpan = document.createElement('span');
        newSpan.innerHTML = newHtml;
        textNode.parentNode.replaceChild(newSpan,textNode);
    }
}

//Testing: Walk the DOM of the <body> handling all non-empty text nodes
function processDocument() {
    //Create the TreeWalker
    let treeWalker = document.createTreeWalker(document.body, NodeFilter.SHOW_TEXT,{
        acceptNode: function(node) { 
            if(node.textContent.length === 0) {
                //Alternately, could filter out the <script> and <style> text nodes here.
                return NodeFilter.FILTER_SKIP; //Skip empty text nodes
            } //else
            return NodeFilter.FILTER_ACCEPT;
        }
    }, false );
    //Make a list of the text nodes prior to modifying the DOM. Once the DOM is 
    //  modified the TreeWalker will become invalid (i.e. the TreeWalker will stop
    //  traversing the DOM after the first modification).
    let nodeList=[];
    while(treeWalker.nextNode()){
        nodeList.push(treeWalker.currentNode);
    }
    //Iterate over all text nodes, calling handleTextNode on each node in the list.
    nodeList.forEach(function(el){
        handleTextNode(el);
    });
} 
document.getElementById('clickTo').addEventListener('click',processDocument,false);
Comment

PREVIOUS NEXT
Code Example
Javascript :: reverse linklist in javascript 
Javascript :: axios with load more 
Javascript :: keep records of number rolled in array javascript 
Javascript :: jasmine returnvalues example 
Javascript :: error 28 mongodb 
Javascript :: https://tutorial.eyehunts.com/js/javascript-escape-backslash-example-code/ 
Javascript :: will stop the loop if the array has any negative number and return all the positive numbers before the negative numbers 
Javascript :: javascript blur get new target 
Javascript :: Private slots are new and can be created via Static initialisation blocks in classes 
Javascript :: cache blogposts for 24 hours react native 
Javascript :: upload image in react next js authentication 
Javascript :: many button with many action in javascript 
Javascript :: on change jquery kendo switch 
Javascript :: js set to array casting 
Javascript :: url.createobjecturl 
Javascript :: verify number of times request was made in cypress 
Javascript :: Example of Numeric Separators in es12 
Javascript :: Multiline string in ES6 
Javascript :: selected item from dropdownlist 
Javascript :: what is render in react native 
Javascript :: all files website checker 
Javascript :: random number between 0 and 50 then round to a whole number 
Javascript :: loop through async javascript -IMP 
Javascript :: change candle color react highcharts 
Javascript :: how to add edit and delete rows of a html table with javascript 
Javascript :: grepper answer 
Javascript :: .datepicker make modal exit 
Javascript :: update mongoose 
Javascript :: how to rmeove white space in a string with jquery 
Javascript :: how to place text input cursor to start in react native 
ADD CONTENT
Topic
Content
Source link
Name
8+7 =