0

I have a HTML page which that using an image name as URL parameter. Why doesn't show the image, what do i wrong?

<!DOCTYPE html>
<html lang="nl">
<head>
    <script src="lib/functions.js"></script>
</head>


<body>

<!-- test03.html?parameter=mountains.jpg -->

<img src="" id="imagename">

<script>
    const queryString = window.location.search;
    const urlParameters = new URLSearchParams(queryString);
    const Parameter = urlParameters.get('parameter');
    document.getElementById("imagename").innerHTML = Parameter;
</script>

</body>

</html>
1
  • 2
    Which bit doesn't work? Do you get any errors in the console? The last line of your script looks wrong at first glance, probably want to explicitly set the SRC of the image. Commented Mar 13 at 10:40

2 Answers 2

3

You don't see your picture because you changed innerHTML, and it has nothing to do with the image's source for img HTML tag.

The right way is document.getElementById("imagename").src = Parameter instead of document.getElementById("imagename").innerHTML = Parameter for this scenario.

Make sure you have this image and the image path is correct.

Sign up to request clarification or add additional context in comments.

1 Comment

Great, thank you so much! It works!
0

I have an alternative solution that could work depending on the use case. I use the hash of the URL to decide what image to show.

div.image {
  display: none;
}

div:target {
  display: block;
}
<ul class="menu">
  <li><a href="#image01">Image01</a></li>
  <li><a href="#image02">Image02</a></li>
</ul>

<div class="image" id="image01">
  <img src="https://placehold.co/200x150/orange/white">
</div>
<div class="image" id="image02">
  <img src="https://placehold.co/200x150/tomato/white">
</div>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.