Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

243
Views
Javascript: concatenate function with the string

Following the question here:

JavaScript getting filename without extension located in different folder

I would like to concatenate this function with my string.

When I apply this code

"use strict";
window.addEventListener('load', onLoad, false);

function onLoad(evt) {
  let scripts = document.querySelectorAll('script');
  scripts.forEach(scr => {
    if (scr.src != '') {
      let slashPos = scr.src.lastIndexOf('/');
      let filename = scr.src.slice(slashPos + 1);
      filename = filename.replace('.js', '');
      //console.log(filename);
      return filename
    }
  });
}

var string = 'Date = ' + onLoad;

console.log(string)My console shows the full function instead of a certain date.

enter image description here

what should I do to have the date populated properly?

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

You are not calling the function onLoad because there are no parenthesis, you are passing a reference to it which is your entire function.

var string = 'Date = ' + onLoad();

about 4 years ago · Juan Pablo Isaza Report

0

You are not running your function. You are doing something akin:

function return_string() {
 return 'a string';
}

var string = 'Date = ' + return_string;
console.log( string );

That returns the function, not the result. So just execute the function as normal

function return_string() {
 return 'a string';
}

var string = 'Date = ' + return_string();
console.log( string );

about 4 years ago · Juan Pablo Isaza Report

0

This is happening because in javascript if you just call the function without braces it returns the function definition instead of actually invoking the function. So, calling onload like this would return its' function definition. You should call onload() to get the actual return value of the function. But then I saw there's no return from that function as well. So what are you expecting from this function to be returned? If you want to get Date inside JavaScript you should call the Date constructor like this new Date() to get the current Date. For more information on the Date constructor visit mdn.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!