1

I just can not figure a way to use location.href to act like you're clicking on a link. What I am trying to do is get the variables included, for example,

The regular link is like this and works fine:

<a href="images/someImage.jpg" title="My Image" rel="gb_page_center[249, 266]">Click Me</a>

I am trying to call a function using href (or some other method) to open the image with the other parameters just as if you were to click on the link.

I hope I am explaining this correctly.

Thanks for any help.

-UPDATE-

I finally found my solution here Displaying the Popup box generated by Greybox on page load(onLoad)

Thank you all for your suggestions.

Community
  • 1
  • 1
  • add onclick event handler to the anchor. – akonsu Jan 06 '13 at 01:35
  • location.href = "images/someImage.jpg" title="My Image" rel="gb_page_center[249, 266]"; That doesnt work at all like I want. – user1952078 Jan 06 '13 at 01:37
  • if you just want to link to the image why do you need javascript? – Julian Krispel-Samsel Jan 06 '13 at 01:43
  • I am using javascript to eventually load the image but, I need the other parameters in the link to work because that is what makes the link open correctly, loading the image on top of the page. Otherwise, the image will just load, replacing the current page. – user1952078 Jan 06 '13 at 01:54
  • So after finally understanding better what I am trying to do, I think I am getting closer to a solution. I found this bit of code but it still doesn't seem to work as expected. `$('#myanchor').click();` Any other ideas? – user1952078 Jan 06 '13 at 22:22

4 Answers4

1

Answer: Yes.

<a href="javascript:yourFunction(); return false;" title="My Image" rel="gb_page_center[249, 266]">Click Me</a>
ddavison
  • 28,221
  • 15
  • 85
  • 110
1

Add an onclick event to your link. Using JQuery, you could ofcourse bind the click event to the links, and use .preventDefault() to block the anchor tag workin. Much cleaner code also.

$(document).ready(function(){
  $('a').bind('click', function(e){
    e.preventDefault();
    //do whatever you wish to do
  });
});
jdepypere
  • 3,453
  • 6
  • 54
  • 84
0
<a onclick="yourJSFunction();return false" href="images/someImage.jpg" title="My Image" rel="gb_page_center[249, 266]">Click Me</a>

You just need to add an onclick attribute to your anchor tag.
Make sure to return false or else the anchor tag will go to the URL like normal.

PleaseStand
  • 31,641
  • 6
  • 68
  • 95
dm03514
  • 54,664
  • 18
  • 108
  • 145
0

location.href or window.location.href refers to the browsers current address. If you change the value of location.href the browser will use that value as it's address. So if you want to navigate your browser to a link programmatically, you just have to change its value.

If you just want a clickhandler for your link don't use onclick or href, that's just bad practice, use a sepperate script that does this unobtrusively like so:

var button = document.getElementById('button');
button.onclick = function(){
     do_something();
}
Julian Krispel-Samsel
  • 7,512
  • 3
  • 33
  • 40