How to make js cut off and change jump links?
Not just changing the herf of <a>.
There is no way to stop the jump of js.
I want to make a php proxy like https://github.com/joshdick/miniProxy but it's not perfect.
When encountering a jump with js, it will not work.
You can preventDefault behaviour with JS. By default it will redirect to link but you can:
document.getElementById("yourlink").addEventListener("click", function(event){
event.preventDefault()
});
Also, you can access your link with:
document.getElementById("yourlink").href;
or event.target.href inside your listener
Your a tag will need to have id of yourlink to achieve that:
<a href="someurl" id="yourlink" />
Related
I have problem. Anchor link changes url. After i press a tag with #down. My url changes to localhost/#down . My anchor link change url. is there way to pervent anchor to change url?
Thanks
Use the following javascript code to prevent the default behaviour of your anchor
document.getElementById("myAnchor").addEventListener("click", function(event){
event.preventDefault()
});
I have a general question about jQuery.
I have created few jQuery buttons but the problem that I have is when you scroll half way down the page and you press the button, the page jumps back to top, instead of staying in the middle where the button is??
How can you stop this from happening, as it's frustrating for the user??
Is there any particular reason why it's happening?
jsFiddle Example:
$(".comment-here").click(function() {
$(".comment-form").slideToggle('slow');
});
$(".main-btn.orange").click(function(){
$(".comment-form").slideUp('slow');
});
You're not preventing the default event. Since you're clicking on an anchor tag, the default event for # is just to jump up to the top of the document.
To prevent this from occurring:
Pass the event into the function, and use preventDefault();
$(".comment-here").click(function(e){
e.preventDefault();
$(".comment-form").slideToggle('slow');
})
$(".main-btn.orange").click(function(e){
e.preventDefault();
$(".comment-form").slideUp('slow');
})
You could also use return false; instead, which goes a bit further than just preventing the default event, it will stop events bubbling up to parent elements.
Add return false; to the click handler of the <a class="comment-here">.
Essentially it is not jQuery, but the default browser behaviour that causes this: you clicked a link, it has to navigate to its href, which is... "#", i.e. this page. So there we go back to this page (the top).
Prevent default behaviour of anchor tags:
$(".comment-here").click(function(e){
e.preventDefault();
//....
});
The problem is with your <a> tag using href="#" which references the current page and pulls you to the top.
I'd recommend the following approaches, instead of the default event prevention mentioned in other answers. Of course those work, but why use an element only to remove it's default functions?
Ergo, use something that is intended for your purpose:
1.) Use a <button> instead of <a>:
http://jsfiddle.net/RvHjx/7/
2.) If you're dead set on using <a>, remove the href attribute in your <a> tag. This will remove the blue-underlined link styling (the link still functions correctly though), but it's easy to fix with some CSS:
http://jsfiddle.net/RvHjx/11/
Adding javascript:; to your a tags href is the simplest way.
my link
I have a gallery of photos with a next and previous button. If one of my javascript methods is broken for one reason or another when one of the buttons is clicked it will add a hash to the url i.e. www.google.com# . I know the hash can be given a div id to jump to that part of the page but when it's blank it jumps around my page a few times and I'm not sure what it's targeting. I thought of attempting to remove the hash from the url but then I'd have to ensure that on every action and that seems like bad practice. I would prefer if the hash just made no difference to the actions on the page.
Don't use href="#", it is improper.
Instead, use href="javascript:;" or a variant thereof (personally I use javascript:void(null);)
This explicitly states that the link does not go to another location, but is handled by JavaScript.
I guess Next And Prev button has <a href="#" ...</a> like markup. In this case you can add event listener to those links with jquery
$('#prev, #next').on({
'click': function(e) {
e.preventDefault();
}
})
and avoid changing location by browser. Or in pure javascript:
document.querySelectorAll('#prev, #next').addEventListener('click', function(e) {
e.preventDefault();
},false)
//Note: this code is not cross-browser
Don't use an anchor tag when you don't want to perform a navigation function. Use button
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:
Click Me
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.
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
});
});
Answer: Yes.
Click Me
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();
}
<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.
I have a tag like below. The question I have is how do I make this <a/> tag not behave like a link when user right clicks it. Since on a regular click the onclick event will fire and return false I am good with the regular click on the link the issue comes when a user right clicks the mouse and then gets the option like open in new tab or open in new window I have to prevent this from happening. I found out I can use javascript:void(0) in the href to do that but for some reason I cannot change the href as it is used for some other stuff. Is there any even or something that I can use.
<A title="Test1" onclick="javascript:search1('search'); return false;"href="team">search</A>
Thanks
as often, there's no universal solution, every browser do it its way. HTML 5 says form.oncontextmenu event handler should be supported. So this
<script>
document.oncontextmenu=function("alert('dont play with sources');return false");
</script>
should work if you use HTML 5.
you can also remove the javascript word, onclick already waits for js code (as oncontextmenu does).
<a onclick="search1('....