skip to Main Content

I am writing script tags from the console to my test.html file:

test.html:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery load() Demo</title>
</head>
<body>
<p>nothing</p>
</body>
<script>
</script>

from console:

var script = document.createElement('script');script.src = "https://code.jquery.com/jquery-3.6.4.min.js";document.getElementsByTagName('head')[0].appendChild(script); var script2 = document.createElement('script');script2.type="text/javascript"; script2.src="http://Localhost:Dropbox/prog/jquery/js/ravaf.js"; document.head.appendChild(script2);

resulting in this in my test.html file:

<script src="https://code.jquery.com/jquery-3.6.4.min.js"></script>
<script type="text/javascript" src="http://Localhost:Dropbox/prog/jquery/js/ravaf.js"></script>

I can access jquery functions from the console but not the javascript from ravaf.js

ravaf.js:

var t2=100;

function t3() {
    alert("t3");
}

const t4="front 242";

var "f1"=43334;

why can’t I use any of the functions/variables (from ravaf.jf) in my console?

2

Answers


  1. Chosen as BEST ANSWER

    So I kinda figured it out. Note that the javascript is injected after the page has loaded but my exernal file is loaded and the functions are there to use in the console. note that the path was wrong and also the append. here is the correct code:

    var script = document.createElement('script');script.src = "https://code.jquery.com/jquery-3.6.4.min.js";document.getElementsByTagName('head')[0].appendChild(script); var script= document.createElement('script');script.type="text/javascript"; script.src="file:///Users/jonas/Dropbox/prog/web/jquery/js/ravaf.js"; document.getElementsByTagName('head')[0].appendChild(script);
    

  2. You have a syntax error in your code. If you fix that, it will work

    var t2=100;
    
    function t3() {
        alert("t3");
    }
    
    const t4="front 242";
    
    var f1=43334; // THIS line used to be var "f1"=43334;
    

    Demo of code running in vs code

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search