How to call a function when a document is ready for a particular page is loaded from a Common JS file?

Hi there

I have a JS file which contain 5 function
function-1
function-2
function-3
function-4
function-5

What I want
function-1 should run when a particular web page is loaded
function-2 should only run when a particular web page is loaded or ready

and so on
How can I acheive this?

Thanks

functions.js contains the five functions.

html1.html has:

<script src="functions.js"></script>
<script>
  window.onload = function-1();
</script>

html2.html has:

<script src="functions.js"></script>
<script>
  window.onload = function-2();
</script>

… and so on, and so on.

HTH,

:slight_smile:

To make it on document ready, just place the script right before </body> that way the page itself is ready before the script is called. If the scripts are there, there is no need for document.ready.

2 Likes

I am getting error like function is not defined this is my js

$(document).ready(function(){

function myProfile(){
    
    $.get("/CollegeManagementSystem/myprofileget",
            {
               
            }
            
    ).error(
    ).success(
            function(data)
            {
                location.reload(true);
                var obj=data;
                console.log(obj.userName);
            }
     );
}

});

and this from where I am calling it

<jsp:include page="/jsp/includes/hrHeader.jsp"></jsp:include>

This is MyProfile in HR

I am getting error in console function is not defined

You seem to have made the same mistake many other Java developers have made.

Javascript is not at all related to Java (other than both are C derivitives.) JavaScript is not a sub-set nor a scripting alternative to Java. JavaScript is parsed by the browser, JSP files are compiled.

I’ll flag your post and request a moderator to move it to a proper forum. :smile:

V/r,

:slight_smile:

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.