What does href=javascript:{} do? - javascript

What is the reason for using javascript:{} in the code below. Is it similar to href="javascript:" or href="javascript:void(0)"?
This is an example

Let hyperlink look like a link but didn't link anything.
This is an example
if you remove href attribute, that a tag will be normal text.

It makes a button act like a link, but lets you execute custom JS code instead of linking to a webpage.
For example:
Press me pls
acts like
<div onclick="doOnClick()"> Press me pls </a>
<script>
function doOnClick(){ document.write('hi u just pressed me'); }
</script>
but the browser treats the former like a link.

Related

Which call faster href vs onclick

Hi~ i want to calling sequence about anchor tag href property and onclick proerty I have some test
asdfasdf
and click anchor tag. is result show alert and link stackoverflow but
<a onclick="setTimeout(function(){console.log('asdf')})" href="https://stackoverflow.com" >test</a>
this tag first call href proerty! Please explain calling
sequence href and onclick
and if you know another knowledge explain for me Please ToT
So onclick functions actually run before href. This allows you to do a number of really cool things, like stop the link from executing if you want to like so:
function stopLink(event) {
event.preventDefault();
}
click me

JS function does not respond to event onclick

Hi guys I have a a problem I think is a easy one but I don't find the problem. I have a "menu" where I want to send a data to another page when I click one button. So I write a function in js to open a new tab and the send the data. But when I do click, it is like if the function doesn't exist.
<div id="botones">
<input type="button" value="<?=L::misc_export?>" onclick="javascript:pasarDatosParaExportar();" name="boton" />
</div>
<script type="text/javascript">
function pasarDatosParaExportar() {
window.open('calculos.exportar.php?fecha2='+'<?=$_POST['fecha2']?>'+'&hasta='<?=$_POST['hasta']?>'&cuartel='<?=(int)$_GET['cuartel']?>'','_blank');
}
</script>
Remove javascript: from content of your "onclick". It's for <a href="[HERE PUT javascript:]"
If you have link and want to put JS code into "href" parameter, should use it:
click
But you can do the same like this:
<a onclick="alert('success!')">click</a>
You should check the string delimiter characters (') in the parameter to the open function. I guess you probably mean to write something like this
function pasarDatosParaExportar() {
window.open('calculos.exportar.php?fecha2='+'<?=$_POST['fecha2']?>'+'&hasta=<?=$_POST['hasta']?>&cuartel=<?=(int)$_GET['cuartel']?>','_blank');
}
Write the JavaScript code to define the function before its use in the div and above in code because html assumes that this code doesn't exists if it doesn't occur before its use. So just replace the div and script with each other. This should work fine. And there is no need to use javascript: before name of function. yo can use onclick="pasarDatosParaExportar();".

A href not working when onclick works

Subject is a problem. Well I have, for example, this html
Learn more
It doesn't work. Okay, perhaps there are some invisible elements that don't allow to click on it. Here I add onclick property:
Learn more
But now I see that "Hi" message appears (so <a> tag is clickable), but it doesn't change current page.
Well, ok. Now i'm changing javascript to:
<a onclick="window.location.href='/ru/pages/socials.aspx';return false;" href="/ru/pages/socials.aspx">Learn more</a>
and now it works as espected, but I'm looking for non-JS pure HTML solution.
Please, advice. Why <a> could be non-clickable for href, but with workable JS onclick event?
Did you add click event listeners via e.g. JQuery? Maybe you're disabling the default behaviour by something like this:
$(..).click(function(e){
e.preventDefault();
...
});
I would exclude 'wrong' markup ( like an overlapping div or something ) because you can use the link when you add an inline onclick.

<a class="link" href="javascript:;">edit</a>

I am seeing this code in a PHP smarty template file *.tpl:
<span id="crmspanid" style="display:none;position:absolute;" onmouseover="show('crmspanid');">
<a class="link" href="javascript:;">{$APP.EDIT_BUTTON}</a>
</span>
$APP.EDIT_BUTTON is essentially an english workd Edit, hence the above line translates to:
<a class="link" href="javascript:;">Edit</a>
I'd think <a class="link" href="javascript:;">Edit</a> makes "Edit" appear as a link but does nothing when clicking on it.
The show function is like this:
function show(divId)
{
if(getObj(divId))
{
var id = document.getElementById(divId);
id.style.display = 'inline';
}
}
However in this page, if Edit is clicked, the page gets 'expanded' a bit, and a text box together with a couple of buttons are shown: (this is the text box and a Save button and Cancel link that appear after clicking on the Edit).
I am lost as to how to find where the code is which gets gone through after Edit is clicked. Any thoughts are welcomed! many thanks.
Someone attached an onclick handler to the link which contains the actual code that is executed. The href attribute seems to be just a dummy.
The click event handler is probably added with JavaScript code that runs on page load. Look through your JavaScript for the code that runs on page load, then you should be able to find the code that adds the click event handler.
It's not good practice to add event handlers in the HTML. This is something you should be doing in JavaScript. It's also bad practice to use href="javascript:" - links should have real destinations for reliability and accessibility reasons.

why do we need javascript: while making inline javascript calls

We have lot of legacy inline javascript code for img onclick , href clicks ets and those clicks starts with javascript:
javascript:showpopup();
why do we need javascript: before calling the javascript functions.
any explanation will be appreciated.
The javascript: scheme indicates to the browser that it's JavaScript code and not a relative path from the current page's base URL.
For inline event handlers like onclick or onmouseover you don't need the javascript: part.
Link
Without javascript: in the href, clicking that link would try to take you to somewhere like this:
http://www.example.com/something/you_need_it_here();
See #Ignacio's answer for the reason.

Categories