href that only runs javascript [closed] - javascript

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
}

Related

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>

Temporarily comment out <script> tag for debugging [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
Often while coding and debugging I might want to comment out a <script> tag. An example might be when doing the following:
<script src="lib/jquery.js"></script>
<!--script src="lib/jquery.min.js"></script-->
I tend to add the new line instead of just changing the original to act as a reminder that I want to put that back before going live. I got this syntax from a colleague but I had never seen this it before. Is there a syntactically correct method to comment out <script> tags in HTML?
EDIT: I know there are lots of discussions about commenting out scripts in order to hide them from older browsers but that is not what I am doing. I am wanting to hide the tag completely.
One option would be to dynamically load your scripts in, given a debug flag. For example:
Markup:
<script src="lib/include.js"></script>
include.js
var IS_DEBUG = true;
if(IS_DEBUG) {
loadScript("jquery.js");
loadScript("anotherscript.js");
}
else {
loadScript("jquery.min.js");
loadScript("anotherscript.min.js");
}
function loadScript(name) {
var elem = document.createElement("script");
elem.src = name;
document.getElementsByTagName("head")[0].appendChild(elem);
}
That means you can just toggle the IS_DEBUG flag to load in the required scripts. This is a very rudimentary example, but you get the idea. You might even be able to tie this in with something like require.js

Just wondering if this is poor JS coding practice [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
I have 2 separate functionalities on one page of my site, both of which call the same Ajax function. The 2 functionalities need to be distinguishable. My solution was to pass a string (called string here) in one of the functions, such as in the following simplified example.
<script type="text/javascript">
function AjaxFunction(string) {
if (string === 'String') {
alert('You have clicked Button2');
} else {
alert('You have clicked Button1');
}
}
</script>
<button type="button" id="Button1" onclick="AjaxFunction()">Click1</button>
<button type="button" id="Button2" onclick="AjaxFunction('String')">Click2</button>
This seems perfectly fine to me. The only issue is that string is null when Button1 is clicked. I imagine there is no problem with this since the if...else takes care of any ambiguity of what to do with string. I have tested with my actual Ajax function and everything works OK, but since I am still somewhat new to coding I always imagine that stupid mistakes I am not aware of will destroy the site. Sorry for the trivial question but am I missing any performance issues, bugs, etc. with this approach? Thank you for any help!
You need to pass an empty string into the first button call. So:
onclick="AjaxFunction('')"

Linking two elements in jQuery [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 9 years ago.
Improve this question
Is there a possibility of "linking" two CSS elements in jQuery?
An example from my project:
I'd like to append a .focused class to multiple elements at once when certain things happen. They all have the same prefix in their ID, but there is >1 of those prefixes (like #first-header - #first-button ; #second-header - #second-button and so on). It would probably use some regular expressions, but I can't seem to figure this stuff out.
A more accurate example, because I'm bad at explaining things:
When #first-header is .focused, append the same .focused class to #first-button as well. And that would need to work to any pair of -header and -button.
I hope I explained it well enough and am thankful for any kind of help.
You can easily select all items whose id starts with first- like this
$('[id^="first-"]').addClass('whatever');
It sounds like instead of using patterns in the ID of your HTML elements you should be using classes.
Example HTML:
<div id="first-button" class="button">...</div>
<div id="second-button" class="button">///</div>
Javascript:
$(".button").click(function(){ /*do stuff*/});
That Javascript would attach a click handler to all div elements with the class button.
$('[id$="header"]').on('focus blur', function(e) {
$('#' + this.id.split('-').shift() + '-button').toggleClass('focused', e.type=='focus');
});
FIDDLE
You cannot have multiple ids for one element. The proper way to do what you're doing is to give everything that you want to change a class like "toFocus"
Try this:
$("[name$='header']").focus(function(){
var t = $(this).attr("id").split("-")[0];
$("#"+t+"-button").addClass("focus");
});

Categories

Resources