Disable href tag when value = declared [closed] - javascript

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I want to disable a href tag when the link of it is # , the reason i want to do this is because i have lots of post on my webpage that have links poiting to other sites that are my partners.
When i make a post i have a template that make it easier on me to put information on it and have the href link ready if anyone site that has to do with the post want a partnership and i link it to them .
Some of my old post have href tags with # value on it and when i click on them they open up my page on another tab so thats why i would like to disable it if possible with javascript (anyone other script language that can do the job is fine too)
Thanks

Simplest way would be:
var emptyAnchors = document.querySelectorAll('a[href="#"]');
for (var i = 0; i < emptyAnchors.length; i++) {
emptyAnchors[i].addEventListener('click', preventDefaultEvent);
}
function preventDefaultEvent(e) {
e.preventDefault();
}
This will prevent the default behavior on a link click (open the href target).

You can select your a tag with the specific # href
ES6 syntax
const elements = document.getElementsByTagName("a[href='#']");
ES5 syntax
var elements = document.getElementsByTagName("a[href='#']");
then add a preventDefault() on the event of each element to make sure the default behaviour won't be executed.
ES6 syntax
elements.addEventListener('click', e => {
e.preventDefault();
});
ES5 syntax
elements.addEventListener('click', function(e){
e.preventDefault();
});
If you want you can just add css to make the a tag lose its default style
a[href="#"] {
color: black;
pointer-events: none;
text-decoration: none;
}

Related

Save parameter from link on page as var in javascript [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I apologize for the lack of code on this one. I am totally lost on a starting point.
On a website there is a link such as the following:
This Is My Link
I am trying to get the accountId of 00T122233ABCEFG123 and use it as a variable. The link that appears above does not have an id, so there is no way to target it other than to find "accountId" on the page, as it only appears in the page once.
Any help is greatly appreciated.
You could use a selector to target the element based on the href attribute, and then extract the value through the URL api
const link = document.querySelector('a[href*="accountId"]');
const href = new URL(link.href);
const id = href.searchParams.get('accountId');
console.log(id);
This Is My Link
You can select it using the attribute contains selector :
a[href*=accountId]
Otherwise, maybe its parent has an ID? There are many ways to target an element in a page.

How can I make anywhere on a webpage clickable and navigate to an address without making the entire page a link? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I am trying to make this website http://quoteswag.ga click through to a random quote just by clicking anywhere on the whitespace or on the quote, but I don't want to have it seem like it is a link with the mouse hand and url status etc for aesthetic purposes. I have searched for a way to do this with javascript, but can't find the correct solution.
I just want the page to navigate to /random whenever a user click on whitespace or the quote, or the div enclosing the whitespace and/or quote div.
I'm assuming this won't be too hard with javascript or jquery, but I'm not a pro at these at all. Thanks.
Seem to be as easy as adding
<script type="text/JavaScript">
$('.redirection').click(function() { window.location.href = 'http://...'; });
</script>
And to add the class redirection to all clickable elements. Or, alternatively, to apply the event to all elements:
<script type="text/JavaScript">
$(document).click(function() { window.location.href = 'http://...'; });
</script>
Please be aware, that using the latter one applies the effect even to things like the logo, the footer etc.
From your website source, there is a div with class named container container-quote this div should be a hyper link as follows:
<a href="#">
<div class="container container-quote">.....
</div>
</a>
Then you have to apply the following CSS:
<style>
a:link, .container.container-quote *{
text-decoration: none;
color:inherit;
cursor: default;
}
</style>
Checkout this DEMO: http://jsbin.com/xevejuvohe/1/
Here try this:
<script type="text/javascript">
document.onclick = function (e) {
window.open("http://www.google.nl", "_blank");
return false;
};
</script>

href that only runs javascript [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I have an a tag inside of a form that is used to remove uploaded files. I don't want the a tag to take the user to another section of the page, or try to submit the form or anything like that, I only want it to run a javascript function. How can I accomplish this? I'm not sure what to put in the href?
You can use #:
function doSomething() {
alert('test');
}
Click me
Really, any URL can go there - the return false; short-circuits the default behaviour of the link.
In general, if at all possible, it's best to use a real URL that does the same thing your JS does, for users who have JS disable. This is called "graceful degradation".
You really need to add more code and examples to your questions in the future. Fortunately I know what you're actually asking.
In the function that runs when you click the link, you want to prevent its default behavior.
Here's a very simple example:
<a href="http://google.com" id="myanchor">
Javascript
document.querySelector('#myanchor').addEventListener('click', function (ev) {
ev.preventDefault();
// your code here.
});
<form>
<!-- other inputs-->
<input type='button' onclick='run()' />
</form>
Then for the Javascript:
function run() {
//do something
}

Remove all facebook [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
i have hard time try to kill all facebook features don't like, don't want, don't vereryting.
So i try using gresemonkey to do it, but so far i can't make it work every time i try, fail when i use get document id lets say "content" works fine buy when i try thinks like "fb-Like" don't do anything . there is what i have so far. i hope us can put me in then right way.
//this part becouse come fb-likes are like class and no have id
var killerface = document.getElementsByClassName("like-box");
for (var i = 0; i < killerface.length; i++) {
killerface[i].setAttribute("id","like-box");
}
this should be remove the child, but it's not...
var killerface2 = document.getElementById("like-box");
killerface2.parentNode.removeChild(killerface2);
have any ideas?
the idea is clean clear remove all pages using greasemonkey from facebook, google plus,any other that follows also some ads and everything dont like you. the idea is not only hide.
Add this in your HTML before last tag. (Need you have a jquery)
<style>
.deadface { display:none !important; }
</style>
<script>
$(function() {
$('.like-box').addClass('deadface');
});
</script>
Or this to completely remove
<script>
$(function() {
$('div.like-box').remove();
});
</script>

How can I apply a style to element if the user came to website using a link with an anchor/hash [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Let’s say I’ve put, somewhere on the internet, a link to my website. To be specific, it was a URL with an anchor/hash linking to a part of an article, like this: http://example.com/article/#subsection.
Now, if someone came through this link to my website, how can I trigger CSS/JS to make some action?
In my particular case, I want to add a style to the <div> with an id of subsection when someone uses the link containing #subsection.
Any ideas?
Given IE9 and later, this is possible entirely through the :target pseudo-class:
#subsection:target {
…
}
If what you're trying to do is to highlight a section on your page that matches the hash value in the current URL (which isn't entirely clear from the wording in your question) and you want to support a wide range of older browsers, then you can do something like this:
The hash value is not sent to the server so you would have to apply a change in client-side javascript. You can do that by adding a class to the object that matches the hash name when your page loads:
<script>
var id = window.location.hash.slice(1);
if (id) {
var obj = document.getElementById(id);
if (obj) {
obj.className += " specialStyle";
}
}
</script>
Or, using jQuery:
<script>
if (window.location.hash.length > 1) {
$(window.location.hash).addClass("specialStyle");
}
</script>
These scripts must be located anywhere after the elements you wish to add the class to or protected from executing until the DOM is loaded using something like $(document).ready(), though the sooner the script can run, the sooner the new style will show.
It's unclear to me whether the XSS vulnerability mentioned in the comments is still an issue in jQuery now or not, but you could either using the plain javascript version (which does not contain this vulnerability) or further protect against that in the jQuery version with something like this:
<script>
if (window.location.hash.length > 1) {
$(window.location.hash.replace(/[<>]/g, "")).addClass("specialStyle");
}
</script>

Categories

Resources