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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
HTML

From novice to tech pro — start learning today.