admin管理员组文章数量:1345728
I am trying to have a redirect on click of an image, but nothing happens. Can anyone please guide me.
<img src="img/facebook.png" style="border:0px;margin:2px;padding:2px;left;width:26px; height:20px;" onclick="window.location.href('');"/>
I am trying to have a redirect on click of an image, but nothing happens. Can anyone please guide me.
<img src="img/facebook.png" style="border:0px;margin:2px;padding:2px;left;width:26px; height:20px;" onclick="window.location.href('https://www.facebook./pages/Gr81');"/>
Share
Improve this question
edited Apr 29, 2015 at 3:25
zed
2,3484 gold badges28 silver badges45 bronze badges
asked Apr 29, 2015 at 2:18
hello temp10hello temp10
411 gold badge1 silver badge7 bronze badges
4 Answers
Reset to default 5For me, the following woked:
<img src="img/facebook.png" style="border:0px;margin:2px;padding:2px;left;width:26px; height:20px;" onclick="javascript:window.location='https://www.facebook./pages/Gr81';" />
What I put in the onclick
was
onclick="window.location='https://www.facebook./pages/Gr81';"
The way to assign a DOMString to a window.location property is by
window.location = "some url";
instead of
window.location('some url'); //incorrect
You could also do window.location.href
instead of window.location
, which seems like what you wanted to do in your code.
if you can wrap the image with an anchor tag around then you can simply do
<a href="https://www.facebook./blah">
<img src="img/facebook.png" style="border:0px;margin:2px;padding:2px;left;width:26px; height:20px;" />
</a>
Or a cleaner way would be to use a class name and jquery.
Now you have:
in the
<head>
or a .css include<style>.fbicon{border:0px;margin:2px;padding:2px;width:26px;height:20px;}</style> </head>
your image in the body
<img src="img/facebook.png" class="fbicon"/>
<script src="https://ajax.googleapis./ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script src="https://code.jquery./ui/1.11.4/jquery-ui.min.js"></script>
<script>
$('.fbicon')(function() {
//alert( "go to site" );
window.location='https://www.facebook./blah';
});
</script>
</body>
you can do it this way
<img src="img/facebook.png" style="border:0px;margin:2px;padding:2px;left;width:26px; height:20px;" onclick="window.location='https://www.facebook./pages/Gr81'"/>
In the DOM (Document Object Model), the WINDOW object represents an open window in a browser. It has a "location" property that you can call and give it a value of your desired redirect URL like this:
<script>
$('.fbicon').click(function() {
window.location='https://www.facebook./blah';
});
Yet another example without JQUERY:
<button onclick="myFunction()">Take me to CNN.</button>
<script>
function myFunction() {
location.href = "http://www.cnn.";
}
</script>
See these URLs for DOM element properties and values: https://www.w3schools./jsref/prop_loc_href.asp https://www.w3schools./jsref/obj_location.asp
本文标签: javascriptRedirect on click of an imageStack Overflow
版权声明:本文标题:javascript - Redirect on click of an image - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743765315a2535128.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论