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
Add a ment  | 

4 Answers 4

Reset to default 5

For 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