This question already has answers here:
How do I create an HTML button that acts like a link?
(35 answers)
Closed 1 year ago.
i want to know how to make it when a a button is pressed it will redirect you to a chosen url.
something like this:
<button onclick = "link()">Press to go to link!</button>
function link(){
//leads to url
}
You can use window.open to open in new tab.
function link(){
window.open('http://www.google.com');
}
<button onclick = "link()">Press to go to link!</button>
or
function link(){
window.location.href = 'http://www.google.com'; //Will take you to Google.
}
<button onclick = "link()">Press to go to link!</button>
You can try something like this:
<button class="btn" onclick="window.location.href = 'https://www.stackoverflow.com';">Click this link</button>
or
<button>Click this link</button>
This will direct the page to a expected link
Related
This question already has answers here:
How to put a link on a button with bootstrap?
(12 answers)
Closed 2 years ago.
i have a bootstrap button and i want redirect onclick. but when click nothing happens
<button class="btn btn-option navbar-btn" onclick="location.href='https://stackoverflow.com/';">Enter Class</button>
i also tried location href and call a separate function like this
<button class="btn btn-option navbar-btn" onclick="GoHome()">Enter</button>
function GoHome(){window.location = myurl; }
and if i use alert or something else onclick, it works fine but i cant redirect to another page
It works. Here is demo
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css">
<button class="btn btn-primary navbar-btn" onclick="location.href='https://stackoverflow.com/';">Enter Class</button>
So i wanted to open a new page replacing the current one, i found that the method should be putting the second parameter on _self but nothing happen...
By the way, if i use the _blank parameter or i left it empty it opens in a new page. The rest of the function works good, but i can't find a way to close the current page and open the new one that i want.
Here is the javascript and the html buttom that call the function.
<button id="rgstr_btn" type="submit" class="btn btn-info" onClick="store()">Register</button>
<script>
function store() {
localStorage.setItem('nome', nome.value);
localStorage.setItem('pw', pw.value);
window.open('url', '_self');
}
</script>
Button has a type attribute which defaults to submit: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/button#attr-type
While this does not affect "everyday" buttons, if the button resides in a form, this way it will submit the form, and result in some page loading, which clashes with your own attempt.
You can just add a type="button" attribute to the button to avoid that:
<button id="rgstr_btn" type="button" class="btn btn-info" onClick="store()">Register</button>
^^^^^^^^^^^^^
windows.open() opens the URL in a new window.
To replace the URL in the current window, use:
window.location.href = 'http://example.com';
This question already has answers here:
Why does jQuery or a DOM method such as getElementById not find the element?
(6 answers)
Closed 6 years ago.
I'm trying to write a simple code in JavaScript where selecting a button calls a prompt function, but the prompt never pops.
This is the HTML:
<div id="btnDiv">
<button type="submit" id="btn" onclick="submit"> send info </button>
</div>
And this is the JavaScript code:
document.getElementById("btn").onclick = function(){
prompt("Thank you");
}
What am I doing wrong?
Make sure that the JS code is loaded after the HTML content, you can use onLoad event:
window.onload=function(){
document.getElementById("btn").onclick = function(){
prompt("Thank you");
}
};
I am trying to detect when the bootstrap confirmation is opened and closed but I am having no luck with detecting this. I am using an <a> tag to trigger the confirmation (code down below), and trying to detect this is jquery.
<a class="delete" data-toggle="confirmation" title="" data-original-title="Delete Row?">
<i class="fa fa-trash-o"></i>
</a>
I originally tried to detect the button click but failed in doing so. It would be better if the confirmation is able to trigger a function once opened and closed.
You may you "data-on-confirm" and "data-on-cancel" attributes to register your callbacks for those particular events.These are given in the documentation provided by the bootstrap confirmation plugin.
Eg
<button class="btn btn-default" data-toggle="confirmation" data-singleton="true" data-on-confirm="myAcceptFunction" data-on-cancel="myRejectFunction">Confirmation 1</button>
Use events, e.g.:
var modalIsShown = false;
$('#myModal').on('shown.bs.modal', function () {
modalIsShown = true;
});
$('#myModal').on('hidden.bs.modal', function () {
modalIsShown = false;
});
http://getbootstrap.com/javascript/#modals-events
I ended up finding my answer thanks to the other answers getting me there. The code simply looks at the trigger button which in my case has a class name of delete and then looks for a <div> with the class name popover to see if it is visible or not.
if ($(".delete").next('div.popover:visible').length){
//do something
}
How would I go about making the button open in a new window, emulating "a href, target = _blank"?
I currently have:
<button class="button" onClick="window.location.href='http://www.example.com';">
<span class="icon">Open</span>
</button>
The button isn't in a form, I just want to make it open in a new window.
Opens a new window with the url you supplied :)
<button class="button" onClick="window.open('http://www.example.com');">
<span class="icon">Open</span>
</button>
I couldn't get your method to work #Damien-at-SF...
So I resorted to my old knowledge.
By encasing the input type="button" within a hyperlink element, you can simply declare the target property as so:
<a href="http://www.site.org" target="_blank">
<input type="button" class="button" value="Open" />
</a>
The 'target="_blank"' is the property which makes the browser open the link within a new tab. This attribute has other properties, See: http://www.w3schools.com/tags/att_a_target.asp for further details.
Since the 'value=""' attribute on buttons will write the contained string to the button, a span is not necessary.
Instead of writing:
<element></element>
for most HTML elements you can simply close them with a trailing slash, like so:
<element />
Oh, and finally... a 'button' element has a refresh trigger within it, so I use an 'input type[button]' to avoid triggering the form.
Good Luck Programmers.
Due to StackOverflow's policy I had to change the domain in the example:
https://meta.stackexchange.com/questions/208963/why-are-certain-example-urls-like-http-site-com-and-http-mysite-com-blocke
<input type="button" onclick="window.open(); return false;" value="click me" />
http://www.javascript-coder.com/window-popup/javascript-window-open.phtml
You can acheive this using window.open() method, passing _blank as one of the parameter. You can refer the below links which has more information on this.
http://www.w3schools.com/jsref/met_win_open.asp
http://msdn.microsoft.com/en-us/library/ms536651(v=vs.85).aspx
Hope this will help you.
If you strictly want to stick to using button,Then simply create an open window function as follows:
<script>
function myfunction() {
window.open("mynewpage.html");
}
</script>
Then in your html do the following with your button:
Join
So you would have something like this:
<body>
<script>
function joinfunction() {
window.open("mynewpage.html");
}
</script>
<button onclick="myfunction()" type="button" class="btn btn-default subs-btn">Join</button>