0

I'm a rookie at javascript. I'm trying to open an html file in the same directory when clicking an image. (My image displays but the onclick function doesn't work).

projects = document.createElement('div');
projects.style.position = 'absolute';
projects.innerHTML = '<img src="image.png" class="projectspage" style="display:block; margin-top: 50px; margin-left: 100px; background-color: red; height: 100px; width: 100px;" />';
projects.onClick = function() {
  window.open('projects.html')
};

uiContainer.appendChild(projects);
2
  • do you get an error? it's possible that you have a popup blocker running. Commented Jan 25, 2017 at 18:43
  • no i have no error or any indication that the button doesn't work. the console comes out with no errors. Commented Jan 25, 2017 at 18:52

1 Answer 1

2
projects.onClick = function() {
  window.location.href = 'valid-location-like-host.com/index.php
};

I'm assuming you mean redirect to a new page in the browser on click. The above is the correct way to implement that in pure js.

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

6 Comments

"I'm assuming..." Why not just ask with a comment?
I tried exactly that but it doesn't work. When selecting the image, it seems like no event is being triggered.
Actually you need to change href propertie, so you write it like: window.location.href = 'valid-location-like-host.com/index.php' or similar. Please edit your answer and it will be ok user2263572.
@Marco i've tried to do that although it still doesn't triggger any event.
Acuatlly it doesn't trigger any event, what it will do is redirect to that page. The code is valid for sure. Maybe if you provide a minimum working example we can help you more?
|

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.