如何获取脚本标签的innerHTML

发布于 2024-10-01 10:07:41 字数 382 浏览 0 评论 0原文

好吧...我已经搜索了一小时了...如何获取脚本标签的innerHTML?这是我一直在努力的...

<script type="text/javascript" src="http://www.google.com" id="externalScript"></script>
<script type="text/javascript">
function getSource()
 {document.getElementById('externalScript').innerHTML;
  }
</script>

我一直在尝试寻找一种使用脚本标记调用另一个域的页面源的方法。我见过一个有效的例子,但我一生都找不到它......

Alright... I've been searching for an hour now... How does one get the innerHTML of a script tag? Here is what I've been working on...

<script type="text/javascript" src="http://www.google.com" id="externalScript"></script>
<script type="text/javascript">
function getSource()
 {document.getElementById('externalScript').innerHTML;
  }
</script>

I've been trying to work on a way to call another domain's page source with the script tag. I've seen a working example, but cannot find it for the life of me...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

请爱~陌生人 2024-10-08 10:07:41

你不能那样做。没有innerHTML...您所能做的就是下拉文件视图XMLHttpRequest以获取其内容...但是当然,这受到同源策略的限制,但脚本标签却不受限制。对不起。

You can't do that. There is no innerHTML....all you can do is pull down the file view XMLHttpRequest to get to its contents....but of course, that is limited by same-origin policy, but script tags are not. Sorry.

如若梦似彩虹 2024-10-08 10:07:41

实际上,有一种方法可以获取内容,但它取决于远程服务器是否允许您获取没有有效标头的文件,并且由于这些设置,很多时候仍然会失败。使用 jQuery 因为今天已经结束了,我要出门了......

$.get($('#externalScript').attr('src'), function(data) {
    alert(data);
});

actually, there is a way to get the content, but it depends on the remote server letting you get the file without valid headers and still fails a lot of the time just because of those settings. using jQuery since it's the end of my day and I'm out the door....

$.get($('#externalScript').attr('src'), function(data) {
    alert(data);
});
拥有 2024-10-08 10:07:41

我猜您想要以下两件事之一:

  1. 使 JavaScript 文件全局化(以便其他页面可以调用它)

  2. 获取当前在文件中的脚本

这两个问题都可以通过将脚本移动到 .js 文件,然后使用标签来解决

<script src="[path-to-file]"></script>

I'm guessing you want one of two things:

  1. To make a JavaScript file global (so that other pages can call it)

  2. To get the script that is currently in the file

Both of those can be solved by moving your script to a .js file, and then using the tag

<script src="[path-to-file]"></script>
狂之美人 2024-10-08 10:07:41

你不能这样做。如果可以的话,这将是一个巨大的安全问题。

脚本内容可以包含任意数量的内容。考虑一下:从银行网站上的 URL 加载的脚本可能包含各种内容,例如您的帐号、余额和其他个人信息。该脚本将由您银行的正常页面加载以执行他们想做的事情。

现在,我是一个邪恶的黑客,我怀疑你可能是 Biggo 银行的客户。因此,在我自己的一个页面上,我包含了 Biggo Bank 脚本的

You can't do this. It would be a massive security problem if you could.

Script content can include any number of things. Consider this: a script loaded from a URL on your bank's website might contain all sorts of things, like your account number, your balance, and other personal information. That script would be loaded by your bank's normal pages to do what they want to do.

Now, I'm an evil hacker, and I suspect you may be a customer of Biggo Bank. So on one of my own pages, I include a <script> tag for that Biggo Bank script. The script may only load if there's a valid Biggo Bank session cookie, but what if there is? What if you visit my hacker site while you're logged in to Biggo Bank in another browser tab? Now my own JavaScript code can read the contents of that script, and your money is now mine :)

夏有森光若流苏 2024-10-08 10:07:41

您可以使用 Html 解析器:

jsoup » jsoup:Java HTML 解析器
jsoup:Java HTML 解析器

jsoup 是一个用于处理实际 HTML 的 Java 库。它使用最好的 DOM、CSS 和类似 jquery 的方法,提供了一个非常方便的 API 来提取和操作数据。

参考这个:
http://jsoup.org/

You can Use Html Parsers:

jsoup » jsoup: Java HTML Parser
jsoup: Java HTML Parser

jsoup is a Java library for working with real-world HTML. It provides a very convenient API for extracting and manipulating data, using the best of DOM, CSS, and jquery-like methods.

refer this:
http://jsoup.org/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文