1

I'm trying to call a function from my html: tasks-div-upload.html to my other html: task-tareas-actualizadas.html.

I'm including my scripts on the script tags of the html files

I tried to call the function like this

First of all this is the html that calls the function: tasks-divs-upload.html

and the function is in task-tareas-actualizadas.html

I tried to call the function like i do in java that is

writing the class and then the function, for example: people.countPeople(5);

In this case, there are not classes because its an html file so what can I do?

//tasks-divs-upload.html

 function contadorTareas(){
   for(var x = 0; x < divs; x++){
     var numeroTareas = x;
   }
   prueba(numeroTareas);    // <---------
 } 

//task-tareas-actualizadas.html

function prueba(numero){
  console.log(numero);
}

Console shows this error "Uncaught ReferenceError: prueba is not defined"

4
  • 2
    Put your js function(s) to separate js file. And add js file reference to this 2 html files with <script> tag. After that, you can call your function(s) from different pages. Commented Aug 1, 2019 at 11:28
  • 1
    by "to my other htm" do you mean FROM and not "to" here? Commented Aug 1, 2019 at 11:29
  • 1
    Also your loop can be changed to prueba(divs); assuming divs is an integer. You need to post more code and explain what the actual aim is here Commented Aug 1, 2019 at 11:31
  • 1
    If one of the pages is in an iframe, you can call a function in the iframe from the other page. Commented May 27, 2022 at 16:36

2 Answers 2

2

This CAN be done but is mostly a bad idea and is not very common and has some specific requirements. It is best it NOT be done unless the user is aware of the interaction.

IF your task-tareas-actualizadas.html opens tasks-divs-upload.html in a new window then tasks-divs-upload.html can call window.opener.prueba() BUT, if the first window gets closed, it will not be there and they must both be of the same origin.

This interaction can also go the other way if the parent keeps a reference to the child window.

Better to create a JavaScript file say "myfunctions.js" that includes the functions you wish to use and include it in both pages UNLESS for some reason you need/want the pages to interact - say the child page alters the parent page DOM or some such.

Reference https://developer.mozilla.org/en-US/docs/Web/API/Window/opener and https://developer.mozilla.org/en-US/docs/Web/API/Window/open

Sign up to request clarification or add additional context in comments.

Comments

0

Well scripts in HTML are JavaScript code. They need to be either defined in separate .js files or included in html using <script> tags.

It is not possible to define a JavaScript function in a html file and then use it in another html file. You need to define the function is a separate JavaScript file and then include this file in the html page.

You may also use JavaScript modules which are natively supported by modern browsers.

1 Comment

Not strictly true here, it CAN be done in limited cases

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.