0

When I'm using inline javascript code, everything works fine. As soon as I try to run the code from my script.js, it doesn't work anymore.

I already searched for this problem in google and it always ends up having something to do the DOM and onload or something like that (sorry I'm new to this whole html/css/js thing). It didn't help though, I'm already using defer and I tried onLoad.

<!DOCTYPE html>
<html>
 <head>
 <link rel="stylesheet" href="https://unpkg.com/tippy.js@5/dist/backdrop.css">
 <link rel="stylesheet" href="style.css">
 <script defer src="https://unpkg.com/popper.js@1"></script>
 <script defer src="https://unpkg.com/tippy.js@5"></script>
 <script defer src="script.js"></script>
 </head>
 <body>
 <div class="container">
 <button id="myButton" class="button" title="button">My Button</button> 
 <div class="columns svg">
 <div class="column is-one-third">
 <div class="box">
 <object type="image/svg+xml" data="SVG/Abbildung1.svg"></object> 
 </div>
 </div>
 </div> 
 </body>
</html>
/* SVG */ 
document.getElementById('ebk').addEventListener('mouseover', function(){
 this.style.fill = "red";
});
document.getElementById('ebk').addEventListener('mouseout', function(){
 this.style.fill = "black";
});
tippy('#myButton', {
 content: "I'm a Tippy tooltip!",
});

I get these errors:

Uncaught ReferenceError: tippy is not defined

^ this is my problem

Uncaught TypeError: Cannot read property 'addEventListener' of null

^ this is just annoying, but my page still works fine. Is it because of the SVG not loading fast enough or something like that?

EDIT:

Tippy works now, I created another external .js file and seperated the code for the SVG and tippy, and now it works. Looks like the problem is that I got

<script type="text/javascript" href="../script.js"></script>

in my SVG file. I don't really know why, since I'm new to this.

Though the "addEventListener" error ist still not gone.

asked Nov 2, 2019 at 22:34
7
  • 3
    You're running your code too early. Put your JS file at the end of the document, before the closing body tag or wrap it in a window.load function Commented Nov 2, 2019 at 22:36
  • I tried both, it doesn't help. Commented Nov 2, 2019 at 22:38
  • Where is your element with the ID of ebk in document.getElementById('ebk')? Commented Nov 2, 2019 at 22:39
  • Its inside a SVG file, its a <text> Commented Nov 2, 2019 at 22:39
  • since your script fails at the document.getElementById('ebk') line, then the tippy('#myButton' could not possibly run - assuming the code you've shown is script.js - so, there's no way you could get both errors Commented Nov 2, 2019 at 22:43

2 Answers 2

1

You have to register the onload event on the object element and do your actions when it's fired.

First define an id for the object element:

<!DOCTYPE html>
<html>
 <head>
 <link rel="stylesheet" href="https://unpkg.com/tippy.js@5/dist/backdrop.css">
 <link rel="stylesheet" href="style.css">
 <script defer src="https://unpkg.com/popper.js@1"></script>
 <script defer src="https://unpkg.com/tippy.js@5"></script>
 <script defer src="script.js"></script>
 </head>
 <body>
 <div class="container">
 <button id="myButton" class="button" title="button">My Button</button> 
 <div class="columns svg">
 <div class="column is-one-third">
 <div class="box">
 <object id="svg" type="image/svg+xml" data="SVG/Abbildung1.svg"></object> 
 </div>
 </div>
 </div> 
 </body>
</html>

Then register the onload event on the object:

var svg = document.getElementById('svg')
svg.onload = function () {
var svgDoc = svg.contentDocument; // Access to object document
/* SVG */ 
svgDoc.getElementById('ebk').addEventListener('mouseover', function(){
 this.style.fill = "red";
});
svgDoc.getElementById('ebk').addEventListener('mouseout', function(){
 this.style.fill = "black";
});
tippy('#myButton', {
 content: "I'm a Tippy tooltip!",
});
}

Be shure to move the script tag at the HTML page end.

answered Nov 2, 2019 at 22:45
Sign up to request clarification or add additional context in comments.

7 Comments

Be sure to move the script tag at the HTML page end why? doesn't defer attribute result in the javascript being executed after the document has been parsed?
Thank you, but now it says "Uncaught TypeError: Cannot set property 'onload' of null".
@user3566608 - did you notice <object id="svg" ... the id attribute?
@user3566608 the id of the object element equals the one used in getElementById?
@user3566608 I made an edit, you have to use contentDocument to access object internal values
|
0

Have you tried removing defer from the tippy.js script tag?

The defer attribute tells the browser that it should go on working with the page, and load the script "in background", then run the script when it loads. Scripts with defer never block the page.

It seems like your script.js file depends on tippy.js to work correctly.

And for:

Uncaught TypeError: Cannot read property 'addEventListener' of null

Seems like you don't have any HTML element with ID 'ebk', so you can't attach an event to an element that doesn't exist.

answered Nov 2, 2019 at 22:51

2 Comments

I tried that, all different kinds of combinations, but it sadly still doesn't work. 'ebk' is inside the SVG File I'm loading. The code actually works, I'm just wondering why the errors keeps popping up.
@user3566608 codesandbox.io/s/58675881-lp1l4 this should work, I wrapped your script.js in a document.onload function.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.