0

I created dynamically new window in JavaScript and add some HTML code in it through JavaScript, but when I insert some script link into html head, it does not load when window is open.

<script type="text/javascript">    
function newWindowMap(){

    var myWindow = window.open('','_blank');
    var head = myWindow.document.getElementsByTagName('head')[0];
    var body = myWindow.document.getElementsByTagName('body')[0];

    var jqueryScript = myWindow.document.createElement('script');
    jqueryScript.src = 'jquery.min.js';
    jqueryScript.type = "text/javascript";
    head.appendChild(jqueryScript);

    var alertScr = myWindow.document.createElement('script');
    var inlineScript = 
 document.createTextNode('$(document).ready(function(){alert("Hello World!");});');
    alertScr.appendChild(inlineScript);
    body.appendChild(alertScr);

}
 </script>

Error on console:

Uncaught ReferenceError: $ is not defined at :1:1

1 Answer 1

2

$ is from JQuery, which is a popular library for JavaScript, and from the looks, you haven't imported it.

Add this into your head tag to fix the issue

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js"></script>
Sign up to request clarification or add additional context in comments.

5 Comments

i already tried to do this but not solved the issue. the same problem persists.When i add jquery script in head of html, it works but when i do the same thing dynamically, it doesn't work. why is that?
Is the jquery script inserted before your own JavaScript code? If it isn't, then your code will not know what $ is
I'm looking at your code, and is it that you are trying to load the JQuery script dynamically rather than when the html document is initialized?
its inserted in the header so, obviously before to my java script
Is this what you are looking for? (Assuming you are trying to load it dynamically)

Your Answer

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.