(Please understand, English may be a mess.)
Try the first question in here.
I’m a student studying web programming. I make a Web Game. Use HTML, JavaScript. Not use Adobe Flash.
When, how can I select one image on image layers? I made two images. Use a web-photoshop.
-
BackGround.png
This image has no function. just background image.
-
go_Lobby.png
This image has two function.
-
Function
onClick = " go_Lobby() "
href = "Lobby.html"
, go to the lobby page. -
Function
onMouseOver = " OverEvent() "
Using effect at image when your mouse on image.
-
Below is the code:
<script>
function go_lobby{<br/>
location.herf="lobby.html";<br/>
}
</script>
<body>
<div style="position:absolute; z-index = 99;">
<img src = "map.png"/>
</div>
<div style="posistion:absolute; z-index = 10;">
<img src = "go_lobby.png" onclick = " go_lobby() " onMouseOver ="" />
</div>
</body>
But unlike my idea was to move the page when you click anywhere in the lobby.
How can you move to the page only when selecting the “Lobby” image?
2
Answers
Your JavaScript is flawed.
href
is spelt incorrectly – so you won’t be redirected.<br/>
in your JavaScript, which will cause the second line to not execute.Change your JavaScript to become the following
Now, when you
onclick
thego_lobby.png
image, a call will be made togo_lobby()
function, which will redirect the window tolobby.html
.Test runs
With your current JavaScript, you would have gotten these errors (looking at the console)
SyntaxError: Unexpected token {
SyntaxError: Unexpected token <
This is an absolute mess..
JavaScript code should not have HTML tags in it unless it’s inside of a string. Also you have a typo – it’s
location.href
notlocation.herf
. Furthermore if you’re defining a function in JavaScript it should have a function keyword, function name, function arguments and then the function body. If you want to redirect with JavaScript you can simply usewindow.location
.Fix your JavaScript part to the following:
You also have a typo in your HTML.
posistion
should beposition
. Empty element attributes are redundant if they aren’t set beforehand. Also if your setting an event event for an event likeonclick
then the spaces that you have there are just useless and can lead to problems.Fix your HTML to:
After this the image onclick event should trigger the redirect.