-2

I'm looking to pull the title of the current page into an img src URL.

For instance:

<title>booth106</title>
<img src="https://mywebsite.com/booth106/images/picture1.jpg">

Is there any code to put in so that as the title changes from page to page, so too will the booth number in the URL automatically. Is there a way to do this within a URL?

sebo620
  • 71
  • 7
  • That link is not functioning. Please how exactly do you want to put this? Do you have a snippet? – ABGR May 27 '20 at 06:55
  • @RahulDwivedi – I updated with code snippet but it's not a functioning link because it is just an example. I am looking to grab the text within the tag and put it in the src URL "...com/page-title-here/images/picture1.jpg" – sebo620 May 27 '20 at 06:59
  • 1
    are you using any framework? `var src='https://mywebsite.com/' + document.title + '/images/picture1.jpg';` this will give you a dynamic url – ABGR May 27 '20 at 07:01
  • Although there is a way to do this, I'd recommend finding a way your framework (if any) / stack has a simpler / more robust way to pass the data instead of using the title tag. What framework / stack are you using? – Olaf May 27 '20 at 07:04

2 Answers2

1

You can do it like this. When the document is ready it will find the page title and edit the url of the picture!

$('img').attr('src', 'https://www.mywebsite.com/'+ document.title +'/images/picture1.jpg');
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<img src="https://www.mywebsite.com/booth106/images/picture1.jpg" alt="my image" />
0
var src='https://mywebsite.com/' + document.title + '/images/picture1.jpg';
var img = document.getElementsByTagName('img')[0]; //considering you want to update the src of the first image

img.src = src;
ysf
  • 4,634
  • 3
  • 27
  • 29
ABGR
  • 4,631
  • 4
  • 27
  • 49