Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9202471
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T23:15:59+00:00 2026-06-17T23:15:59+00:00

I want to define a Javascript object inside a js file and make it

  • 0

I want to define a Javascript object inside a js file and make it possible for users to create new instances of it on their HTML by calling the src on the head and then using “new”. [maybe this is not the best approach, but bear with me] — very basic stuff, right?

Code in JS file myFile.js:

function myObject(){
   this.myMethod = thisMethod;

   function thisMethod(){
     return 2;
   }
}

Code in HTML:

<head>
  <script type="text/javascript" src="http://myserver/myFile.js">
     var customObject = new myObject();     
  </script>
</head>
<body>
   <input type="button" value="Click Me" onclick="alert(customObject.myMethod());">
</body>

However, when you click the button, you get an error:

“Uncaught ReferenceError: customObject is not defined”

I am sure this is about how I am declaring the instance and then calling it. If I put the var declaration inside the js file, the click works:

    function myObject(){
       this.myMethod = thisMethod;

       function thisMethod(){
         return 2;
       }
    }

var customObject = new myObject();

But this defeats the purpose of letting the user create the object and use it at will.

Of course there is a problem with my structure, but all my researches don’t give me a clue. I thought bringing the js file was the same as putting the code inside script tag in the HEAD and instantiating the object there — because if I do that, without .js call, it works.

I have many theories, some of them regarding events and the order where things are loaded, but don’t want to clutter this question.

So the question is: how to you define an object in a js file, then let the user instantiate and call its methods inside the HTML? (Especially for events like onclick)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T23:16:00+00:00Added an answer on June 17, 2026 at 11:16 pm

    The problem is that you can’t use the same tag to both include an external resource and define your javascript code. You have to use 2 separate tags, otherwise whatever code you put inside the tags will be omitted. This works fine:

    <script type="text/javascript" src="http://myserver/myFile.js"></script>    
    <script type="text/javascript">
        var customObject = new myObject();     
    </script>
    

    Hope it helps,

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: How do I make JavaScript Object using a variable String to define
I have a JavaScript object with about 1000 properties and want to create a
I want to associate a JavaScript object with an HTML element. Is there a
Possible Duplicate: Javascript expression to define object’s property name? I'm trying to add objects
I want to define the following object in javascript: var property = 'foo'; var
I'm new to Java and JavaFX I have php,javascript,HTML experience... I'm trying to create
I'm rewriting a JavaScript project, and I want to be able to use object
In JavaScript, you can define an object like this: var d = {1: 'test'};
How can I define a function's prototype in JavaScript? I want to do this
I want to send JavaScript object array as list object to server, the server

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.