I have a .js file which has a class defined. I want to call this class from <script>
from another html file.
ponent.js:
class TryClass {
constructor(name) {
this.name = name;
}
sayHi() {
alert( this.name );
}
}
main.html:
<html>
<script src="./ponent.js" />
<script>
var user = new TryClass( "John" );
user.sayHi();
</script>
<body>
</body>
This doesn't show the alert when I load main.html (from my webserver). However, with the inspect console, I am able to use the TryClass.
How to resolve this?
I have a .js file which has a class defined. I want to call this class from <script>
from another html file.
ponent.js:
class TryClass {
constructor(name) {
this.name = name;
}
sayHi() {
alert( this.name );
}
}
main.html:
<html>
<script src="./ponent.js" />
<script>
var user = new TryClass( "John" );
user.sayHi();
</script>
<body>
</body>
This doesn't show the alert when I load main.html (from my webserver). However, with the inspect console, I am able to use the TryClass.
How to resolve this?
Share Improve this question asked Apr 1, 2018 at 7:13 mkusemkuse 2,5085 gold badges38 silver badges66 bronze badges2 Answers
Reset to default 7after test i think the problem is your format
<!DOCTYPE html>
<html lang="en">
<head>
<script src="ponent.js"></script>
<script>
var user = new TryClass("John");
user.sayHi();
</script>
</head>
<body>
</body>
</html>
you lost</script>
after <script src="./ponent.js" />
,i guess this is the worst error though you also lost </html>
and <head></head>
Please try by
var TryClass = function(name){
this.name = name;
}
TryClass.prototype.sayHi = function () {
alert( this.name );
}