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

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>

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.

OnClick Redirect [duplicate]

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>

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

Edit onclick url in button

I'm trying to edit the url in a button onclick event. Below is the source of the button and the text I'd like to strip from the onclick portion. The end goal is to have a Tampermonkey script that allows me to easily download stp files without manually editing a link after clicking it.
Remove
"fusion360://command=insert&file=" +
From
<button type='button' class="event btn btn-default btn-lg btn-block" onclick='window.location.href = "fusion360://command=insert&file=" + encodeURIComponent("https://assets.sas-automation.com/cad/SWM%203.stp");' data-category="Product Sidebar Right" data-action="Button Click" data-label="Opened CAD File in Fusion" aria-label="button">Open in Fusion360</button>
There are many ways to achieve this, here is one that came to my mind:
Use a getElementsByClassName (or any other selector function) to grab the button
Save the onclick attribute value (which is of text type) to a variable
Find the given substring and replace it with an empty string
Save the result back to the button's onclick attribute.
NOTE: your solution has a gotcha: in order to get a proper redirect on button click, you have to remove the method encodeURIComponent since the url appears to be encoded already.
And you should be good to go. You can play with the snippet.
var button = document.getElementsByClassName('event btn btn-default btn-lg btn-block')[0];
var onClick = button.getAttribute('onclick')
.replace(/"fusion360:\/\/command=insert&file="\s*\+\s/, '')
.replace('encodeURIComponent(', '')
.replace(');', '');
button.setAttribute('onclick', onClick);
<button type='button' class="event btn btn-default btn-lg btn-block" onclick='window.location.href = "fusion360://command=insert&file=" + encodeURIComponent("https://assets.sas-automation.com/cad/SWM%203.stp");'
data-category="Product Sidebar Right" data-action="Button Click" data-label="Opened CAD File in Fusion" aria-label="button">Open in Fusion360</button>

Hiding button in javascript

In HTML file, there are 3 buttons as save,edit and cancel. Im hiding save and edit button depends on the functionality in javascript.
<div class="controls col-sm-9">
<button onclick="modJs.save();return false;" class="saveBtn btn btn-primary pull-right"><i class="fa fa-save"></i> <t>Save</t></button>
<button onclick="modJs.editrecord();return false;" class="EditBtn btn btn-primary " style="display:none;"><i class="fa fa-save"></i> <t>Update</t></button>
<button onclick="modJs.cancel();return false;" class="cancelBtn btn pull-right" style="margin-right:5px;"><i class="fa fa-times-circle-o"></i> <t>Cancel</t></button>
</div>
in my javascript fucntion, im showing and hiding edit and save buttons.
if(object != undefined && object != null) { //editing selected
$(".editBtn").show();
$(".saveBtn").hide();
this.fillForm(object);
}
Already i gave display:none in html tag for edit button. I want to show edit button if object is not null (that means edit). By the above code, the save button get hide, but edit button is not showing.
I guess there is a little spelling mistake there
replace
$(".editBtn").show();
with
$(".EditBtn").show();
Or maybe simply change classname EditBtn to editBtn to keep naming consistent.
In your HTML you're using the class EditBtn, in the JS code you use the class editBtn. Class names are case sensitive! Change it in the HTML to editBtn.

Categories

Resources