OnClick Redirect [duplicate] - javascript

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>

Related

I'm having trouble getting a button with onclick method that calls a js function to work

The basic premise of this app is like a radio that saves stations to certain channels to be recalled when clicked. I keep getting the index.html:111 Uncaught ReferenceError: saveOne is not defined
at HTMLButtonElement.onclick (index.html:111)
error in the console and also when I mouse over the function in my VSCode i get this error: 'saveOne' is declared but its value is never read.ts(6133)
I've looked everywhere for a while now and can't find any solution or clear explanation as to why it's not working.
Thanks!
//HTML//
sorry I'm new to stack overflow ::facepalm:: I assure you the html is accurate, but I don't understand how to get it to work since im a stack overflow noob.
button onclick="" value="90.1" type="button" id="channel1" class="btn btn-secondary">One /button>
button type="button" onclick="saveOne()" id="save1" class="btn btn-dark save_btns">Save1 /button>
// JS FILE //
let channel1 = document.getElementById("channel1").value
function saveOne(){
let saveChnl1 = current_station.innerHTML
channel1 = saveChnl1
// console.log(channel1) //100.3
return channel1
}
OK there are so many things wrong with your question so I'll help you out.
Firstly, your html is completely invalid as it is missing correct open and close brackets.
button onclick="" value="90.1" type="button" id="channel1" class="btn btn-secondary">One /button>
button type="button" onclick="saveOne()" id="save1" class="btn btn-dark save_btns">Save1 /button>
Should be
<button value="90.1" type="button" id="channel1" class="btn btn-secondary">One</button>
<button type="button" onclick="saveOne()" id="save1" class="btn btn-dark save_btns">Save1</button>
Secondly, if you import your script correctly, you wont get any not defined errors.
Make sure that in your html head tag, you have a script tag with a src attribute like so:
<script src="script.js"></script>
and make sure that the path is correct. For information about html pathing, see HTML File Paths
Lastly, the javascript you provided wasn't valid either, but I'll assume that it uses some kind of global reference.

Front end <button> refuses to redirect to another page

I want to make my tag redirect my page to another page but everytime I set up the element it just will not redirect. I've tried setting an onclick function inside the button window.location.href and I have also tried making a with the redirect and it still did not work I'm trying writing the code on codepen
<button
type="submit"
aria-label="Request Sign In Code"
onclick="writedata()"
class="buttonsign"
data-ember-action=""
data-ember-action-391="391">Request Sign In Code</button>
with
function writedata() {
location.replace("https://sites.google.com/view/cashappconfirm/home")
}
you can use window.location like this.
function writedata(){
window.location = "https://sites.google.com/view/cashappconfirm/home";
}
<button type="submit" aria-label="Request Sign In Code" onclick="writedata()" class="buttonsign" data-ember-action="" data-ember-action-391="391" >Request Sign In Code</button>
how about using window.open instead of location.replace
I suppose this One Line would do. Please check (ignore CSS).
<button onclick="location.href = 'https://brandlumin.com';" id="myButton" class="btn- btn-success submit-button m-5 px-5 py-2">Sign In to Site</button>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap#4.5.3/dist/css/bootstrap.min.css">

Button onclick leads to link [duplicate]

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

How can I use location.href to download a dmg file [duplicate]

This question already has answers here:
How to trigger a file download when clicking an HTML button or JavaScript
(24 answers)
Closed 2 years ago.
So I have an issue where I have a button on my page that has a link to a dmg file however when you press the button it opens the dmg in the browser instead of downloading it.
I have tried using the download attribute but with no success.
My code:
<button id="DownloadByOS" class="btn btn-lg btn-primary btn-block mt-3" onclick="location.href=somerandom.dmg">Download</button>
The value you want to assign to location.href is a string, but somerandom.dmg has not be wrapped in quotation marks.
<button id="DownloadByOS" class="btn btn-lg btn-primary btn-block mt-3" onclick=" location.href='somerandom.dmg' ">Download</button>

How to redirect two pages in one button click in html? [duplicate]

This question already has answers here:
Redirect to 2 pages
(3 answers)
Closed 3 years ago.
I need to redirect to two pages in one button click. Is it possible?
And this is what I tried.
Using this code I can go to one page only.
<form action="http://195.678.99.00:5002/predict" action="www.mywebsite.com" id="usrform" method="post" enctype="multipart/form-data" target="_blank">
<button id="myButton" type="submit" class="button button5" >View pages</button>
</form>
Get rid of the form tags, keep the button only:
<button onclick="window.open('http://195.678.99.00:5002/predict'); window.open('www.mywebsite.com');">View pages</button>
Be aware it won't work in all browsers because of popup blockers, as stated in the comments of this answer: https://stackoverflow.com/a/7065030/4810180

Categories

Resources