Jump to content

Anchor event (#link) using onclick event


Elemental

Recommended Posts

Hey Folks, Quick question about anchor links in onclick() events.How do you accomplish this type of html anchor link (#link) using JavaScript?

<a href="blahblah.html#link">link</a>

I tried the same thing with an onclick() event,

<a href="blahblah.html#link" onclick="open_win(); return false">link</a>

but it did not work.?????Peace,Elemental

Link to comment
Share on other sites

What didn't work, what happened and what were you expecting to happen?
justsomeguy, Que pasa amigo?The page opened alright but it did not take me to the specific section on the page I wanted to go, #link1I have this on each link <a href="blahblah.html#link1">link</a>, each link would of coures have it's own anchor, #link2, #link3..., for those that don't have JavaScript enabled; As you know this link would take them to the specific section within the blahblah.html page, #link1, #link1, #link2 ...For those that have JavaScript enabled, a small ToolTip pops-up on a mouseover event with an Image and a small discription of what the link is. Within the ToolTip I have another link that has this:<a href="blahblah.html#link" onclick="open_win(); return false">link</a>.I thought that this would do the same as the first link, the html anchor, but it did not. Like I mentioned above it opens the page but does not take the user to the specific #link1, #link2, ... section.That's what's not workingPeaceElemental
Link to comment
Share on other sites

Is the open_win function sending it to the right URL? Is is stripping off the end of it or something? What does the open_win function look like?
Justsomeguy,Here you go. Do I need to include the #link within it? I didn't think so but ...
<script type="text/javascript">function open_win(){window.open("nutriInfo.html","_blank","toolbar=no,location=no,directories=no,status=no,statusbar=no,menubar=no,scrollbars=yes,resizable=no,copyhistory=no,width=800,height=600,screenX=50,screenY=100,top=50,left=100");}</script>

Peace,Elemental

Link to comment
Share on other sites

Yeah, you need to include the URL you want to load. Or, in other words, it's only going to open whatever is in the window.open call. You can set it up this way if you want to open whatever the href is that they clicked on:<a href="blahblah.html#link" onclick="open_win(this.href); return false">link</a>

function open_win(theurl){window.open(theurl,"_blank","toolbar=no,location=no,directories=no,status=no,statusbar=no,menubar=no,scrollbars=yes,resizable=no,copyhistory=no,width=800,height=600,screenX=50,screenY=100,top=50,left=100");}

Link to comment
Share on other sites

Do you have something like this:<p id="link1">Blah blah blah...</p> If not, the link won't work.
Natechs Thanks for the reply, Yes I do, all four links have their own id.Peace,Elemental
Link to comment
Share on other sites

Yeah, you need to include the URL you want to load. Or, in other words, it's only going to open whatever is in the window.open call. You can set it up this way if you want to open whatever the href is that they clicked on:<a href="blahblah.html#link" onclick="open_win(this.href); return false">link</a>
function open_win(theurl){window.open(theurl,"_blank","toolbar=no,location=no,directories=no,status=no,statusbar=no,menubar=no,scrollbars=yes,resizable=no,copyhistory=no,width=800,height=600,screenX=50,screenY=100,top=50,left=100");}

justsomeguy, So the (this.href) argument of the onclick() event is what triggers the actual #link anchor to happen?Peace,Elemental
Link to comment
Share on other sites

That just passes the href of the link you're clicking on to the function so that the function can open the same link.
justsomeguy, Thank you sir, most helpful.Peace,Elemental
Link to comment
Share on other sites

Hey Folks, Does Opera have an issue with the (#anchorName) cause it ain't working as it should, It opens a new tab instead of a new window?The code is the same as in the example given in this post, it works fine in IE, FF and Safari Peace,Elemental

Link to comment
Share on other sites

It's a browser setting whether or not to open a link in a new tab or window, that's not something you can control.
justsomeguy, Oh really?! Well I guess I'll have to figure out how to use an if statement then that aplies to Opera only. *@#&% !!!Thanks man, or as a ninja would say "justsomeguy-son, domo arigato"Peace,Elemental
Link to comment
Share on other sites

That's actually a setting in every browser. IE7+, Firefox, Safari, and Opera all have settings on whether to open a new page in a new window or a new tab. You can't override those types of settings with HTML code, they're actually there in the browser specifically so you can have your browser work like you want, regardless of what the page is telling it to do. It's just something you have to work around as a developer, don't assume that a new page will be in a new tab or window, it could be in either.

Link to comment
Share on other sites

That's actually a setting in every browser. IE7+, Firefox, Safari, and Opera all have settings on whether to open a new page in a new window or a new tab. You can't override those types of settings with HTML code, they're actually there in the browser specifically so you can have your browser work like you want, regardless of what the page is telling it to do. It's just something you have to work around as a developer, don't assume that a new page will be in a new tab or window, it could be in either.
justsomeguy, Oh the sleepless nights, the wife's going to think I'm having an online affair.Peace,Elemental
Link to comment
Share on other sites

Indeed, the internet is a cruel mistress.
justsomeguy, Okay, I'm being a little lazy here but... Would it be fair to say that the DOM holds the key to check for these settings?Peace,Elemental
Link to comment
Share on other sites

I'm not aware of a way to check those settings, the DOM only applies to the structure of the document being rendered. Other than the string that identifies what the browser is, the browser doesn't publish much about it in the DOM or otherwise.

Link to comment
Share on other sites

I'm not aware of a way to check those settings, the DOM only applies to the structure of the document being rendered. Other than the string that identifies what the browser is, the browser doesn't publish much about it in the DOM or otherwise.
justsomeguy, Okay then what would you recommend I read?If I can't have a window open where or how I would like it to because the user has control over those settings, and you're telling me that you know of no way to read those settings and then design accordingly, how would one go about designing for that?I'm not asking for a "do-it-for-me" answer just a pointer in the right direction.Peace,Elemental
Link to comment
Share on other sites

I'm just saying proceed normally, you can use window.open like you normally do or target="_blank" or whatever, but just don't assume that the new page is definitely in either a standalone window or a tab. So don't have language like "click here to open in a new tab", because it could be either. Practically, as far as Javascript goes, it doesn't really matter. If you open a new page in either a tab or a window, window.opener will still point to the page that opened it, etc. The DOM for the new page will be the same regardless of how the browser chooses to open it (as far as I know, anyway). Why is it an issue where the page opens?

Link to comment
Share on other sites

I'm just saying proceed normally, you can use window.open like you normally do or target="_blank" or whatever, but just don't assume that the new page is definitely in either a standalone window or a tab. So don't have language like "click here to open in a new tab", because it could be either. Practically, as far as Javascript goes, it doesn't really matter. If you open a new page in either a tab or a window, window.opener will still point to the page that opened it, etc. The DOM for the new page will be the same regardless of how the browser chooses to open it (as far as I know, anyway). Why is it an issue where the page opens?
justsomeguy, I'm a Taurus, everything's an issue.But seriously, and I'm sure it's due to my lack of experience, I visually designed the page, that the JavaScript code would open, so that everything would fit within the width of the window so naturally if it doesn't open that way it wont look as nice, everything gets pushed to the left of the screen. Again, I'm sure it's all due to my inexperience.PeaceElemental
Link to comment
Share on other sites

Yeah, if a window meant to be opened at a certain size gets opened fullscreen then everything will be in the upper left. Other than centering everything, I'm not sure what else to do. I would probably just center it and move on, but then again I'm a programmer and not a graphic designer.

Link to comment
Share on other sites

Yeah, if a window meant to be opened at a certain size gets opened fullscreen then everything will be in the upper left. Other than centering everything, I'm not sure what else to do. I would probably just center it and move on, but then again I'm a programmer and not a graphic designer.
justsomeguy, "...I'm a programmer and not a graphic designer." that explains the patients you have with people like me. Thanks again, and again, and again.....Peace,Elemental
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...