I wanted to click on an image, which will call a javascript function. The function is intended to change from one page to another web page on the same site. I don't want to open() a new page, but an existing page within the current site. I tried using an anchor tag, with the href targetting the url of the destination. But I dont want an anchor, I want to use a clickable image. I also tried the window.open() method but ended up with lots of opened windows! I then tried donuts "window.location.replace(newUrl);" which worked instantly. How do I vote-up donuts advice from pure time ago? Can't see no up-arrow to the left of the post?
Asked
Active
Viewed 52 times
-1
-
Does this answer your question? [How can I change the current URL?](https://stackoverflow.com/questions/3846935/how-can-i-change-the-current-url) – Donut Oct 27 '21 at 15:44
-
Doesn't this also essentially answer the question? `
` Just put the image or button inside the anchor tags. – ᴓᴓᴓ Oct 27 '21 at 15:45
-
@talfreds pretty much with exception that `img` in an empty tag and has no closing tag... – tacoshy Oct 27 '21 at 15:45
-
I'll edit it both work though – ᴓᴓᴓ Oct 27 '21 at 15:46
-
`open()` accepts other parameters next to the URL. `_blank` is the default behaviour. You can however change it: `window.open('https://duckduckgo.com','_parent')` More details here: https://www.w3schools.com/jsref/met_win_open.asp – Timo Oct 27 '21 at 15:55
-
Donut I'm not suprised your rep is so high. Your line "window.location.replace(newUrl);" worked instantly. Thankyou. Timo yours did not, I just ended up with many open windows that I did not want. – SiO Oct 28 '21 at 23:08
1 Answers
1
You don't need JavaScript to make a button or image into a link.
For an image, as others have pointed out in the comments, just do
<a href="..."> <img src="..."> </a>
For a button, see How to create an HTML button that acts like a link.

Fii
- 190
- 13
-
I apologise for not being clearer! I have no intention of using any forms in my program, so the above link does not help. – SiO Oct 28 '21 at 22:01
-
@SimonOsaka The second answer (https://stackoverflow.com/a/2906611/1798629) to that question shows you how to do it with just a ` – Fii Nov 01 '21 at 12:37