Executing a JavaScript file directly from the browser

前端 未结 6 1098
攒了一身酷
攒了一身酷 2021-01-31 04:31

This sounds like a trivia question but I really need to know.

If you put the URL of an HTML file in the Location bar of your browser, it will render that HTML. That\'s

相关标签:
6条回答
  • 2021-01-31 05:09

    Use Node.js. Download and install node.js and create a http/s server and write down what you want to display in browser. use localhost::portNumber on server as url to run your file. refer to node js doc - https://nodejs.org/dist/latest-v7.x/docs/api/http.html

    Run - http://localhost:3000

    sample code below :

      var http = require("http");
      var server = http.createServer(function(req,res){
      res.writeHead(200,{'Content-Type':'text/html'});
           res.end("hello user");
      }); server.listen(3000);`
    
    0 讨论(0)
  • 2021-01-31 05:10

    In the address bar, you simply write

    javascript:/some javascript code here/;void(0);

    http://www.javascriptkata.com/2007/05/01/execute-javascript-code-directly-in-your-browser/

    0 讨论(0)
  • 2021-01-31 05:14

    This is not possible. The browser has no idea what context the JavaScript should run in; for example, what are the properties of window? If you assume it can come up with some random defaults, what about the behavior of document? If someone does document.body.innerHTML = "foo" what should happen?

    JavaScript, unlike images or HTML pages, is dependent on a context in which it runs. That context could be a HTML page, or it could be a Node server environment, or it could even be Windows Scripting Host. But if you just navigate to a URL, the browser has no idea what context it should run the script in.


    As a workaround, perhaps use about:blank as a host page. Then you can insert the script into the document, giving it the appropriate execution context, by pasting the following in your URL bar:

    javascript:(function () { var el = document.createElement("script"); el.src = "PUT_URL_HERE"; document.body.appendChild(el); })();
    
    0 讨论(0)
  • 2021-01-31 05:23

    Not directly, but you could make a simple server-side script, e.g. in PHP. Instead of

    http://example.com/file.js
    

    , navigate to:

    http://localhost/execute_script.php?url=http://example.com/file.js
    

    Of course, you could smallen this by using RewriteRule in Apache, and/or adding another entry in your hosts file that redirects to 127.0.0.1.

    Note that this is not great in terms of security, but if you use it yourself and know what you're downloading, you should be fine.

    <html>
     <head>
    
      <script>
       <? echo file_get_contents($_GET['url']); ?>
      </script>
    
     </head>
    
     <body>
    
     </body>
    </html>
    
    0 讨论(0)
  • 2021-01-31 05:25

    you can write your own browser using qt /webkit and do that. when user enters a js file in url location you can read that file and execute the javascript .

    http://code.google.com/apis/v8/get_started.html is another channel. not sure if it meets ur need.

    0 讨论(0)
  • 2021-01-31 05:28

    Or you can use RunJS: https://github.com/Dharmoslap/RunJS

    Then you will be able to run .js files just with drag&drop.

    0 讨论(0)
提交回复
热议问题