Run remote javascript from browser without modifying DOM

Question!

The only way I know to run remote javascript is to add a tag like

<script src="example.com/ascript.js"></script>

Is it possible to do a similar thing in Javascript without needing to modify the DOM as part of the call?

By : djechlin


Answers

If what you're asking is whether it's possible to dynamically load Javascript from a remote source and run it without inserting a script tag, then there are some ways to do that (though it is unclear why it's a burden to momentarily insert a <script> tag and let the system load and run the code all by itself).

In a nutshell, you can load some Javascript into a string using an Ajax call and you can then run eval() on that string to execute it. This will have all the usual security caveats about running Javascript that comes from an external source in the context of your page.

The Ajax call (unlike <script> tags inserted into the DOM) will be subject to same-origin limitations unless the target site enables access with CORS.

For example:

fetch(someScriptURL).then(function(data) {
    eval(data);
});
By : jfriend00


This has to do with the fact that and short-circuits, and returns the truthy value instead of True or False.

In:

7 >= 77 and 7

7 >= 77 equates to False, so the second operand of and is never evaluated, so it becomes False.

In:

7 >= 7 and 77

7 is equal to 7 (obviously), so the second operand of and is evaluated. Since 77 is truthy, it's what's returned.



You need a logical condition in the while. Perhaps

program.toUpperCase().equals("Y")
By : nicomp


This video can help you solving your question :)
By: admin