简体   繁体   English

HTML 向后包含脚本

[英]HTML Include Script Backwards

I know that one can include JS in their HTML files with a <script src=""></script> include, but can you specify it to load in the script src backwards (reading line by line bottom to top instead of top to bottom)?我知道可以在他们的 HTML 文件中包含 JS,其中包含一个<script src=""></script> ,但是你能指定它向后加载脚本src (从下到上而不是从上到下逐行读取)底部)? I would not like to include the script src with any inline JS or anything like that: it needs to be simply included in (but from bottom to top instead of top to bottom).我不想将脚本src包含在任何内联 JS 或类似的东西中:它需要简单地包含在(但从下到上而不是从上到下)。 Further, I understand I could just flip the script around so that loading it top to bottom isn't an issue, but I would like to know if there is any way I could simply load from bottom to top.此外,我知道我可以翻转脚本,以便从上到下加载它不是问题,但我想知道是否有任何方法可以简单地从下到上加载。

For example, here's a test.js file:例如,这是一个test.js文件:

alert("line 1!");
alert("line 2!");
alert("line 3!");

And if I include it in a test.html file, I could do this in the <head></head> or <body></body> :如果我将它包含在test.html文件中,我可以在<head></head><body></body>中执行此操作:

<script src="test.js"></script> <!-- Assuming They are in the same directory -->

Now when this test.js gets included, it is (as far as I am aware) going to read test.js from top to bottom, so alert("line 1!") will run before alert("line 3!") .现在,当包含此test.js时,它(据我所知)将从上到下读取test.js ,因此alert("line 1!")将在alert("line 3!")之前运行. And, if let's say line 1 in test.js is invalid, then the whole script stops getting included.而且,如果假设test.js中的第 1 行无效,那么整个脚本将停止包含在内。 I am wondering if I can do a <script src="test.js"></script> include where line 3 (the last line) gets read first (so alert("line 3!") ) and the first line gets read last ( alert("line 1!") ).我想知道我是否可以做一个<script src="test.js"></script> include 其中第 3 行(最后一行)首先被读取(所以alert("line 3!") )并且第一行被读取最后阅读( alert("line 1!") )。 I am doing a cyber security challenge and I know that the top of the file I am trying to include is not valid JS, but the bottom is, so theoretically if I can include it from bottom to top I could get my JS to execute before it throws an error.我正在做一个网络安全挑战,我知道我试图包含的文件顶部不是有效的 JS,但底部是,所以理论上如果我可以从下到上包含它,我可以让我的 JS 在执行之前它抛出一个错误。

You can create a function that executes the script contents backward by sending an AJAX request to the target URL, splitting the response by a newline, reversing the resulting array, join into a string then use eval .您可以创建一个 function,它通过向目标 URL 发送 AJAX 请求向后执行脚本内容,用换行符拆分响应,反转结果数组,加入一个字符串,然后使用eval

Something like this:是这样的:

function loadScriptBackwards(url) {
  var xhttp;
  xhttp = new XMLHttpRequest();
  xhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
      eval(this.responseText.split("\n").reverse().join('\n'))
    }
  };
  xhttp.open("GET", url, true);
  xhttp.send();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM