Displaying a simple HTML page from input embedded into a URL (part II)

hi,

I'd like to adapt the following great little script to allow links to be shown with a display text instead of the URL:

https://www.experts-exchange.com/questions/29086043/Displaying-a-simple-HTML-page-from-input-embedded-into-a-URL.html


ie instead of showing

https://www.google.co.uk/search?q=apples

to show

apples

Note the URL could be anything, as per adjustment in this comment: https://www.experts-exchange.com/questions/29086043/Displaying-a-simple-HTML-page-from-input-embedded-into-a-URL.html#a42481743

Thanks
xeniumAsked:
Who is Participating?
 
Julian HansenCommented:
Here is the script for reference
<script>
$(function() {
  if (window.location.search.indexOf('=') > -1) {
    var href = window.location.search.split('=')[1].split(',');
    var list = $('#links');
    for(var i = 0; i < href.length;i++) {
      var link = document.createElement('a');
      link.href = 'https://www.google.co.uk/search?q=' + href[i];
      link.target = '_blank';
      list.append($('<li>', {class: 'list-group-item'}).append($(link).html(href[i])));
    }
  }
});
</script>

Open in new window

We are interested in line 10
list.append($('<li>', {class: 'list-group-item'}).append($(link).html(link.href)));

Open in new window

Specifically this part
.html(link.href)

Open in new window

This is where we set the text - to change it to show the search term all we do is change the html(link.href) to the href[ i] which is the current search term we are dealing with. Like this
<script>
$(function() {
	if (window.location.search.indexOf('=') > -1) {
		var href = window.location.search.split('=')[1].split(',');
		var list = $('#links');
		for(var i = 0; i < href.length;i++) {
			var link = document.createElement('a');
			link.href = 'https://www.google.co.uk/search?q=' + href[i];
			link.target = '_blank';
			list.append($('<li>', {class: 'list-group-item'}).append($(link).html(href[i])));
		}
	}
});
</script>

Open in new window

Updated sample http://www.marcorpsa.com/ee/t3003.html?links=apple,banana,orange
0
 
xeniumAuthor Commented:
Thanks a lot, that works well where the display text is the search term as in my first example.

I will post a follow-up question where the display text is provided in the URL.

Thanks
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.