Javascript, prevent preventDefault() from being passed to child elements
I have a div container and for some reason I had to add e.preventDefault() but surprisingly it also prevented the anchor elements from doing their job jQuery(‘#anything’).on(“click”, function(e) { e.preventDefault(); }); Link should work I didn’t expect anchors not to work, I’ve never dealt with this problem before. I StopPropagation() tried it as suggested somewhere, but that didn’t work How can I get the anchor to work again? 1> CertainPerfo..: One option is to use an event delegate and exclude a from triggering the listener All s: jQuery(‘#anything’).on(“click”, ‘*:not(a)’, function(e) { e.preventDefault(); }); Link should work
Javascript, ES5+ still have activation objects?
Reading some information about the Javascript execution context. I am reading the following article by Rupesh Mishra. The article states that a new execution context is created every time a new function is executed. The execution context does have two phases, the creation phase and the execution phase, where the code is executed line by line. Some people say that during the creation phase, the JS engine performs three things: Determine the value this Create a scope chain Create activation or variable objects This is the description of the activated object: Create activation object or variable object:Activation object is a special object in JS that contains all variables, function parameters and internal function declaration information. Since the activation object is a special object, it does not have the dunder proto attribute. Question: Is it true that ES5+ still has this activation object structure? If not, what is the current step in the execution context creation phase? 1> Bergi..: No, ES5 (and higher) no longer uses standard JS objects to store variables. It uses lexical environments (with this values and scope chains) that contain various types of environment records for storing variable values. What is the current step in the execution…
Javascript, array to string format
I am in a situation in my Ionic project where I am converting an array to string format (array). Here is an example var fruits = [“Banana”, “Orange”, “Apple”, “Mango”]; should be converted to var strFruits = “[‘Banana’, ‘Orange’, ‘Apple’, ‘Mango’]”; I can do it using loops and string manipulation, but there should be an easy way to solve this problem. 1> qjnr..: Try: const fruits = [‘Banana’, ‘Apple’, ‘Orange’]; const format = “[‘” + fruits.join(“‘, ‘”) + “‘]”; console.log(format); // => [‘Banana’, ‘Apple’, ‘Orange’]
Javascript, how to wait for multiple promises
What I want to accomplish: Gather artist IDs Either find them in the database Or create them Create an event in db and get event_id When both people are finished, the artist and the event people gather together Now loop through artists and event combinations What I got: I’m using Node and mysql. To insert a relationship, I have to wait for the artist to be inserted or created. I tried to do it using the following code: let promises = []; if (artists.length != 0) { for (key in artists) { promises.push( find_artist_id_or_create_new_artist(artists[key]) ) } } await Promise.all(promises); Return an id: async function find_artist_id_or_create_new_artist(artist_name) { return await find_artist_return_id(artist_name, create_artist_return_id) } Find an artist: async function find_artist_return_id(artist_name, callback) { var sql = “SELECT * FROM `artists` WHERE `name` LIKE “+con.escape(artist_name)+” LIMIT 1;” con.query(sql, (err,row) => { if(err) throw err; if (row. length == 0) { return callback(artist_name) } else { return row[0].id } }); } Create an artist async function create_artist_return_id(artist_name) { var sql = “INSERT INTO `artists` (`id`, `name`, `meta_1`, `meta_2`) VALUES (NULL, “+con.escape(artist_name)+”, NULL, NULL)”; con.query(sql, (err, result) => { if(err) throw err; return result.insertId }); } I know I can’t return the con.query function, but I don’t…
How to draw an arc under a word (HTML5, CSS3, JavaScript)?
If I have a word encoded in UTF-8, how can I draw an arc under it? For example, let’s say I have a function that does the job above: I call it arc(). Now, arc(ABCD) should return the following example image: p> For a practical application though, it would be ideal if the output was not an image but UTF-8 encoded text. Is it possible to do this using HTML, CSS and/or Javascript? 1> Pete..: You can use the after element: .arc { display: inline-block; position: relative; } .arc:after { content: ”; display: block; position: absolute; top: calc(100% – 7px); left: 0; right: 0; height:10px; border-radius: 50%; border-bottom: 2px solid black; } abcd
Return same value using count in JSON array, Javascript
My data looks like this: const myObj = { “incidents”: [{ “id”: 4, “fullName”: “edsadas”, “address”: “Bagbaguin, Pandi, Bulacan”, }, { “id”: 5, “fullName”: “reasdsa”, “address”: “Dalig, Balagtas, Bulacan”, }, { “id”: 6, “fullName”: “dsa”, “address”: “Dalig, Balagtas, Bulacan”, }], } My question is, how to use count to return similar values, as shown below: { “Dalig, Balagtas, Bulacan”: 2, “Bagbaguin, Pandi, Bulacan”: 1 } Ele.. 5 You can use this function reduce: var obj = { “incidents”: [{ “id”: 4, “fullName”: “edsadas”, “address”: ” Bagbaguin, Pandi, Bulacan”, }, { “id”: 5, “fullName”: “reasdsa”, “address”: “Dalig, Balagtas, Bulacan”, }, { “id”: 6, “fullName”: ” dsa”, “address”: “Dalig, Balagtas, Bulacan”, } ]}; var result = obj.incidents.reduce((a, c) => { a[c.address] = (a[c.address] || 0) + 1 return a; }, {}); console.log(result); .as-console-wrapper { max-height: 100% !important; top: 0; } Use reduce and comma operator: var obj = { “incidents”: [{ “id”: 4, “fullName”: “edsadas”, “address”: ” Bagbaguin, Pandi, Bulacan”, }, { “id”: 5, “fullName”: “reasdsa”, “address”: “Dalig, Balagtas, Bulacan”, }, { “id”: 6, “fullName”: ” dsa”, “address”: “Dalig, Balagtas, Bulacan”, } ]}, result = obj.incidents .reduce((a, c) => (a[c.address] = (a[c.address] || 0) + 1, a), {}); console.log(result); .as-console-wrapper { max-height:…
Javascript, Nodejs: Search for a specific string in a file
I’m trying to make an application that searches all files containing a specified string under the current directory/subdirectory. As I understand it, this means I need to create a read stream, loop over it, load the read data into an array (given __filename, dirname and if for the word found)! Message not found. Unfortunately I can’t get it to work… any clues? var path = require(‘path’), fs=require(‘fs’); function fromDir(startPath,filter,ext){ if (!fs.existsSync(startPath)){ console.log(“no dir “,startPath); return; }; var files=fs.readdirSync(startPath); let found = files.find((file) => { let thisFilename = path.join(startPath, file); let stat = fs.lstatSync(thisFilename); var readStream = fs.createReadStream(fs); var readline = require(‘readline’); if (stat.isDirectory()) { fromDir(thisFilename, filename,readline, ext); } else { if (path.extname(createReadStream) === ext && path.basename(thisFilename, ext) === filename) { return true; } } }); console.log(‘– your word has found on : ‘,filename,__dirname); } if (!found) { console.log(“Sorry, we didn’t find your term”); } } fromDir(‘./’, process.argv[3], process.argv[2]); Kamil Soleck.. 5 Because not everything is included in the question, I made an assumption: We are looking for complete words (if not, replace the regular expression with a simple indexOf()). Now I split the code into two functions – making it more readable and easier to find files recursively. Sync version:…
Django Chinese-style static files (css, javascript)
Using css, Javascript in Django templates Test environment (r’^css/(?P.*)$’, ‘django.views.static.serve’, {‘document_root’: ‘/var/www/django-demo/css ‘}),(r’^js/(?P.*)$’, ‘django.views.static.serve’, {‘document_root’: ‘/var/www/ django-demo/js’}),(r’^images/(?P.*)$’, ‘django.views.static.serve’, {‘document_root’: ‘ /var/www/django-demo/images’}), Use the following method in the template: Note: os. path.dirname(globals()[“__file__”]) to get the path of the current file, such as (r’^css/(?P.*)$’, ‘django.views .static.serve’, {‘document_root’: os.path.dirname(globals()[“__file__”])+’/css’}), You can use os.path.abspath() The function returns the absolute path of this path. ============== To reference static files such as css, js, gif, etc. in django’s template file, first create a setting.py The DEBUG switch is turned on. 1. Create a directory to store static files in the project directory, such as: medias2. Add a line to url.py patterns: (r’^site_media/(?P. *)$’,’django.views.static.serve’,{‘document_root’:settings.STATIC_PATH}), Also from django.conf import setting3. Add a line to setting.py: STATIC_PATH=’./medias’ After setting this, you can reference the static files stored in media in the template file, such as: Online environment The use of css cannot be avoided in web projects developed with Django , Javascript, js and other static files. As for the processing of these static files, Django’s official website writes: Django itself doesn’t serve static (media) files, such as images, style sheets, or video. It leaves that job to whichever Web server you choose. That is to say, Django itself…