|
I'm taking the w3schools tutorial on javascript. In the tutorial it is discussing numerical sort. It says since javascript doesn't sort numerals you need to use a trick of employing a comparison function with sort.
<pre><!DOCTYPE html>
<html>
<body>
<h2>JavaScript Array Sort</h2>
<p>Click the button to sort the array in ascending order.</p>
<button onclick="myFunction()">Try it</button>
<p id="demo"></p>
<script>
const points = [40, 100, 1, 5, 25, 10];
document.getElementById("demo").innerHTML = points;
function myFunction() {
points.sort(function(a, b){return a - b});
document.getElementById("demo").innerHTML = points;
}
</script>
</body>
</html>
How does function(a, b) get the input a and b? If you could give a detailed message because I'm not getting how it all works.
|
|
|
|
|
The standard sort function takes a comparison function (which "you" supply) as its argument.
As it runs, when it wants to compare two elements of the array being sorted, it calls "your" function with the elements to be compared, and uses the returned value to move the elements according to its algorithm.
The reason for doing it that way is so you can provide whatever sort order you like. for example
function(x,y){return y-x} will sort in reverse numeric order, and function(p,q){console.log(comparing p.toString() to q.toString()); return p-q} will give you a log of all the comparisons made during the sort.
If you don't specify a comparison function, it defaults to a string comparison (which is why it doesn't work with numbers).
Software rusts. Simon Stephenson, ca 1994. So does this signature. me, 2012
|
|
|
|
|
Thank you very much for your response Peter_in_2780. It makes more sense, now that I know sort() is looking for a comparison function with two parameters.
|
|
|
|
|
Afternoon all. I'm putting together a kiosk style website and I'm trying to figure out how to make it recover if the connection to the server is lost. I'm using a soap service (i think...) to pass the data. Currently it just hangs up and the page has to be reloaded.
|
|
|
|
|
If it's the client network dropping off, maybe you could use the network information API to detect changes to the connection state?
Network Information API - Web APIs | MDN[^]
Beyond that, without seeing the relevant parts of the code, we can't tell you how to fix it.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
|
I am ok with all the fundamentals of javascript but i don't know what to build with it or how to even improve my profile or get a job it is really frustrating to be in this position.
|
|
|
|
|
In my opinion, just JavaScript is not enough to know to be able to do much of anything. I would suggest learning backend technologies as well, maybe some php or .Net.
|
|
|
|
|
It's quite surprising that you don't know what to build since JS may be employed not only on a client but on a server as well. Even I as .NET person had to do node.js service at my current workplace to convert doc file content to html. Unfortunately C# port wasn't mature enough.
But anyways the main usage for JS is SPA applications. Given you have any REST API of your interest you may visualize it's responses.
|
|
|
|
|
|
I have been studying javascript, I want to be a frontend developer but I don't know if jquery is the best option nowadays especially for the UI, is it necessary to study jquery? or can I study something like vue js, node js?
What other web technologies do you recommend?
Thank you so much
|
|
|
|
|
E-FANIE Rh wrote: vue js, node js? Those are still Javascript.
You need to know CSS and HTML. I use jQuery a ton so I would recommend learning that.
But I also do not see a lot of front end only jobs so I'd also suggest learning the full stack.
|
|
|
|
|
Python, Ruby, PHP; and, we can study a couple of frameworks, Angular. js and Twitter Bootstrap.
|
|
|
|
|
IMO jQuery is not that popular anymore. First of all I'd suggest learning CSS including newer layout techniques such as flexbox and grid . Then on of vue, angular, react. After that, I'd suggest looking deeper into ecosystem tools. Namely webpack as nowadays it is used for a quite variety of tasks. For starters boilerplates provided by frameworks are enough but eventually you'll need to dig deeper
|
|
|
|
|
I built a Web server myself, want to achieve the entire browser minimization,except "JAVA" and "JSP", what method can achieve this effect. There are a lot of examples of browser minimization using "JS" on the internet, but none of them worked for me. Some people mentioned that it might be the ActiveX controls. I don't know what the problem is. Please help me to solve the problem, because I am a rookie, please tell me detailed , thank you very much! Here is an example of using JS for browser minimization that I found on the Internet:
<object id="min" classid="clsid:ADB880A6-D8FF-11CF-9377-00AA003B7A11">
<param name="Command" value="Minimize" />
<object id="max" classid="clsid:ADB880A6-D8FF-11CF-9377-00AA003B7A11">
<param name="Command" value="Maximize" />
<input type="button" value="minimize" οnclick="min.Click()">
<input type="button" value="maximize" οnclick="max.Click()">
|
|
|
|
|
Javascript cannot change the state of the browser window.
Imagine how annoying it would be if every scammy site you accidentally clicked through to immediately minimized your browser window to hide its nefarious activities!
And no, there is no workaround.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Richard Deeming wrote: immediately minimized your browser window to hide its nefarious activities! ... or set full screen (hiding the magic X) with an onanything() handler to restore itself...
Back in the day that caused quite a few heart attacks.
Software rusts. Simon Stephenson, ca 1994. So does this signature. me, 2012
|
|
|
|
|
Why would you want to do this?
|
|
|
|
|
function confirmDescConfirmIcon(){
$.sendConfirm({
hideHeader: true,
withIcon: true,
hideClose: true,
autoClose: true,
bgHide: false,
escHide: true,
withCenter: true,
button: {
confirm: 'confirmed',
},
onConfirm: function(){
window.open('test.php','','depended=no,height=-200,width=-200,top=2000,left=2000,toolbar=no,menubar=no,scrollbars=no,location=no,status=no');
}
})};
confirmDescConfirmIcon();
This button page implements "window.open()" to open a new window.
I would like to ask how I can automatically bring the button page to focus if I am browsing another page and the button page is not closed. I don't want to use "window.open" again to bring the button page to focus.
Before, I tried to execute a certain page in the background, and then I wanted to use the "window.location.href" to a page that open a new window of the button page after a period of time, so that the button page becomes the focus of execution, but I failed.
Later I tried a page in the background using "window.location.href" to jump to the button page then the button page automatically become the focus, but failed!
Because I am new for js, trouble a little more detailed, please help me! Thank you very much!
|
|
|
|
|
You have already posted this in the thread immediately below this one.
Do not repost your question. If you do not get a reply, that suggests either you haven't provided enough information, or nobody has a solution for you. Posting the same question again will simply annoy people, and could get you banned from the site.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
function confirmDescConfirmIcon(){
$.sendConfirm({
hideHeader: true,
withIcon: true,
hideClose: true,
autoClose: true,
bgHide: false,
escHide: true,
withCenter: true,
button: {
confirm: '确定',
},
onConfirm: function(){
window.open('test.php','','depended=no,height=-200,width=-200,top=2000,left=2000,toolbar=no,menubar=no,scrollbars=no,location=no,status=no');
}
})};
confirmDescConfirmIcon();
This button page implements "window.open()" to open a new window.
I would like to ask how I can automatically bring the button page to focus if I am browsing another page and the button page is not closed. I don't want to use "window.open" again to bring the button page to focus.
Before, I tried to execute a certain page in the background, and then I wanted to use the "window.location.href" to a page that open a new window of the button page after a period of time, so that the button page becomes the focus of execution, but I failed.
Later I tried a page in the background using "window.location.href" to jump to the button page then the button page automatically become the focus, but failed!
Because I am new for js, trouble a little more detailed, please help me! Thank you very much!
|
|
|
|
|
Greetings experts,
I have a two part question but first what I am trying to accomplish and what I have tried so far.
Currently, we have a button on our web page with an Amend. value.
When you click the Amend button, a textbox is displayed which allows users to enter whatever text they wish to enter.
If user changes his/her mind after clicking Amend, users can click Cancel to hide the textbox again.
This works well.
The original intent is to allow users to enter data as an amendment to existing data and submit to the database.
Here is the working code:
$("#btconsultsup").click(function () {
if ($(this).val().toLowerCase() == "amend") {
$.each($('.consultsup'), function(i, item){
$(this).show();
});
$(this).val("Cancel");
} else {
$.each($('.consultsup'), function(i, item){
$(this).hide();
});
$(this).val("Amend");
}
});
<input id="btconsultsup" type="button" value="Amend" name="btconsultsup" />
<div class="consultsup" style="display: none"><input class="shortCosts" id="amendconSupportContractCosts" name="amendconSupportContractCosts" value=""/></div>
However, management has decided that once data is entered into this textbox, (A), once you click Amend button to display the textbox, then Cancel button should be hidden because they want the textbox to display permanently on the page even after the page is closed and reopened; and (B), data should displayed permanently on that textbox.
So, how do I hide the Cancel button so it will no longer be available to users to click on it to hide the textbox once it is already displayed?
Two, how do I ensure that once the Amend button is clicked and textbox is displayed, that it remains displayed even after the page is closed and reopened.
I had suggested to them that the best solution is to just add the textbox to the page without the need for Amend button but they refused.
Any suggestion is greatly appreaciated.
modified 5-Jul-21 9:49am.
|
|
|
|
|
Assuming your server-side code outputs the stored value from the database in the textbox, then you simply need to test on page load whether the textbox is empty, and display it if it has a value.
$(function(){
const displayShortCosts = function(){
$(".consultsup").show();
$("#btconsultsup").hide();
});
if ($("#amendconSupportContractCosts").val()) {
displayShortCosts();
}
else {
$("#btconsultsup").click(displayShortCosts);
}
});
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
modified 5-Jul-21 9:39am.
|
|
|
|
|
Hello Richard,
Thank you very much sir for your awesome response.
Just one follow up question.
So your solution and my JS would work together as shown below?
$(function(){
const displayShortCosts = function(){
$(".consultsup").show();
$("#btconsultsup").hide();
});
if ($("#shortCosts").val()) {
displayShortCosts();
}
else {
$("#btconsultsup").click(displayShortCosts);
}
});
$("#btconsultsup").click(function () {
if ($(this).val().toLowerCase() == "amend") {
$.each($('.consultsup'), function(i, item){
$(this).show();
});
$(this).val("Cancel");
} else {
$.each($('.consultsup'), function(i, item){
$(this).hide();
});
$(this).val("Amend");
}
});
|
|
|
|
|
You wouldn't need your script.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|